aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Uhlin <markus@nifty-networks.net>2024-08-03 20:29:56 +0200
committerMarkus Uhlin <markus@nifty-networks.net>2024-08-03 20:29:56 +0200
commit0ca2c0edd46b8f705daed44dc00de3bbee933263 (patch)
tree3e8301222439dd778f41d0014c5eaa522dbee230
parent69c3b0c5b52d75c68b176729e17a7afecffc7c03 (diff)
Run in the background
-rw-r--r--FICS/ficsmain.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/FICS/ficsmain.c b/FICS/ficsmain.c
index 5a52951..dd47740 100644
--- a/FICS/ficsmain.c
+++ b/FICS/ficsmain.c
@@ -71,6 +71,49 @@ BrokenPipe(int sig)
}
PRIVATE void
+daemonize(void)
+{
+ int fd[2];
+ mode_t mode[2];
+
+ mode[0] = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+ mode[1] = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+ if (file_exists(DAEMON_LOCKFILE)) {
+ errx(1, "%s: %s: already exists\ndelete the file manually and "
+ "try again after making sure that no copy of the program "
+ "is already running in the background",
+ __func__,
+ DAEMON_LOCKFILE);
+ } else if ((fd[0] = open(DAEMON_LOCKFILE, (O_CREAT | O_RDWR),
+ mode[0])) == -1)
+ err(1, "%s: open(%s, ...)", __func__, DAEMON_LOCKFILE);
+
+ dprintf(fd[0], "%jd\n", (intmax_t)getpid());
+ close(fd[0]);
+
+ if ((fd[1] = open(DAEMON_LOGFILE, (O_APPEND | O_CREAT | O_RDWR),
+ mode[1])) == -1)
+ err(1, "%s: open(%s, ...)", __func__, DAEMON_LOGFILE);
+ else if (daemon(1, 1) == -1) {
+ int i;
+
+ i = errno;
+ close(fd[1]);
+ errno = i;
+
+ err(1, "%s: failed to run in the background", __func__);
+ }
+
+ (void) dup2(fd[1], STDIN_FILENO);
+ (void) dup2(fd[1], STDOUT_FILENO);
+ (void) dup2(fd[1], STDERR_FILENO);
+
+ if (fd[1] > 2)
+ close(fd[1]);
+}
+
+PRIVATE void
GetArgs(int argc, char *argv[])
{
port = DEFAULT_PORT;
@@ -91,6 +134,9 @@ GetArgs(int argc, char *argv[])
exit(1);
withConsole = 1;
break;
+ case 'd':
+ daemonize();
+ break;
case 'h':
usage(argv[0]);
break;