2 * Copyright (C) Joerg Lenneis 2003
3 * Copyright (C) Frank Lahm 2009, 2010
5 * All Rights Reserved. See COPYING.
9 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
13 1. afpd -------> cnid_metad
15 via UNIX domain socket
16 2. cnid_metad -------> cnid_dbd
19 3. cnid_metad -------> cnid_dbd
23 4. afpd -------> cnid_dbd
25 cnid_metad and cnid_dbd have been converted to non-blocking IO in 2010.
31 #endif /* HAVE_CONFIG_H */
37 #include <sys/param.h>
41 #include <sys/types.h>
47 #include <sys/socket.h>
52 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
53 #endif /* ! WEXITSTATUS */
55 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
56 #endif /* ! WIFEXITED */
58 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
62 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
65 #define WTERMSIG(status) ((status) & 0x7f)
68 /* functions for username and group */
75 #define USE_SETRESUID 1
76 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
77 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
78 #endif /* USE_SETRESUID */
82 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
83 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
84 #endif /* USE_SETEUID */
87 #include <atalk/util.h>
88 #include <atalk/logger.h>
89 #include <atalk/cnid_dbd_private.h>
90 #include <atalk/paths.h>
91 #include <atalk/volinfo.h>
92 #include <atalk/compat.h>
96 #define DBHOME ".AppleDB"
101 static volatile sig_atomic_t sigchild = 0;
103 #define MAXSPAWN 3 /* Max times respawned in.. */
104 #define TESTTIME 42 /* this much seconds apfd client tries to *
105 * to reconnect every 5 secondes, catch it */
107 #define DEFAULTHOST "localhost"
108 #define DEFAULTPORT "4700"
111 struct volinfo *volinfo;
113 time_t tm; /* When respawned last */
114 int count; /* Times respawned in the last TESTTIME secondes */
115 int control_fd; /* file descriptor to child cnid_dbd process */
118 static struct server srv[MAXVOLS];
120 /* Default logging config: log to syslog with level log_note */
121 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
123 static void daemon_exit(int i)
125 server_unlock(_PATH_CNID_METAD_LOCK);
129 /* ------------------ */
130 static void sigterm_handler(int sig)
134 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
137 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
142 static struct server *test_usockfn(struct volinfo *volinfo)
145 for (i = 0; i < MAXVOLS; i++) {
146 if ((srv[i].volinfo) && (strcmp(srv[i].volinfo->v_path, volinfo->v_path) == 0)) {
153 /* -------------------- */
154 static int maybe_start_dbd(char *dbdpn, struct volinfo *volinfo)
163 char *volpath = volinfo->v_path;
165 LOG(log_debug, logtype_cnid, "maybe_start_dbd: Volume: \"%s\"", volpath);
167 up = test_usockfn(volinfo);
169 /* we already have a process, send our fd */
170 if (send_fd(up->control_fd, rqstfd) < 0) {
177 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet");
181 /* find an empty slot */
182 for (i = 0; i < MAXVOLS; i++) {
183 if (srv[i].volinfo == NULL) {
185 up->volinfo = volinfo;
186 retainvolinfo(volinfo);
193 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
197 /* we have a slot but no process, check for respawn too fast */
198 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
200 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
201 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
202 return -1; /* just exit, dont sleep, because we might have work to do for another client */
204 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
205 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
210 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
211 if (up->count > MAXSPAWN) {
212 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
213 we will just return -1 above */
214 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
219 Create socketpair for comm between parent and child.
220 We use it to pass fds from connecting afpd processes to our
221 cnid_dbd child via fd passing.
223 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
224 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
228 if ((pid = fork()) < 0) {
229 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
235 * Child. Close descriptors and start the daemon. If it fails
236 * just log it. The client process will fail connecting
243 for (i = 0; i < MAXVOLS; i++) {
244 if (srv[i].pid && up != &srv[i]) {
245 close(srv[i].control_fd);
249 sprintf(buf1, "%i", sv[1]);
250 sprintf(buf2, "%i", rqstfd);
252 if (up->count == MAXSPAWN) {
253 /* there's a pb with the db inform child
254 * it will run recover, delete the db whatever
256 LOG(log_error, logtype_cnid, "try with -d %s", up->volinfo->v_path);
257 ret = execlp(dbdpn, dbdpn, "-d", volpath, buf1, buf2, logconfig, NULL);
260 ret = execlp(dbdpn, dbdpn, volpath, buf1, buf2, logconfig, NULL);
262 /* Yikes! We're still here, so exec failed... */
263 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
271 up->control_fd = sv[0];
275 /* ------------------ */
276 static int set_dbdir(char *dbdir)
283 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
284 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
288 if (dbdir[len - 1] != '/') {
292 strcpy(dbdir + len, DBHOME);
293 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
294 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
300 /* ------------------ */
301 static uid_t user_to_uid (char *username)
303 struct passwd *this_passwd;
305 /* check for anything */
306 if ( !username || strlen ( username ) < 1 ) return 0;
308 /* grab the /etc/passwd record relating to username */
309 this_passwd = getpwnam ( username );
311 /* return false if there is no structure returned */
312 if (this_passwd == NULL) return 0;
314 /* return proper uid */
315 return this_passwd->pw_uid;
319 /* ------------------ */
320 static gid_t group_to_gid ( char *group)
322 struct group *this_group;
324 /* check for anything */
325 if ( !group || strlen ( group ) < 1 ) return 0;
327 /* grab the /etc/groups record relating to group */
328 this_group = getgrnam ( group );
330 /* return false if there is no structure returned */
331 if (this_group == NULL) return 0;
333 /* return proper gid */
334 return this_group->gr_gid;
338 /* ------------------ */
339 static void catch_child(int sig _U_)
344 /* ----------------------- */
345 static void set_signal(void)
350 memset(&sv, 0, sizeof(sv));
353 sv.sa_handler = catch_child;
354 sv.sa_flags = SA_NOCLDSTOP;
355 sigemptyset(&sv.sa_mask);
356 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
357 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
358 daemon_exit(EXITERR_SYS);
362 sv.sa_handler = sigterm_handler;
363 sigfillset(&sv.sa_mask );
364 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
365 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
366 daemon_exit(EXITERR_SYS);
369 /* Ignore the rest */
370 sv.sa_handler = SIG_IGN;
371 sigemptyset(&sv.sa_mask );
372 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
373 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
374 daemon_exit(EXITERR_SYS);
376 sv.sa_handler = SIG_IGN;
377 sigemptyset(&sv.sa_mask );
378 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
379 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
380 daemon_exit(EXITERR_SYS);
382 sv.sa_handler = SIG_IGN;
383 sigemptyset(&sv.sa_mask );
384 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
385 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
386 daemon_exit(EXITERR_SYS);
388 sv.sa_handler = SIG_IGN;
389 sigemptyset(&sv.sa_mask );
390 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
391 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
392 daemon_exit(EXITERR_SYS);
394 sv.sa_handler = SIG_IGN;
395 sigemptyset(&sv.sa_mask );
396 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
397 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
398 daemon_exit(EXITERR_SYS);
401 /* block everywhere but in pselect */
403 sigaddset(&set, SIGCHLD);
404 sigprocmask(SIG_BLOCK, &set, NULL);
407 /* ------------------ */
408 int main(int argc, char *argv[])
410 char volpath[MAXPATHLEN + 1];
414 char *dbdpn = _PATH_CNID_DBD;
415 char *host = DEFAULTHOST;
416 char *port = DEFAULTPORT;
424 char *loglevel = NULL;
425 char *logfile = NULL;
427 struct volinfo *volinfo;
429 set_processname("cnid_metad");
431 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
437 host = strdup(optarg);
440 uid = user_to_uid (optarg);
442 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
447 gid =group_to_gid (optarg);
449 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
454 port = strdup(optarg);
457 dbdpn = strdup(optarg);
460 loglevel = strdup(optarg);
463 logfile = strdup(optarg);
472 strlcpy(logconfig + 8, loglevel, 13);
474 strcat(logconfig, " ");
477 strlcat(logconfig, logfile, MAXPATHLEN);
483 LOG(log_error, logtype_cnid, "main: bad arguments");
487 /* Check PID lockfile and become a daemon */
488 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, debug)) {
490 daemon_exit(EXITERR_SYS);
493 default: /* server */
497 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
502 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
504 if (SWITCH_TO_GID(gid) < 0) {
505 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
510 if (SWITCH_TO_UID(uid) < 0) {
511 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
520 sigprocmask(SIG_SETMASK, NULL, &set);
521 sigdelset(&set, SIGCHLD);
524 rqstfd = usockfd_check(srvfd, &set);
525 /* Collect zombie processes and log what happened to them */
526 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
527 for (i = 0; i < MAXVOLS; i++) {
528 if (srv[i].pid == pid) {
530 close(srv[i].control_fd);
534 if (WIFEXITED(status)) {
535 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
536 pid, WEXITSTATUS(status));
538 else if (WIFSIGNALED(status)) {
539 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
540 pid, WTERMSIG(status));
547 ret = readt(rqstfd, &len, sizeof(int), 1, 4);
554 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
557 else if (ret != sizeof(int)) {
558 LOG(log_error, logtype_cnid, "short read: got %d", ret);
562 * checks for buffer overruns. The client libatalk side does it too
563 * before handing the dir path over but who trusts clients?
565 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
566 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
570 actual_len = readt(rqstfd, volpath, len, 1, 5);
571 if (actual_len < 0) {
572 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
575 if (actual_len != len) {
576 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
581 /* Load .volinfo file */
582 if ((volinfo = allocvolinfo(volpath)) == NULL) {
583 LOG(log_severe, logtype_cnid, "allocvolinfo(\"%s\"): %s",
584 volpath, strerror(errno));
588 if (set_dbdir(volinfo->v_dbpath) < 0) {
592 maybe_start_dbd(dbdpn, volinfo);
594 (void)closevolinfo(volinfo);