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>
43 #include <sys/resource.h>
48 #include <sys/socket.h>
53 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
54 #endif /* ! WEXITSTATUS */
56 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
57 #endif /* ! WIFEXITED */
59 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
63 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
66 #define WTERMSIG(status) ((status) & 0x7f)
69 /* functions for username and group */
76 #define USE_SETRESUID 1
77 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
78 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
79 #endif /* USE_SETRESUID */
83 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
84 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
85 #endif /* USE_SETEUID */
88 #include <atalk/util.h>
89 #include <atalk/logger.h>
90 #include <atalk/cnid_dbd_private.h>
91 #include <atalk/paths.h>
92 #include <atalk/volinfo.h>
96 #define DBHOME ".AppleDB"
101 static volatile sig_atomic_t sigchild = 0;
104 #define MAXSPAWN 3 /* Max times respawned in.. */
105 #define TESTTIME 42 /* this much seconds apfd client tries to *
106 * to reconnect every 5 secondes, catch it */
108 #define DEFAULTHOST "localhost"
109 #define DEFAULTPORT "4700"
112 struct volinfo *volinfo;
114 time_t tm; /* When respawned last */
115 int count; /* Times respawned in the last TESTTIME secondes */
116 int control_fd; /* file descriptor to child cnid_dbd process */
119 static struct server srv[MAXVOLS];
121 /* Default logging config: log to syslog with level log_note */
122 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
124 static void daemon_exit(int i)
126 server_unlock(_PATH_CNID_METAD_LOCK);
130 /* ------------------ */
131 static void sigterm_handler(int sig)
135 LOG(log_info, logtype_afpd, "shutting down on signal %d", sig );
138 LOG(log_error, logtype_afpd, "unexpected signal: %d", sig);
143 static struct server *test_usockfn(struct volinfo *volinfo)
146 for (i = 0; i < maxvol; i++) {
147 if ((srv[i].volinfo) && (strcmp(srv[i].volinfo->v_path, volinfo->v_path) == 0)) {
154 /* -------------------- */
155 static int maybe_start_dbd(char *dbdpn, struct volinfo *volinfo)
164 char *volpath = volinfo->v_path;
166 LOG(log_debug, logtype_cnid, "maybe_start_dbd: Volume: \"%s\"", volpath);
168 up = test_usockfn(volinfo);
170 /* we already have a process, send our fd */
171 if (send_fd(up->control_fd, rqstfd) < 0) {
178 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet");
182 /* find an empty slot (i < maxvol) or the first free slot (i == maxvol)*/
183 for (i = 0; i <= maxvol; i++) {
184 if (srv[i].volinfo == NULL && i < MAXVOLS) {
186 up->volinfo = volinfo;
187 retainvolinfo(volinfo);
196 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
200 /* we have a slot but no process, check for respawn too fast */
201 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
203 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
204 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
205 return -1; /* just exit, dont sleep, because we might have work to do for another client */
207 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
208 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
213 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
214 if (up->count > MAXSPAWN) {
215 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
216 we will just return -1 above */
217 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
222 Create socketpair for comm between parent and child.
223 We use it to pass fds from connecting afpd processes to our
224 cnid_dbd child via fd passing.
226 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
227 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
231 if ((pid = fork()) < 0) {
232 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
238 * Child. Close descriptors and start the daemon. If it fails
239 * just log it. The client process will fail connecting
246 for (i = 0; i < MAXVOLS; i++) {
247 if (srv[i].pid && up != &srv[i]) {
248 close(srv[i].control_fd);
252 sprintf(buf1, "%i", sv[1]);
253 sprintf(buf2, "%i", rqstfd);
255 if (up->count == MAXSPAWN) {
256 /* there's a pb with the db inform child, it will delete the db */
257 LOG(log_warning, logtype_cnid,
258 "Multiple attempts to start CNID db daemon for \"%s\" failed, wiping the slate clean...",
259 up->volinfo->v_path);
260 ret = execlp(dbdpn, dbdpn, "-d", volpath, buf1, buf2, logconfig, NULL);
262 ret = execlp(dbdpn, dbdpn, volpath, buf1, buf2, logconfig, NULL);
264 /* Yikes! We're still here, so exec failed... */
265 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
273 up->control_fd = sv[0];
277 /* ------------------ */
278 static int set_dbdir(char *dbdir)
285 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
286 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
290 if (dbdir[len - 1] != '/') {
294 strcpy(dbdir + len, DBHOME);
295 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
296 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
302 /* ------------------ */
303 static uid_t user_to_uid (char *username)
305 struct passwd *this_passwd;
307 /* check for anything */
308 if ( !username || strlen ( username ) < 1 ) return 0;
310 /* grab the /etc/passwd record relating to username */
311 this_passwd = getpwnam ( username );
313 /* return false if there is no structure returned */
314 if (this_passwd == NULL) return 0;
316 /* return proper uid */
317 return this_passwd->pw_uid;
321 /* ------------------ */
322 static gid_t group_to_gid ( char *group)
324 struct group *this_group;
326 /* check for anything */
327 if ( !group || strlen ( group ) < 1 ) return 0;
329 /* grab the /etc/groups record relating to group */
330 this_group = getgrnam ( group );
332 /* return false if there is no structure returned */
333 if (this_group == NULL) return 0;
335 /* return proper gid */
336 return this_group->gr_gid;
340 /* ------------------ */
341 static void catch_child(int sig _U_)
346 /* ----------------------- */
347 static void set_signal(void)
352 memset(&sv, 0, sizeof(sv));
355 sv.sa_handler = catch_child;
356 sv.sa_flags = SA_NOCLDSTOP;
357 sigemptyset(&sv.sa_mask);
358 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
359 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
360 daemon_exit(EXITERR_SYS);
364 sv.sa_handler = sigterm_handler;
365 sigfillset(&sv.sa_mask );
366 if (sigaction(SIGTERM, &sv, NULL ) < 0 ) {
367 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
368 daemon_exit(EXITERR_SYS);
371 /* Ignore the rest */
372 sv.sa_handler = SIG_IGN;
373 sigemptyset(&sv.sa_mask );
374 if (sigaction(SIGALRM, &sv, NULL ) < 0 ) {
375 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
376 daemon_exit(EXITERR_SYS);
378 sv.sa_handler = SIG_IGN;
379 sigemptyset(&sv.sa_mask );
380 if (sigaction(SIGHUP, &sv, NULL ) < 0 ) {
381 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
382 daemon_exit(EXITERR_SYS);
384 sv.sa_handler = SIG_IGN;
385 sigemptyset(&sv.sa_mask );
386 if (sigaction(SIGUSR1, &sv, NULL ) < 0 ) {
387 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
388 daemon_exit(EXITERR_SYS);
390 sv.sa_handler = SIG_IGN;
391 sigemptyset(&sv.sa_mask );
392 if (sigaction(SIGUSR2, &sv, NULL ) < 0 ) {
393 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
394 daemon_exit(EXITERR_SYS);
396 sv.sa_handler = SIG_IGN;
397 sigemptyset(&sv.sa_mask );
398 if (sigaction(SIGPIPE, &sv, NULL ) < 0 ) {
399 LOG(log_error, logtype_afpd, "sigaction: %s", strerror(errno) );
400 daemon_exit(EXITERR_SYS);
403 /* block everywhere but in pselect */
405 sigaddset(&set, SIGCHLD);
406 sigprocmask(SIG_BLOCK, &set, NULL);
409 static int setlimits(void)
413 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
414 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
417 if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < 65535) {
418 rlim.rlim_cur = 65535;
419 if (rlim.rlim_max != RLIM_INFINITY && rlim.rlim_max < 65535)
420 rlim.rlim_max = 65535;
421 if (setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
422 LOG(log_error, logtype_afpd, "setlimits: %s", strerror(errno));
429 /* ------------------ */
430 int main(int argc, char *argv[])
432 char volpath[MAXPATHLEN + 1];
436 char *dbdpn = _PATH_CNID_DBD;
437 char *host = DEFAULTHOST;
438 char *port = DEFAULTPORT;
446 char *loglevel = NULL;
447 char *logfile = NULL;
449 struct volinfo *volinfo;
451 set_processname("cnid_metad");
453 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
459 host = strdup(optarg);
462 uid = user_to_uid (optarg);
464 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
469 gid =group_to_gid (optarg);
471 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
476 port = strdup(optarg);
479 dbdpn = strdup(optarg);
482 loglevel = strdup(optarg);
485 logfile = strdup(optarg);
494 strlcpy(logconfig + 8, loglevel, 13);
496 strcat(logconfig, " ");
499 strlcat(logconfig, logfile, MAXPATHLEN);
505 LOG(log_error, logtype_cnid, "main: bad arguments");
511 /* Check PID lockfile and become a daemon */
512 switch(server_lock("cnid_metad", _PATH_CNID_METAD_LOCK, debug)) {
514 daemon_exit(EXITERR_SYS);
517 default: /* server */
521 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
526 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
528 if (SWITCH_TO_GID(gid) < 0) {
529 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
534 if (SWITCH_TO_UID(uid) < 0) {
535 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
544 sigprocmask(SIG_SETMASK, NULL, &set);
545 sigdelset(&set, SIGCHLD);
548 rqstfd = usockfd_check(srvfd, &set);
549 /* Collect zombie processes and log what happened to them */
550 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
551 for (i = 0; i < maxvol; i++) {
552 if (srv[i].pid == pid) {
554 close(srv[i].control_fd);
558 if (WIFEXITED(status)) {
559 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
560 pid, WEXITSTATUS(status));
562 else if (WIFSIGNALED(status)) {
563 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
564 pid, WTERMSIG(status));
571 ret = readt(rqstfd, &len, sizeof(int), 1, 4);
578 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
581 else if (ret != sizeof(int)) {
582 LOG(log_error, logtype_cnid, "short read: got %d", ret);
586 * checks for buffer overruns. The client libatalk side does it too
587 * before handing the dir path over but who trusts clients?
589 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
590 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
594 actual_len = readt(rqstfd, volpath, len, 1, 5);
595 if (actual_len < 0) {
596 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
599 if (actual_len != len) {
600 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
605 /* Load .volinfo file */
606 if ((volinfo = allocvolinfo(volpath)) == NULL) {
607 LOG(log_severe, logtype_cnid, "allocvolinfo(\"%s\"): %s",
608 volpath, strerror(errno));
612 if (set_dbdir(volinfo->v_dbpath) < 0) {
616 maybe_start_dbd(dbdpn, volinfo);
618 (void)closevolinfo(volinfo);