chiark / gitweb /
util_run_program(): fix possible buffer overflow #2
[elogind.git] / udev / udevadm-settle.c
1 /*
2  * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
3  * Copyright (C) 2009 Canonical Ltd.
4  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <syslog.h>
29 #include <getopt.h>
30 #include <signal.h>
31 #include <time.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include "udev.h"
36
37 #define DEFAULT_TIMEOUT                 180
38 #define LOOP_PER_SECOND                 20
39
40 static volatile sig_atomic_t is_timeout;
41
42 static void sig_handler(int signum)
43 {
44         switch (signum) {
45                 case SIGALRM:
46                         is_timeout = 1;
47                 case SIGUSR1:
48                         ;
49         }
50 }
51
52 int udevadm_settle(struct udev *udev, int argc, char *argv[])
53 {
54         static const struct option options[] = {
55                 { "seq-start", required_argument, NULL, 's' },
56                 { "seq-end", required_argument, NULL, 'e' },
57                 { "timeout", required_argument, NULL, 't' },
58                 { "exit-if-exists", required_argument, NULL, 'E' },
59                 { "quiet", no_argument, NULL, 'q' },
60                 { "help", no_argument, NULL, 'h' },
61                 {}
62         };
63         unsigned long long start = 0;
64         unsigned long long end = 0;
65         int quiet = 0;
66         const char *exists = NULL;
67         int timeout = DEFAULT_TIMEOUT;
68         struct sigaction act;
69         struct udev_queue *udev_queue = NULL;
70         int rc = 1;
71
72         dbg(udev, "version %s\n", VERSION);
73
74         /* set signal handlers */
75         memset(&act, 0x00, sizeof(act));
76         act.sa_handler = sig_handler;
77         sigemptyset (&act.sa_mask);
78         act.sa_flags = 0;
79         sigaction(SIGALRM, &act, NULL);
80         sigaction(SIGUSR1, &act, NULL);
81
82         while (1) {
83                 int option;
84                 int seconds;
85
86                 option = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL);
87                 if (option == -1)
88                         break;
89
90                 switch (option) {
91                 case 's':
92                         start = strtoull(optarg, NULL, 0);
93                         break;
94                 case 'e':
95                         end = strtoull(optarg, NULL, 0);
96                         break;
97                 case 't':
98                         seconds = atoi(optarg);
99                         if (seconds >= 0)
100                                 timeout = seconds;
101                         else
102                                 fprintf(stderr, "invalid timeout value\n");
103                         dbg(udev, "timeout=%i\n", timeout);
104                         break;
105                 case 'q':
106                         quiet = 1;
107                         break;
108                 case 'E':
109                         exists = optarg;
110                         break;
111                 case 'h':
112                         printf("Usage: udevadm settle OPTIONS\n"
113                                "  --timeout=<seconds>     maximum time to wait for events\n"
114                                "  --seq-start=<seqnum>    first seqnum to wait for\n"
115                                "  --seq-end=<seqnum>      last seqnum to wait for\n"
116                                "  --exit-if-exists=<file> stop waiting if file exists\n"
117                                "  --quiet                 do not print list after timeout\n"
118                                "  --help\n\n");
119                         exit(0);
120                 }
121         }
122
123         if (timeout > 0)
124                 alarm(timeout);
125         else
126                 is_timeout = 1;
127
128         udev_queue = udev_queue_new(udev);
129         if (udev_queue == NULL)
130                 exit(2);
131
132         if (start > 0) {
133                 unsigned long long kernel_seq;
134
135                 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
136
137                 /* unless specified, the last event is the current kernel seqnum */
138                 if (end == 0)
139                         end = udev_queue_get_kernel_seqnum(udev_queue);
140
141                 if (start > end) {
142                         err(udev, "seq-start larger than seq-end, ignoring\n");
143                         fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
144                         start = 0;
145                         end = 0;
146                 }
147
148                 if (start > kernel_seq || end > kernel_seq) {
149                         err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
150                         fprintf(stderr, "seq-start or seq-end larger than current kernel value, ignoring\n");
151                         start = 0;
152                         end = 0;
153                 }
154                 info(udev, "start=%llu end=%llu current=%llu\n", start, end, kernel_seq);
155         } else {
156                 if (end > 0) {
157                         err(udev, "seq-end needs seq-start parameter, ignoring\n");
158                         fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
159                         end = 0;
160                 }
161         }
162
163         /* guarantee that the udev daemon isn't pre-processing */
164         if (getuid() == 0) {
165                 struct udev_ctrl *uctrl;
166
167                 uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
168                 if (uctrl != NULL) {
169                         sigset_t mask, oldmask;
170
171                         sigemptyset(&mask);
172                         sigaddset(&mask, SIGUSR1);
173                         sigaddset(&mask, SIGALRM);
174                         sigprocmask(SIG_BLOCK, &mask, &oldmask);
175                         if (udev_ctrl_send_settle(uctrl) > 0)
176                                 sigsuspend(&oldmask);
177                         sigprocmask(SIG_SETMASK, &oldmask, NULL);
178                         udev_ctrl_unref(uctrl);
179                 }
180         }
181
182         while (1) {
183                 struct stat statbuf;
184                 const struct timespec duration = { 0 , 1000 * 1000 * 1000 / LOOP_PER_SECOND };
185
186                 if (exists != NULL && stat(exists, &statbuf) == 0) {
187                         rc = 0;
188                         break;
189                 }
190
191                 if (start > 0) {
192                         /* if asked for, wait for a specific sequence of events */
193                         if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) {
194                                 rc = 0;
195                                 break;
196                         }
197                 } else {
198                         /* exit if queue is empty */
199                         if (udev_queue_get_queue_is_empty(udev_queue)) {
200                                 rc = 0;
201                                 break;
202                         }
203                 }
204
205                 if (is_timeout)
206                         break;
207
208                 nanosleep(&duration, NULL);
209         }
210
211         /* if we reached the timeout, print the list of remaining events */
212         if (is_timeout) {
213                 struct udev_list_entry *list_entry;
214
215                 if (!quiet && udev_queue_get_queued_list_entry(udev_queue) != NULL) {
216                         info(udev, "timeout waiting for udev queue\n");
217                         printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
218                         udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
219                                 printf("  %s (%s)\n",
220                                        udev_list_entry_get_name(list_entry),
221                                        udev_list_entry_get_value(list_entry));
222                 }
223         }
224
225         udev_queue_unref(udev_queue);
226         return rc;
227 }