diff options
Diffstat (limited to 'daemon/main.c')
-rw-r--r-- | daemon/main.c | 618 |
1 files changed, 0 insertions, 618 deletions
diff --git a/daemon/main.c b/daemon/main.c deleted file mode 100644 index 140bee1..0000000 --- a/daemon/main.c +++ /dev/null | |||
@@ -1,618 +0,0 @@ | |||
1 | /* | ||
2 | usbmuxd - iPhone/iPod Touch USB multiplex server daemon | ||
3 | |||
4 | Copyright (C) 2009 Hector Martin "marcan" <hector@marcansoft.com> | ||
5 | Copyright (C) 2009 Nikias Bassen <nikias@gmx.li> | ||
6 | Copyright (C) 2009 Paul Sladen <libiphone@paul.sladen.org> | ||
7 | |||
8 | This program is free software; you can redistribute it and/or modify | ||
9 | it under the terms of the GNU General Public License as published by | ||
10 | the Free Software Foundation, either version 2 or version 3. | ||
11 | |||
12 | This program is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | GNU General Public License for more details. | ||
16 | |||
17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program; if not, write to the Free Software | ||
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | |||
21 | */ | ||
22 | |||
23 | #define _BSD_SOURCE | ||
24 | #define _GNU_SOURCE | ||
25 | |||
26 | #ifdef HAVE_CONFIG_H | ||
27 | #include <config.h> | ||
28 | #endif | ||
29 | |||
30 | #include <stdio.h> | ||
31 | #include <errno.h> | ||
32 | #include <string.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <signal.h> | ||
35 | #include <unistd.h> | ||
36 | #include <sys/socket.h> | ||
37 | #include <sys/un.h> | ||
38 | #include <sys/stat.h> | ||
39 | #include <sys/types.h> | ||
40 | #include <fcntl.h> | ||
41 | #include <getopt.h> | ||
42 | #include <pwd.h> | ||
43 | #include <grp.h> | ||
44 | |||
45 | #include "log.h" | ||
46 | #include "usb.h" | ||
47 | #include "device.h" | ||
48 | #include "client.h" | ||
49 | |||
50 | static const char *socket_path = "/var/run/usbmuxd"; | ||
51 | static const char *lockfile = "/var/run/usbmuxd.pid"; | ||
52 | |||
53 | int should_exit; | ||
54 | int should_discover; | ||
55 | |||
56 | static int verbose = 0; | ||
57 | static int foreground = 0; | ||
58 | static int drop_privileges = 0; | ||
59 | static const char *drop_user = NULL; | ||
60 | static int opt_udev = 0; | ||
61 | static int opt_exit = 0; | ||
62 | static int exit_signal = 0; | ||
63 | static int daemon_pipe; | ||
64 | |||
65 | static int report_to_parent = 0; | ||
66 | |||
67 | int create_socket(void) { | ||
68 | struct sockaddr_un bind_addr; | ||
69 | int listenfd; | ||
70 | |||
71 | if(unlink(socket_path) == -1 && errno != ENOENT) { | ||
72 | usbmuxd_log(LL_FATAL, "unlink(%s) failed: %s", socket_path, strerror(errno)); | ||
73 | return -1; | ||
74 | } | ||
75 | |||
76 | listenfd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
77 | if (listenfd == -1) { | ||
78 | usbmuxd_log(LL_FATAL, "socket() failed: %s", strerror(errno)); | ||
79 | return -1; | ||
80 | } | ||
81 | |||
82 | bzero(&bind_addr, sizeof(bind_addr)); | ||
83 | bind_addr.sun_family = AF_UNIX; | ||
84 | strcpy(bind_addr.sun_path, socket_path); | ||
85 | if (bind(listenfd, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) != 0) { | ||
86 | usbmuxd_log(LL_FATAL, "bind() failed: %s", strerror(errno)); | ||
87 | return -1; | ||
88 | } | ||
89 | |||
90 | // Start listening | ||
91 | if (listen(listenfd, 5) != 0) { | ||
92 | usbmuxd_log(LL_FATAL, "listen() failed: %s", strerror(errno)); | ||
93 | return -1; | ||
94 | } | ||
95 | |||
96 | chmod(socket_path, 0666); | ||
97 | |||
98 | return listenfd; | ||
99 | } | ||
100 | |||
101 | void handle_signal(int sig) | ||
102 | { | ||
103 | if (sig != SIGUSR1 && sig != SIGUSR2) { | ||
104 | usbmuxd_log(LL_NOTICE,"Caught signal %d, exiting", sig); | ||
105 | should_exit = 1; | ||
106 | } else { | ||
107 | if(opt_udev) { | ||
108 | if (sig == SIGUSR1) { | ||
109 | usbmuxd_log(LL_INFO, "Caught SIGUSR1, checking if we can terminate (no more devices attached)..."); | ||
110 | if (device_get_count() > 0) { | ||
111 | // we can't quit, there are still devices attached. | ||
112 | usbmuxd_log(LL_NOTICE, "Refusing to terminate, there are still devices attached. Kill me with signal 15 (TERM) to force quit."); | ||
113 | } else { | ||
114 | // it's safe to quit | ||
115 | should_exit = 1; | ||
116 | } | ||
117 | } else if (sig == SIGUSR2) { | ||
118 | usbmuxd_log(LL_INFO, "Caught SIGUSR2, scheduling device discovery"); | ||
119 | should_discover = 1; | ||
120 | } | ||
121 | } else { | ||
122 | usbmuxd_log(LL_INFO, "Caught SIGUSR1/2 but we weren't started in --udev mode, ignoring"); | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | |||
127 | void set_signal_handlers(void) | ||
128 | { | ||
129 | struct sigaction sa; | ||
130 | sigset_t set; | ||
131 | |||
132 | // Mask all signals we handle. They will be unmasked by ppoll(). | ||
133 | sigemptyset(&set); | ||
134 | sigaddset(&set, SIGINT); | ||
135 | sigaddset(&set, SIGQUIT); | ||
136 | sigaddset(&set, SIGTERM); | ||
137 | sigaddset(&set, SIGUSR1); | ||
138 | sigaddset(&set, SIGUSR2); | ||
139 | sigprocmask(SIG_SETMASK, &set, NULL); | ||
140 | |||
141 | memset(&sa, 0, sizeof(struct sigaction)); | ||
142 | sa.sa_handler = handle_signal; | ||
143 | sigaction(SIGINT, &sa, NULL); | ||
144 | sigaction(SIGQUIT, &sa, NULL); | ||
145 | sigaction(SIGTERM, &sa, NULL); | ||
146 | sigaction(SIGUSR1, &sa, NULL); | ||
147 | sigaction(SIGUSR2, &sa, NULL); | ||
148 | } | ||
149 | |||
150 | #if defined(__FreeBSD__) || defined(__APPLE__) | ||
151 | static int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask) | ||
152 | { | ||
153 | int ready; | ||
154 | sigset_t origmask; | ||
155 | int to = timeout->tv_sec*1000 + timeout->tv_nsec/1000000; | ||
156 | |||
157 | sigprocmask(SIG_SETMASK, sigmask, &origmask); | ||
158 | ready = poll(fds, nfds, to); | ||
159 | sigprocmask(SIG_SETMASK, &origmask, NULL); | ||
160 | |||
161 | return ready; | ||
162 | } | ||
163 | #endif | ||
164 | |||
165 | int main_loop(int listenfd) | ||
166 | { | ||
167 | int to, cnt, i, dto; | ||
168 | struct fdlist pollfds; | ||
169 | struct timespec tspec; | ||
170 | |||
171 | sigset_t empty_sigset; | ||
172 | sigemptyset(&empty_sigset); // unmask all signals | ||
173 | |||
174 | fdlist_create(&pollfds); | ||
175 | while(!should_exit) { | ||
176 | usbmuxd_log(LL_FLOOD, "main_loop iteration"); | ||
177 | to = usb_get_timeout(); | ||
178 | usbmuxd_log(LL_FLOOD, "USB timeout is %d ms", to); | ||
179 | dto = device_get_timeout(); | ||
180 | usbmuxd_log(LL_FLOOD, "Device timeout is %d ms", to); | ||
181 | if(dto < to) | ||
182 | to = dto; | ||
183 | |||
184 | fdlist_reset(&pollfds); | ||
185 | fdlist_add(&pollfds, FD_LISTEN, listenfd, POLLIN); | ||
186 | usb_get_fds(&pollfds); | ||
187 | client_get_fds(&pollfds); | ||
188 | usbmuxd_log(LL_FLOOD, "fd count is %d", pollfds.count); | ||
189 | |||
190 | tspec.tv_sec = to / 1000; | ||
191 | tspec.tv_nsec = (to % 1000) * 1000000; | ||
192 | cnt = ppoll(pollfds.fds, pollfds.count, &tspec, &empty_sigset); | ||
193 | usbmuxd_log(LL_FLOOD, "poll() returned %d", cnt); | ||
194 | if(cnt == -1) { | ||
195 | if(errno == EINTR) { | ||
196 | if(should_exit) { | ||
197 | usbmuxd_log(LL_INFO, "Event processing interrupted"); | ||
198 | break; | ||
199 | } | ||
200 | if(should_discover) { | ||
201 | should_discover = 0; | ||
202 | usbmuxd_log(LL_INFO, "Device discovery triggered by udev"); | ||
203 | usb_discover(); | ||
204 | } | ||
205 | } | ||
206 | } else if(cnt == 0) { | ||
207 | if(usb_process() < 0) { | ||
208 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | ||
209 | fdlist_free(&pollfds); | ||
210 | return -1; | ||
211 | } | ||
212 | device_check_timeouts(); | ||
213 | } else { | ||
214 | int done_usb = 0; | ||
215 | for(i=0; i<pollfds.count; i++) { | ||
216 | if(pollfds.fds[i].revents) { | ||
217 | if(!done_usb && pollfds.owners[i] == FD_USB) { | ||
218 | if(usb_process() < 0) { | ||
219 | usbmuxd_log(LL_FATAL, "usb_process() failed"); | ||
220 | fdlist_free(&pollfds); | ||
221 | return -1; | ||
222 | } | ||
223 | done_usb = 1; | ||
224 | } | ||
225 | if(pollfds.owners[i] == FD_LISTEN) { | ||
226 | if(client_accept(listenfd) < 0) { | ||
227 | usbmuxd_log(LL_FATAL, "client_accept() failed"); | ||
228 | fdlist_free(&pollfds); | ||
229 | return -1; | ||
230 | } | ||
231 | } | ||
232 | if(pollfds.owners[i] == FD_CLIENT) { | ||
233 | client_process(pollfds.fds[i].fd, pollfds.fds[i].revents); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | fdlist_free(&pollfds); | ||
240 | return 0; | ||
241 | } | ||
242 | |||
243 | /** | ||
244 | * make this program run detached from the current console | ||
245 | */ | ||
246 | static int daemonize(void) | ||
247 | { | ||
248 | pid_t pid; | ||
249 | pid_t sid; | ||
250 | int pfd[2]; | ||
251 | int res; | ||
252 | |||
253 | // already a daemon | ||
254 | if (getppid() == 1) | ||
255 | return 0; | ||
256 | |||
257 | if((res = pipe(pfd)) < 0) { | ||
258 | usbmuxd_log(LL_FATAL, "pipe() failed."); | ||
259 | return res; | ||
260 | } | ||
261 | |||
262 | pid = fork(); | ||
263 | if (pid < 0) { | ||
264 | usbmuxd_log(LL_FATAL, "fork() failed."); | ||
265 | return pid; | ||
266 | } | ||
267 | |||
268 | if (pid > 0) { | ||
269 | // exit parent process | ||
270 | int status; | ||
271 | close(pfd[1]); | ||
272 | |||
273 | if((res = read(pfd[0],&status,sizeof(int))) != sizeof(int)) { | ||
274 | fprintf(stderr, "usbmuxd: ERROR: Failed to get init status from child, check syslog for messages.\n"); | ||
275 | exit(1); | ||
276 | } | ||
277 | if(status != 0) | ||
278 | fprintf(stderr, "usbmuxd: ERROR: Child process exited with error %d, check syslog for messages.\n", status); | ||
279 | exit(status); | ||
280 | } | ||
281 | // At this point we are executing as the child process | ||
282 | // but we need to do one more fork | ||
283 | |||
284 | daemon_pipe = pfd[1]; | ||
285 | close(pfd[0]); | ||
286 | report_to_parent = 1; | ||
287 | |||
288 | // Change the file mode mask | ||
289 | umask(0); | ||
290 | |||
291 | // Create a new SID for the child process | ||
292 | sid = setsid(); | ||
293 | if (sid < 0) { | ||
294 | usbmuxd_log(LL_FATAL, "setsid() failed."); | ||
295 | return -1; | ||
296 | } | ||
297 | |||
298 | pid = fork(); | ||
299 | if (pid < 0) { | ||
300 | usbmuxd_log(LL_FATAL, "fork() failed (second)."); | ||
301 | return pid; | ||
302 | } | ||
303 | |||
304 | if (pid > 0) { | ||
305 | // exit parent process | ||
306 | close(daemon_pipe); | ||
307 | exit(0); | ||
308 | } | ||
309 | |||
310 | // Change the current working directory. | ||
311 | if ((chdir("/")) < 0) { | ||
312 | usbmuxd_log(LL_FATAL, "chdir() failed"); | ||
313 | return -2; | ||
314 | } | ||
315 | // Redirect standard files to /dev/null | ||
316 | if (!freopen("/dev/null", "r", stdin)) { | ||
317 | usbmuxd_log(LL_FATAL, "Redirection of stdin failed."); | ||
318 | return -3; | ||
319 | } | ||
320 | if (!freopen("/dev/null", "w", stdout)) { | ||
321 | usbmuxd_log(LL_FATAL, "Redirection of stdout failed."); | ||
322 | return -3; | ||
323 | } | ||
324 | |||
325 | return 0; | ||
326 | } | ||
327 | |||
328 | static int notify_parent(int status) | ||
329 | { | ||
330 | int res; | ||
331 | |||
332 | report_to_parent = 0; | ||
333 | if ((res = write(daemon_pipe, &status, sizeof(int))) != sizeof(int)) { | ||
334 | usbmuxd_log(LL_FATAL, "Could not notify parent!"); | ||
335 | if(res >= 0) | ||
336 | return -2; | ||
337 | else | ||
338 | return res; | ||
339 | } | ||
340 | close(daemon_pipe); | ||
341 | if (!freopen("/dev/null", "w", stderr)) { | ||
342 | usbmuxd_log(LL_FATAL, "Redirection of stderr failed."); | ||
343 | return -1; | ||
344 | } | ||
345 | return 0; | ||
346 | } | ||
347 | |||
348 | static void usage() | ||
349 | { | ||
350 | printf("usage: usbmuxd [options]\n"); | ||
351 | printf("\t-h|--help Print this message.\n"); | ||
352 | printf("\t-v|--verbose Be verbose (use twice or more to increase).\n"); | ||
353 | printf("\t-f|--foreground Do not daemonize (implies one -v).\n"); | ||
354 | printf("\t-U|--user USER Change to this user after startup (needs usb privileges).\n"); | ||
355 | printf("\t-u|--udev Run in udev operation mode.\n"); | ||
356 | printf("\t-x|--exit Tell a running instance to exit if there are no devices\n"); | ||
357 | printf("\t connected (must be in udev mode).\n"); | ||
358 | printf("\t-X|--force-exit Tell a running instance to exit, even if there are still\n"); | ||
359 | printf("\t devices connected (always works).\n"); | ||
360 | printf("\n"); | ||
361 | } | ||
362 | |||
363 | static void parse_opts(int argc, char **argv) | ||
364 | { | ||
365 | static struct option longopts[] = { | ||
366 | {"help", 0, NULL, 'h'}, | ||
367 | {"foreground", 0, NULL, 'f'}, | ||
368 | {"verbose", 0, NULL, 'v'}, | ||
369 | {"user", 2, NULL, 'U'}, | ||
370 | {"udev", 0, NULL, 'u'}, | ||
371 | {"exit", 0, NULL, 'x'}, | ||
372 | {"force-exit", 0, NULL, 'X'}, | ||
373 | {NULL, 0, NULL, 0} | ||
374 | }; | ||
375 | int c; | ||
376 | |||
377 | while (1) { | ||
378 | c = getopt_long(argc, argv, "hfvuU:xX", longopts, (int *) 0); | ||
379 | if (c == -1) { | ||
380 | break; | ||
381 | } | ||
382 | |||
383 | switch (c) { | ||
384 | case 'h': | ||
385 | usage(); | ||
386 | exit(0); | ||
387 | case 'f': | ||
388 | foreground = 1; | ||
389 | break; | ||
390 | case 'v': | ||
391 | ++verbose; | ||
392 | break; | ||
393 | case 'U': | ||
394 | drop_privileges = 1; | ||
395 | drop_user = optarg; | ||
396 | break; | ||
397 | case 'u': | ||
398 | opt_udev = 1; | ||
399 | break; | ||
400 | case 'x': | ||
401 | opt_exit = 1; | ||
402 | exit_signal = SIGUSR1; | ||
403 | break; | ||
404 | case 'X': | ||
405 | opt_exit = 1; | ||
406 | exit_signal = SIGTERM; | ||
407 | break; | ||
408 | default: | ||
409 | usage(); | ||
410 | exit(2); | ||
411 | } | ||
412 | } | ||
413 | } | ||
414 | |||
415 | int main(int argc, char *argv[]) | ||
416 | { | ||
417 | int listenfd; | ||
418 | int res = 0; | ||
419 | int lfd; | ||
420 | struct flock lock; | ||
421 | char pids[10]; | ||
422 | |||
423 | parse_opts(argc, argv); | ||
424 | |||
425 | argc -= optind; | ||
426 | argv += optind; | ||
427 | |||
428 | if (!foreground) { | ||
429 | verbose += LL_WARNING; | ||
430 | log_enable_syslog(); | ||
431 | } else { | ||
432 | verbose += LL_NOTICE; | ||
433 | } | ||
434 | |||
435 | /* set log level to specified verbosity */ | ||
436 | log_level = verbose; | ||
437 | |||
438 | usbmuxd_log(LL_NOTICE, "usbmuxd v%s starting up", USBMUXD_VERSION); | ||
439 | should_exit = 0; | ||
440 | should_discover = 0; | ||
441 | |||
442 | set_signal_handlers(); | ||
443 | signal(SIGPIPE, SIG_IGN); | ||
444 | |||
445 | res = lfd = open(lockfile, O_WRONLY|O_CREAT, 0644); | ||
446 | if(res == -1) { | ||
447 | usbmuxd_log(LL_FATAL, "Could not open lockfile"); | ||
448 | goto terminate; | ||
449 | } | ||
450 | lock.l_type = F_WRLCK; | ||
451 | lock.l_whence = SEEK_SET; | ||
452 | lock.l_start = 0; | ||
453 | lock.l_len = 0; | ||
454 | fcntl(lfd, F_GETLK, &lock); | ||
455 | close(lfd); | ||
456 | if (lock.l_type != F_UNLCK) { | ||
457 | if (opt_exit) { | ||
458 | if (lock.l_pid && !kill(lock.l_pid, 0)) { | ||
459 | usbmuxd_log(LL_NOTICE, "Sending signal %d to instance with pid %d", exit_signal, lock.l_pid); | ||
460 | res = 0; | ||
461 | if (kill(lock.l_pid, exit_signal) < 0) { | ||
462 | usbmuxd_log(LL_FATAL, "Could not deliver signal %d to pid %d", exit_signal, lock.l_pid); | ||
463 | res = -1; | ||
464 | } | ||
465 | goto terminate; | ||
466 | } else { | ||
467 | usbmuxd_log(LL_ERROR, "Could not determine pid of the other running instance!"); | ||
468 | res = -1; | ||
469 | goto terminate; | ||
470 | } | ||
471 | } else { | ||
472 | if (!opt_udev) { | ||
473 | usbmuxd_log(LL_ERROR, "Another instance is already running (pid %d). exiting.", lock.l_pid); | ||
474 | res = -1; | ||
475 | } else { | ||
476 | usbmuxd_log(LL_NOTICE, "Another instance is already running (pid %d). Telling it to check for devices.", lock.l_pid); | ||
477 | if (lock.l_pid && !kill(lock.l_pid, 0)) { | ||
478 | usbmuxd_log(LL_NOTICE, "Sending signal SIGUSR2 to instance with pid %d", lock.l_pid); | ||
479 | res = 0; | ||
480 | if (kill(lock.l_pid, SIGUSR2) < 0) { | ||
481 | usbmuxd_log(LL_FATAL, "Could not deliver SIGUSR2 to pid %d", lock.l_pid); | ||
482 | res = -1; | ||
483 | } | ||
484 | } else { | ||
485 | usbmuxd_log(LL_ERROR, "Could not determine pid of the other running instance!"); | ||
486 | res = -1; | ||
487 | } | ||
488 | } | ||
489 | goto terminate; | ||
490 | } | ||
491 | } | ||
492 | unlink(lockfile); | ||
493 | |||
494 | if (opt_exit) { | ||
495 | usbmuxd_log(LL_NOTICE, "No running instance found, none killed. exiting."); | ||
496 | goto terminate; | ||
497 | } | ||
498 | |||
499 | if (!foreground) { | ||
500 | if ((res = daemonize()) < 0) { | ||
501 | fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n"); | ||
502 | usbmuxd_log(LL_FATAL, "Could not daemonize!"); | ||
503 | goto terminate; | ||
504 | } | ||
505 | } | ||
506 | |||
507 | // now open the lockfile and place the lock | ||
508 | res = lfd = open(lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644); | ||
509 | if(res < 0) { | ||
510 | usbmuxd_log(LL_FATAL, "Could not open lockfile"); | ||
511 | goto terminate; | ||
512 | } | ||
513 | lock.l_type = F_WRLCK; | ||
514 | lock.l_whence = SEEK_SET; | ||
515 | lock.l_start = 0; | ||
516 | lock.l_len = 0; | ||
517 | if ((res = fcntl(lfd, F_SETLK, &lock)) < 0) { | ||
518 | usbmuxd_log(LL_FATAL, "Lockfile locking failed!"); | ||
519 | goto terminate; | ||
520 | } | ||
521 | sprintf(pids, "%d", getpid()); | ||
522 | if ((res = write(lfd, pids, strlen(pids))) != strlen(pids)) { | ||
523 | usbmuxd_log(LL_FATAL, "Could not write pidfile!"); | ||
524 | if(res >= 0) | ||
525 | res = -2; | ||
526 | goto terminate; | ||
527 | } | ||
528 | |||
529 | usbmuxd_log(LL_INFO, "Creating socket"); | ||
530 | res = listenfd = create_socket(); | ||
531 | if(listenfd < 0) | ||
532 | goto terminate; | ||
533 | |||
534 | // drop elevated privileges | ||
535 | if (drop_privileges && (getuid() == 0 || geteuid() == 0)) { | ||
536 | struct passwd *pw; | ||
537 | if (!drop_user) { | ||
538 | usbmuxd_log(LL_FATAL, "No user to drop privileges to?"); | ||
539 | res = -1; | ||
540 | goto terminate; | ||
541 | } | ||
542 | pw = getpwnam(drop_user); | ||
543 | if (!pw) { | ||
544 | usbmuxd_log(LL_FATAL, "Dropping privileges failed, check if user '%s' exists!", drop_user); | ||
545 | res = -1; | ||
546 | goto terminate; | ||
547 | } | ||
548 | if (pw->pw_uid == 0) { | ||
549 | usbmuxd_log(LL_INFO, "Not dropping privileges to root"); | ||
550 | } else { | ||
551 | if ((res = initgroups(drop_user, pw->pw_gid)) < 0) { | ||
552 | usbmuxd_log(LL_FATAL, "Failed to drop privileges (cannot set supplementary groups)"); | ||
553 | goto terminate; | ||
554 | } | ||
555 | if ((res = setgid(pw->pw_gid)) < 0) { | ||
556 | usbmuxd_log(LL_FATAL, "Failed to drop privileges (cannot set group ID to %d)", pw->pw_gid); | ||
557 | goto terminate; | ||
558 | } | ||
559 | if ((res = setuid(pw->pw_uid)) < 0) { | ||
560 | usbmuxd_log(LL_FATAL, "Failed to drop privileges (cannot set user ID to %d)", pw->pw_uid); | ||
561 | goto terminate; | ||
562 | } | ||
563 | |||
564 | // security check | ||
565 | if (setuid(0) != -1) { | ||
566 | usbmuxd_log(LL_FATAL, "Failed to drop privileges properly!"); | ||
567 | res = -1; | ||
568 | goto terminate; | ||
569 | } | ||
570 | if (getuid() != pw->pw_uid || getgid() != pw->pw_gid) { | ||
571 | usbmuxd_log(LL_FATAL, "Failed to drop privileges properly!"); | ||
572 | res = -1; | ||
573 | goto terminate; | ||
574 | } | ||
575 | usbmuxd_log(LL_NOTICE, "Successfully dropped privileges to '%s'", drop_user); | ||
576 | } | ||
577 | } | ||
578 | |||
579 | client_init(); | ||
580 | device_init(); | ||
581 | usbmuxd_log(LL_INFO, "Initializing USB"); | ||
582 | if((res = usb_init()) < 0) | ||
583 | goto terminate; | ||
584 | |||
585 | usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s"); | ||
586 | |||
587 | usbmuxd_log(LL_NOTICE, "Initialization complete"); | ||
588 | |||
589 | if (report_to_parent) | ||
590 | if((res = notify_parent(0)) < 0) | ||
591 | goto terminate; | ||
592 | |||
593 | if(opt_udev) | ||
594 | usb_autodiscover(0); // discovery triggered by udev | ||
595 | |||
596 | res = main_loop(listenfd); | ||
597 | if(res < 0) | ||
598 | usbmuxd_log(LL_FATAL, "main_loop failed"); | ||
599 | |||
600 | usbmuxd_log(LL_NOTICE, "usbmuxd shutting down"); | ||
601 | device_kill_connections(); | ||
602 | usb_shutdown(); | ||
603 | device_shutdown(); | ||
604 | client_shutdown(); | ||
605 | usbmuxd_log(LL_NOTICE, "Shutdown complete"); | ||
606 | |||
607 | terminate: | ||
608 | log_disable_syslog(); | ||
609 | |||
610 | if (res < 0) | ||
611 | res = -res; | ||
612 | else | ||
613 | res = 0; | ||
614 | if (report_to_parent) | ||
615 | notify_parent(res); | ||
616 | |||
617 | return res; | ||
618 | } | ||