Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop --foreground #234

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions docs/usbmuxd.8
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
104 changes: 1 addition & 103 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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;
Expand All @@ -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':
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down