chiark / gitweb /
use fnmatch() instead of our own pattern match code
[elogind.git] / udevsettle.c
1 /*
2  * udevsettle.c
3  *
4  * Copyright (C) 2006 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <syslog.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32
33 #include "udev.h"
34 #include "udevd.h"
35
36 #define DEFAULT_TIMEOUT                 180
37 #define LOOP_PER_SECOND                 20
38
39
40 #ifdef USE_LOG
41 void log_message(int priority, const char *format, ...)
42 {
43         va_list args;
44
45         if (priority > udev_log_priority)
46                 return;
47
48         va_start(args, format);
49         vsyslog(priority, format, args);
50         va_end(args);
51 }
52 #endif
53
54 int main(int argc, char *argv[], char *envp[])
55 {
56         char queuename[PATH_SIZE];
57         char filename[PATH_SIZE];
58         unsigned long long seq_kernel;
59         unsigned long long seq_udev;
60         char seqnum[32];
61         int fd;
62         ssize_t len;
63         int timeout = DEFAULT_TIMEOUT;
64         int loop;
65         int i;
66         int rc = 1;
67
68         logging_init("udevsettle");
69         udev_config_init();
70         dbg("version %s", UDEV_VERSION);
71         sysfs_init();
72
73         for (i = 1 ; i < argc; i++) {
74                 char *arg = argv[i];
75
76                 if (strncmp(arg, "--timeout=", 10) == 0) {
77                         char *str = &arg[10];
78                         int seconds;
79
80                         seconds = atoi(str);
81                         if (seconds > 0)
82                                 timeout = seconds;
83                         else
84                                 fprintf(stderr, "invalid timeout value\n");
85                         dbg("timeout=%i", timeout);
86                 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
87                         printf("Usage: udevsettle [--help] [--timeout=<seconds>]\n");
88                         goto exit;
89                 } else {
90                         fprintf(stderr, "unrecognized option '%s'\n", arg);
91                         err("unrecognized option '%s'\n", arg);
92                 }
93         }
94
95         strlcpy(queuename, udev_root, sizeof(queuename));
96         strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));
97
98         loop = timeout * LOOP_PER_SECOND;
99         while (loop--) {
100                 /* wait for events in queue to finish */
101                 while (loop--) {
102                         struct stat statbuf;
103
104                         if (stat(queuename, &statbuf) < 0) {
105                                 info("queue is empty");
106                                 break;
107                         }
108                         usleep(1000 * 1000 / LOOP_PER_SECOND);
109                 }
110                 if (loop <= 0) {
111                         info("timeout waiting for queue");
112                         goto exit;
113                 }
114
115                 /* read current kernel seqnum */
116                 strlcpy(filename, sysfs_path, sizeof(filename));
117                 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
118                 fd = open(filename, O_RDONLY);
119                 if (fd < 0)
120                         goto exit;
121                 len = read(fd, seqnum, sizeof(seqnum)-1);
122                 close(fd);
123                 if (len <= 0)
124                         goto exit;
125                 seqnum[len] = '\0';
126                 seq_kernel = strtoull(seqnum, NULL, 10);
127                 info("kernel seqnum = %llu", seq_kernel);
128
129                 /* read current udev seqnum */
130                 strlcpy(filename, udev_root, sizeof(filename));
131                 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
132                 fd = open(filename, O_RDONLY);
133                 if (fd < 0)
134                         goto exit;
135                 len = read(fd, seqnum, sizeof(seqnum)-1);
136                 close(fd);
137                 if (len <= 0)
138                         goto exit;
139                 seqnum[len] = '\0';
140                 seq_udev = strtoull(seqnum, NULL, 10);
141                 info("udev seqnum = %llu", seq_udev);
142
143                 /* make sure all kernel events have arrived in the queue */
144                 if (seq_udev >= seq_kernel) {
145                         info("queue is empty and no pending events left");
146                         rc = 0;
147                         goto exit;
148                 }
149                 usleep(1000 * 1000 / LOOP_PER_SECOND);
150                 info("queue is empty, but events still pending");
151         }
152
153 exit:
154         sysfs_cleanup();
155         logging_close();
156         return rc;
157 }