2 * UCW Library -- Daemonization
4 * (c) 2012 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/daemon.h>
12 #include <ucw/strtonum.h>
24 daemon_resolve_ugid(struct daemon_params *dp)
27 const char *u = dp->run_as_user;
28 struct passwd *pw = NULL;
34 const char *err = str_to_uns(&id, u, NULL, 10 | STN_WHOLE);
36 die("Cannot parse user `%s': %s", u, err);
44 die("No such user `%s'", u);
45 dp->run_as_uid = pw->pw_uid;
51 const char *g = dp->run_as_group;
58 const char *err = str_to_uns(&id, g, NULL, 10 | STN_WHOLE);
60 die("Cannot parse group `%s': %s", g, err);
68 die("No such group `%s'", g);
69 dp->run_as_gid = gr->gr_gid;
75 dp->run_as_gid = pw->pw_gid;
81 daemon_init(struct daemon_params *dp)
83 daemon_resolve_ugid(dp);
87 // Check that PID file path is absolute
88 if (!(dp->flags & DAEMON_FLAG_PRESERVE_CWD) && dp->pid_file[0] != '/')
89 die("Path to PID file `%s' must be absolute", dp->pid_file);
92 dp->pid_fd = open(dp->pid_file, O_RDWR | O_CREAT, 0666);
94 die("Cannot open `%s': %m", dp->pid_file);
95 int fl = fcntl(dp->pid_fd, F_GETFD);
96 if (fl < 0 || fcntl(dp->pid_fd, F_SETFD, fl | FD_CLOEXEC))
97 die("Cannot set FD_CLOEXEC: %m");
99 // Try to lock it with an exclusive lock
100 if (flock(dp->pid_fd, LOCK_EX | LOCK_NB) < 0)
102 if (errno == EINTR || errno == EWOULDBLOCK)
103 die("Daemon is already running (`%s' locked)", dp->pid_file);
105 die("Cannot lock `%s': %m", dp->pid_file);
108 // Make a note that the daemon is starting
109 if (write(dp->pid_fd, "(starting)\n", 11) != 11 ||
110 ftruncate(dp->pid_fd, 11) < 0)
111 die("Error writing `%s': %m", dp->pid_file);
116 daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp))
118 // Switch GID and UID
119 if (dp->want_setgid && setresgid(dp->run_as_gid, dp->run_as_gid, dp->run_as_gid) < 0)
120 die("Cannot set GID to %d: %m", (int) dp->run_as_gid);
121 if (dp->want_setgid > 1 && initgroups(dp->run_as_user, dp->run_as_gid) < 0)
122 die("Cannot initialize groups: %m");
123 if (dp->want_setuid && setresuid(dp->run_as_uid, dp->run_as_uid, dp->run_as_uid) < 0)
124 die("Cannot set UID to %d: %m", (int) dp->run_as_uid);
126 // Create a new session and close stdio
129 if (open("/dev/null", O_RDWR, 0) < 0 ||
131 die("Cannot redirect stdio to `/dev/null': %m");
133 // Set umask to a reasonable value
136 // Do not hold the current working directory
137 if (!(dp->flags & DAEMON_FLAG_PRESERVE_CWD))
140 die("Cannot chdir to root: %m");
146 die("Cannot fork: %m");
149 // We still keep the PID file open and thus locked
158 int c = snprintf(buf, sizeof(buf), "%d\n", (int) pid);
159 ASSERT(c <= (int) sizeof(buf));
160 if (lseek(dp->pid_fd, 0, SEEK_SET) < 0 ||
161 write(dp->pid_fd, buf, c) != c ||
162 ftruncate(dp->pid_fd, c) ||
163 close(dp->pid_fd) < 0)
164 die("Cannot write PID to `%s': %m", dp->pid_file);
169 daemon_exit(struct daemon_params *dp)
173 if (unlink(dp->pid_file) < 0)
174 msg(L_ERROR, "Cannot unlink PID file `%s': %m", dp->pid_file);
183 static volatile sig_atomic_t terminate;
185 static void term_handler(int sig UNUSED)
187 msg(L_INFO | L_SIGHANDLER, "SIGTERM received, terminating in a while");
191 static void hup_handler(int sig UNUSED)
193 msg(L_INFO | L_SIGHANDLER, "SIGHUP received");
196 static void body(struct daemon_params *dp)
199 msg(L_INFO, "Daemon is running");
200 msg(L_INFO, "uid=%d/%d gid=%d/%d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid());
202 struct sigaction sa_term = { .sa_handler = term_handler };
203 struct sigaction sa_hup = { .sa_handler = hup_handler };
204 if (sigaction(SIGTERM, &sa_term, NULL) < 0 ||
205 sigaction(SIGHUP, &sa_hup, NULL) < 0)
212 msg(L_INFO, "Timeout elapsed, terminating in a while");
218 msg(L_INFO, "Daemon is shutting down");
222 int main(int argc, char **argv)
224 struct daemon_params dp = {
225 .pid_file = "/tmp/123",
229 while ((opt = getopt(argc, argv, "p:u:g:")) >= 0)
233 dp.pid_file = optarg;
236 dp.run_as_user = optarg;
239 dp.run_as_group = optarg;
242 die("Invalid arguments");
246 daemon_run(&dp, body);
247 msg(L_INFO, "Main program has ended");