chiark / gitweb /
udevd: remove useless udevinitsend parameter
[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 static const char *udev_log_str;
40
41 #ifdef USE_LOG
42 void log_message(int priority, const char *format, ...)
43 {
44         va_list args;
45
46         if (priority > udev_log_priority)
47                 return;
48
49         va_start(args, format);
50         vsyslog(priority, format, args);
51         va_end(args);
52 }
53 #endif
54
55 int main(int argc, char *argv[], char *envp[])
56 {
57         char queuename[PATH_SIZE];
58         char filename[PATH_SIZE];
59         unsigned long long seq_kernel;
60         unsigned long long seq_udev;
61         char seqnum[32];
62         int fd;
63         ssize_t len;
64         int timeout = DEFAULT_TIMEOUT;
65         int loop;
66         int i;
67         int rc = 1;
68
69         logging_init("udevsettle");
70         udev_config_init();
71         dbg("version %s", UDEV_VERSION);
72
73         udev_log_str = getenv("UDEV_LOG");
74
75         for (i = 1 ; i < argc; i++) {
76                 char *arg = argv[i];
77
78                 if (strncmp(arg, "--timeout=", 10) == 0) {
79                         char *str = &arg[10];
80
81                         timeout = atoi(str);
82                         dbg("timeout=%i", timeout);
83                         if (timeout <= 0) {
84                                 fprintf(stderr, "Invalid timeout value.\n");
85                                 goto exit;
86                         }
87                 } else {
88                         fprintf(stderr, "Usage: udevsettle [--timeout=<seconds>]\n");
89                         goto exit;
90                 }
91         }
92
93         sysfs_init();
94         strlcpy(queuename, udev_root, sizeof(queuename));
95         strlcat(queuename, "/" EVENT_QUEUE_DIR, sizeof(queuename));
96
97         loop = timeout * LOOP_PER_SECOND;
98         while (loop--) {
99                 /* wait for events in queue to finish */
100                 while (loop--) {
101                         struct stat statbuf;
102
103                         if (stat(queuename, &statbuf) < 0) {
104                                 info("queue is empty");
105                                 break;
106                         }
107                         usleep(1000 * 1000 / LOOP_PER_SECOND);
108                 }
109                 if (loop <= 0) {
110                         info("timeout waiting for queue");
111                         goto exit;
112                 }
113
114                 /* read current kernel seqnum */
115                 strlcpy(filename, sysfs_path, sizeof(filename));
116                 strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
117                 fd = open(filename, O_RDONLY);
118                 if (fd < 0)
119                         goto exit;
120                 len = read(fd, seqnum, sizeof(seqnum)-1);
121                 close(fd);
122                 if (len <= 0)
123                         goto exit;
124                 seqnum[len] = '\0';
125                 seq_kernel = strtoull(seqnum, NULL, 10);
126                 info("kernel seqnum = %llu", seq_kernel);
127
128                 /* read current udev seqnum */
129                 strlcpy(filename, udev_root, sizeof(filename));
130                 strlcat(filename, "/" EVENT_SEQNUM, sizeof(filename));
131                 fd = open(filename, O_RDONLY);
132                 if (fd < 0)
133                         goto exit;
134                 len = read(fd, seqnum, sizeof(seqnum)-1);
135                 close(fd);
136                 if (len <= 0)
137                         goto exit;
138                 seqnum[len] = '\0';
139                 seq_udev = strtoull(seqnum, NULL, 10);
140                 info("udev seqnum = %llu", seq_udev);
141
142                 /* make sure all kernel events have arrived in the queue */
143                 if (seq_udev >= seq_kernel) {
144                         info("queue is empty and no pending events left");
145                         rc = 0;
146                         goto exit;
147                 }
148                 usleep(1000 * 1000 / LOOP_PER_SECOND);
149                 info("queue is empty, but events still pending");
150         }
151
152 exit:
153         sysfs_cleanup();
154         logging_close();
155         return rc;
156 }