From: Tomohiro Kusumi Date: Thu, 12 Sep 2019 19:26:41 +0000 (+0900) Subject: sbin/hammer2: Move hammer2_demon() to cmd_service.c X-Git-Tag: v5.8.0rc1~916 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/ff05496f5c8926c97edf2a5d56055f1f7794e479 sbin/hammer2: Move hammer2_demon() to cmd_service.c Remove dependencies (global variables and pthread) from subs.c to make it usable from newfs_hammer2(8) which defines several duplicated functions. --- diff --git a/sbin/hammer2/cmd_service.c b/sbin/hammer2/cmd_service.c index 8c75dfebbe..ea3241bbab 100644 --- a/sbin/hammer2/cmd_service.c +++ b/sbin/hammer2/cmd_service.c @@ -995,3 +995,80 @@ xdisk_connect(void) return; } } + +/* + * Execute the specified function as a detached independent process/daemon, + * unless we are in debug mode. If we are in debug mode the function is + * executed as a pthread in the current process. + */ +void +hammer2_demon(void *(*func)(void *), void *arg) +{ + pthread_t thread; + pid_t pid; + int ttyfd; + + /* + * Do not disconnect in debug mode + */ + if (DebugOpt) { + pthread_create(&thread, NULL, func, arg); + NormalExit = 0; + return; + } + + /* + * Otherwise disconnect us. Double-fork to get rid of the ppid + * association and disconnect the TTY. + */ + if ((pid = fork()) < 0) { + fprintf(stderr, "hammer2: fork(): %s\n", strerror(errno)); + exit(1); + } + if (pid > 0) { + while (waitpid(pid, NULL, 0) != pid) + ; + return; /* parent returns */ + } + + /* + * Get rid of the TTY/session before double-forking to finish off + * the ppid. + */ + ttyfd = open("/dev/null", O_RDWR); + if (ttyfd >= 0) { + if (ttyfd != 0) + dup2(ttyfd, 0); + if (ttyfd != 1) + dup2(ttyfd, 1); + if (ttyfd != 2) + dup2(ttyfd, 2); + if (ttyfd > 2) + close(ttyfd); + } + + ttyfd = open("/dev/tty", O_RDWR); + if (ttyfd >= 0) { + ioctl(ttyfd, TIOCNOTTY, 0); + close(ttyfd); + } + setsid(); + + /* + * Second fork to disconnect ppid (the original parent waits for + * us to exit). + */ + if ((pid = fork()) < 0) { + _exit(2); + } + if (pid > 0) + _exit(0); + + /* + * The double child + */ + setsid(); + pthread_create(&thread, NULL, func, arg); + pthread_exit(NULL); + _exit(2); /* NOT REACHED */ +} diff --git a/sbin/hammer2/subs.c b/sbin/hammer2/subs.c index a0d7e2dffc..1df7108b50 100644 --- a/sbin/hammer2/subs.c +++ b/sbin/hammer2/subs.c @@ -62,83 +62,6 @@ hammer2_ioctl_handle(const char *sel_path) return (fd); } -/* - * Execute the specified function as a detached independent process/daemon, - * unless we are in debug mode. If we are in debug mode the function is - * executed as a pthread in the current process. - */ -void -hammer2_demon(void *(*func)(void *), void *arg) -{ - pthread_t thread; - pid_t pid; - int ttyfd; - - /* - * Do not disconnect in debug mode - */ - if (DebugOpt) { - pthread_create(&thread, NULL, func, arg); - NormalExit = 0; - return; - } - - /* - * Otherwise disconnect us. Double-fork to get rid of the ppid - * association and disconnect the TTY. - */ - if ((pid = fork()) < 0) { - fprintf(stderr, "hammer2: fork(): %s\n", strerror(errno)); - exit(1); - } - if (pid > 0) { - while (waitpid(pid, NULL, 0) != pid) - ; - return; /* parent returns */ - } - - /* - * Get rid of the TTY/session before double-forking to finish off - * the ppid. - */ - ttyfd = open("/dev/null", O_RDWR); - if (ttyfd >= 0) { - if (ttyfd != 0) - dup2(ttyfd, 0); - if (ttyfd != 1) - dup2(ttyfd, 1); - if (ttyfd != 2) - dup2(ttyfd, 2); - if (ttyfd > 2) - close(ttyfd); - } - - ttyfd = open("/dev/tty", O_RDWR); - if (ttyfd >= 0) { - ioctl(ttyfd, TIOCNOTTY, 0); - close(ttyfd); - } - setsid(); - - /* - * Second fork to disconnect ppid (the original parent waits for - * us to exit). - */ - if ((pid = fork()) < 0) { - _exit(2); - } - if (pid > 0) - _exit(0); - - /* - * The double child - */ - setsid(); - pthread_create(&thread, NULL, func, arg); - pthread_exit(NULL); - _exit(2); /* NOT REACHED */ -} - const char * hammer2_time64_to_str(uint64_t htime64, char **strp) {