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>
23 static void daemon_resolve_ugid(struct daemon_params *dp)
26 const char *u = dp->run_as_user;
27 struct passwd *pw = NULL;
33 const char *err = str_to_uns(&id, u, NULL, 10 | STN_WHOLE);
35 die("Cannot parse user `%s': %s", u, err);
43 die("No such user `%s'", u);
44 dp->run_as_uid = pw->pw_uid;
50 const char *g = dp->run_as_group;
57 const char *err = str_to_uns(&id, g, NULL, 10 | STN_WHOLE);
59 die("Cannot parse group `%s': %s", g, err);
67 die("No such group `%s'", g);
68 dp->run_as_gid = gr->gr_gid;
74 dp->run_as_gid = pw->pw_gid;
79 void daemon_init(struct daemon_params *dp)
81 daemon_resolve_ugid(dp);
85 // Check that PID file path is absolute
86 if (!(dp->flags & DAEMON_FLAG_PRESERVE_CWD) && dp->pid_file[0] != '/')
87 die("Path to PID file `%s' must be absolute", dp->pid_file);
90 dp->pid_fd = open(dp->pid_file, O_RDWR | O_CREAT, 0666);
92 die("Cannot open `%s': %m", dp->pid_file);
93 int fl = fcntl(dp->pid_fd, F_GETFD);
94 if (fl < 0 || fcntl(dp->pid_fd, F_SETFD, fl | FD_CLOEXEC))
95 die("Cannot set FD_CLOEXEC: %m");
97 // Try to lock it with an exclusive lock
98 if (flock(dp->pid_fd, LOCK_EX | LOCK_NB) < 0)
100 if (errno == EINTR || errno == EWOULDBLOCK)
101 die("Daemon is already running (`%s' locked)", dp->pid_file);
103 die("Cannot lock `%s': %m", dp->pid_file);
106 // Make a note that the daemon is starting
107 if (write(dp->pid_fd, "(starting)\n", 11) != 11 ||
108 ftruncate(dp->pid_fd, 11) < 0)
109 die("Error writing `%s': %m", dp->pid_file);
113 void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp))
115 // Switch GID and UID
116 if (dp->want_setgid && setresgid(dp->run_as_gid, dp->run_as_gid, dp->run_as_gid) < 0)
117 die("Cannot set GID to %d: %m", (int) dp->run_as_gid);
118 if (dp->want_setgid > 1 && initgroups(dp->run_as_user, dp->run_as_gid) < 0)
119 die("Cannot initialize groups: %m");
120 if (dp->want_setuid && setresuid(dp->run_as_uid, dp->run_as_uid, dp->run_as_uid) < 0)
121 die("Cannot set UID to %d: %m", (int) dp->run_as_uid);
123 // Create a new session and close stdio
126 if (open("/dev/null", O_RDWR, 0) < 0 ||
128 die("Cannot redirect stdio to `/dev/null': %m");
130 // Set umask to a reasonable value
133 // Do not hold the current working directory
134 if (!(dp->flags & DAEMON_FLAG_PRESERVE_CWD))
137 die("Cannot chdir to root: %m");
143 die("Cannot fork: %m");
146 // We still keep the PID file open and thus locked
155 int c = snprintf(buf, sizeof(buf), "%d\n", (int) pid);
156 ASSERT(c <= (int) sizeof(buf));
157 if (lseek(dp->pid_fd, 0, SEEK_SET) < 0 ||
158 write(dp->pid_fd, buf, c) != c ||
159 ftruncate(dp->pid_fd, c) ||
160 close(dp->pid_fd) < 0)
161 die("Cannot write PID to `%s': %m", dp->pid_file);
165 void daemon_exit(struct daemon_params *dp)
169 if (unlink(dp->pid_file) < 0)
170 msg(L_ERROR, "Cannot unlink PID file `%s': %m", dp->pid_file);
177 static void body(struct daemon_params *dp)
180 msg(L_INFO, "Daemon is running");
181 msg(L_INFO, "uid=%d/%d gid=%d/%d", (int) getuid(), (int) geteuid(), (int) getgid(), (int) getegid());
183 msg(L_INFO, "Daemon is shutting down");
187 int main(int argc, char **argv)
189 struct daemon_params dp = {
190 .pid_file = "/tmp/123",
194 while ((opt = getopt(argc, argv, "p:u:g:")) >= 0)
198 dp.pid_file = optarg;
201 dp.run_as_user = optarg;
204 dp.run_as_group = optarg;
207 die("Invalid arguments");
211 daemon_run(&dp, body);
212 msg(L_INFO, "Main program has ended");