summaryrefslogtreecommitdiffstats
path: root/usbmuxd/main.c
diff options
context:
space:
mode:
authorGravatar Hector Martin2009-08-19 01:37:38 +0200
committerGravatar Hector Martin2009-08-19 03:56:09 +0200
commit3a6f72edfb352ab57f58fb4fe4b6314daaddb362 (patch)
treec9fc4ae663e0bf38ea411a31587b021bc4464442 /usbmuxd/main.c
parent2c5b67950a2a7257d03c57ae6cbc7d8e3f6bcaee (diff)
downloadusbmuxd-3a6f72edfb352ab57f58fb4fe4b6314daaddb362.tar.gz
usbmuxd-3a6f72edfb352ab57f58fb4fe4b6314daaddb362.tar.bz2
Reorder stuff in main() to be saner
Diffstat (limited to 'usbmuxd/main.c')
-rw-r--r--usbmuxd/main.c55
1 files changed, 29 insertions, 26 deletions
diff --git a/usbmuxd/main.c b/usbmuxd/main.c
index 525e095..182178f 100644
--- a/usbmuxd/main.c
+++ b/usbmuxd/main.c
@@ -373,29 +373,6 @@ int main(int argc, char *argv[])
goto terminate;
}
- usbmuxd_log(LL_INFO, "Creating socket");
- listenfd = create_socket();
- if(listenfd < 0)
- return 1;
-
- client_init();
- device_init();
- usbmuxd_log(LL_INFO, "Initializing USB");
- if((res = usb_init()) < 0)
- return 2;
- usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s");
-
- usbmuxd_log(LL_NOTICE, "Initialization complete");
-
- if (!foreground) {
- if (daemonize() < 0) {
- fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n");
- usbmuxd_log(LL_ERROR, "FATAL: Could not daemonize!");
- log_disable_syslog();
- exit(EXIT_FAILURE);
- }
- }
-
// now open the lockfile and place the lock
lfd = fopen(lockfile, "w");
if (lfd) {
@@ -403,13 +380,21 @@ int main(int argc, char *argv[])
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
- if (fcntl(fileno(lfd), F_SETLK, &lock) == -1) {
+ if ((res = fcntl(fileno(lfd), F_SETLK, &lock)) < 0) {
usbmuxd_log(LL_FATAL, "Lockfile locking failed!");
- log_disable_syslog();
- exit(EXIT_FAILURE);
+ goto terminate;
}
+ } else {
+ usbmuxd_log(LL_FATAL, "Lockfile open failed!");
+ res = -1;
+ goto terminate;
}
+ usbmuxd_log(LL_INFO, "Creating socket");
+ res = listenfd = create_socket();
+ if(listenfd < 0)
+ goto terminate;
+
// drop elevated privileges
if (drop_privileges && (getuid() == 0 || geteuid() == 0)) {
struct passwd *pw = getpwnam(drop_user);
@@ -446,6 +431,24 @@ int main(int argc, char *argv[])
usbmuxd_log(LL_NOTICE, "Successfully dropped privileges to '%s'", drop_user);
}
+ client_init();
+ device_init();
+ usbmuxd_log(LL_INFO, "Initializing USB");
+ if((res = usb_init()) < 0)
+ goto terminate;
+
+ usbmuxd_log(LL_INFO, "%d device%s detected", res, (res==1)?"":"s");
+
+ usbmuxd_log(LL_NOTICE, "Initialization complete");
+
+ if (!foreground) {
+ if ((res = daemonize()) < 0) {
+ fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n");
+ usbmuxd_log(LL_FATAL, "Could not daemonize!");
+ goto terminate;
+ }
+ }
+
res = main_loop(listenfd);
if(res < 0)
usbmuxd_log(LL_FATAL, "main_loop failed");