From b487d1f0edf0bf882b8898d322a3b0a7119b1826 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Sun, 19 Nov 2023 20:29:03 +0800 Subject: [PATCH] Drop --foreground Always run in foreground by default. Service manager and supervisors expect services to run in foreground, so they can track the lifetime of the child process. For users running `usbmuxd` manually, running in background by default provides a confusing experience, since it gives the impression of exiting immediately. Anyone needing to run `usbmuxd` in background can use a shell's & operator or something like nohop. --- NEWS | 2 + README.md | 4 +- docs/usbmuxd.8 | 3 -- src/main.c | 104 +------------------------------------------------ 4 files changed, 5 insertions(+), 108 deletions(-) diff --git a/NEWS b/NEWS index b2ba013a..f3e8179b 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,8 @@ Version 1.1.1 - Unify and improve log message output - Improve README.md with project description, installation, contributing and usage sections + - The "--foreground" flag has been removed; usbmuxd now runs in foreground by + default. Version 1.1.0 ~~~~~~~~~~~~~ diff --git a/README.md b/README.md index 82493582..a020229d 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,8 @@ The daemon also manages pairing records with iOS devices and the host in Ensure proper permissions are setup for the daemon to access the directory. -For debugging purposes it is helpful to start usbmuxd using the foreground `-f` -argument and enable verbose mode `-v` to get suitable logs. +For debugging purposes it is helpful to enable verbose mode `-v` to get +suitable logs. Please consult the usage information or manual page for a full documentation of available command line options: diff --git a/docs/usbmuxd.8 b/docs/usbmuxd.8 index 590afdc2..ca2b3041 100644 --- a/docs/usbmuxd.8 +++ b/docs/usbmuxd.8 @@ -32,9 +32,6 @@ Ensure proper permissions are setup for the daemon to access the directory. .B \-U, \-\-user USER Change to this user after startup (needs USB privileges). .TP -.B \-f, \-\-foreground -Do not daemonize (implies one -v). -.TP .B \-n, \-\-disable-hotplug Disables automatic discovery of devices on hotplug. Starting another instance will trigger discovery instead. diff --git a/src/main.c b/src/main.c index 8702a4b3..ee1d7b14 100644 --- a/src/main.c +++ b/src/main.c @@ -65,7 +65,6 @@ int no_preflight = 0; // Global state for main.c static int verbose = 0; -static int foreground = 0; static int drop_privileges = 0; static const char *drop_user = NULL; static int opt_disable_hotplug = 0; @@ -393,88 +392,6 @@ static int main_loop(int listenfd) return 0; } -/** - * make this program run detached from the current console - */ -static int daemonize(void) -{ - pid_t pid; - pid_t sid; - int pfd[2]; - int res; - - // already a daemon - if (getppid() == 1) - return 0; - - if((res = pipe(pfd)) < 0) { - usbmuxd_log(LL_FATAL, "pipe() failed."); - return res; - } - - pid = fork(); - if (pid < 0) { - usbmuxd_log(LL_FATAL, "fork() failed."); - return pid; - } - - if (pid > 0) { - // exit parent process - int status; - close(pfd[1]); - - if((res = read(pfd[0],&status,sizeof(int))) != sizeof(int)) { - fprintf(stderr, "usbmuxd: ERROR: Failed to get init status from child, check syslog for messages.\n"); - exit(1); - } - if(status != 0) - fprintf(stderr, "usbmuxd: ERROR: Child process exited with error %d, check syslog for messages.\n", status); - exit(status); - } - // At this point we are executing as the child process - // but we need to do one more fork - - daemon_pipe = pfd[1]; - close(pfd[0]); - report_to_parent = 1; - - // Create a new SID for the child process - sid = setsid(); - if (sid < 0) { - usbmuxd_log(LL_FATAL, "setsid() failed."); - return -1; - } - - pid = fork(); - if (pid < 0) { - usbmuxd_log(LL_FATAL, "fork() failed (second)."); - return pid; - } - - if (pid > 0) { - // exit parent process - close(daemon_pipe); - exit(0); - } - - // Change the current working directory. - if ((chdir("/")) < 0) { - usbmuxd_log(LL_FATAL, "chdir() failed"); - return -2; - } - // Redirect standard files to /dev/null - if (!freopen("/dev/null", "r", stdin)) { - usbmuxd_log(LL_FATAL, "Redirection of stdin failed."); - return -3; - } - if (!freopen("/dev/null", "w", stdout)) { - usbmuxd_log(LL_FATAL, "Redirection of stdout failed."); - return -3; - } - - return 0; -} - static int notify_parent(int status) { int res; @@ -504,7 +421,6 @@ static void usage() printf("OPTIONS:\n"); printf(" -h, --help\t\tPrint this message.\n"); printf(" -v, --verbose\t\tBe verbose (use twice or more to increase).\n"); - printf(" -f, --foreground\tDo not daemonize (implies one -v).\n"); printf(" -U, --user USER\tChange to this user after startup (needs USB privileges).\n"); printf(" -n, --disable-hotplug\tDisables automatic discovery of devices on hotplug.\n"); printf(" \tStarting another instance will trigger discovery instead.\n"); @@ -537,7 +453,6 @@ static void parse_opts(int argc, char **argv) { static struct option longopts[] = { {"help", no_argument, NULL, 'h'}, - {"foreground", no_argument, NULL, 'f'}, {"verbose", no_argument, NULL, 'v'}, {"user", required_argument, NULL, 'U'}, {"disable-hotplug", no_argument, NULL, 'n'}, @@ -577,9 +492,6 @@ static void parse_opts(int argc, char **argv) case 'h': usage(); exit(0); - case 'f': - foreground = 1; - break; case 'v': ++verbose; break; @@ -602,7 +514,6 @@ static void parse_opts(int argc, char **argv) #ifdef HAVE_SYSTEMD case 's': opt_enable_exit = 1; - foreground = 1; break; #endif case 'n': @@ -675,12 +586,7 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - if (!foreground && !use_logfile) { - verbose += LL_WARNING; - log_enable_syslog(); - } else { - verbose += LL_NOTICE; - } + verbose += LL_WARNING; /* set log level to specified verbosity */ log_level = verbose; @@ -751,14 +657,6 @@ int main(int argc, char *argv[]) goto terminate; } - if (!foreground) { - if ((res = daemonize()) < 0) { - fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n"); - usbmuxd_log(LL_FATAL, "Could not daemonize!"); - goto terminate; - } - } - if (lockfile) { // now open the lockfile and place the lock res = lfd = open(lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);