2 * $Id: cnid_metad.c,v 1.22 2009-11-16 02:04:47 didg Exp $
4 * Copyright (C) Joerg Lenneis 2003
5 * All Rights Reserved. See COPYING.
10 cnid_dbd metadaemon to start up cnid_dbd upon request from afpd.
14 1. afpd -------> cnid_metad
16 via UNIX domain socket
17 2. cnid_metad -------> cnid_dbd
20 3. cnid_metad -------> cnid_dbd
24 4. afpd -------> cnid_dbd
30 #endif /* HAVE_CONFIG_H */
36 #include <sys/param.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_TIME_H
46 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/socket.h>
57 #include <sys/ioctl.h>
60 #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
61 #endif /* ! WEXITSTATUS */
63 #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
64 #endif /* ! WIFEXITED */
66 #define WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
70 #define WIFSIGNALED(status) (!WIFSTOPPED(status) && !WIFEXITED(status))
73 #define WTERMSIG(status) ((status) & 0x7f)
76 /* functions for username and group */
83 #define USE_SETRESUID 1
84 #define SWITCH_TO_GID(gid) ((setresgid(gid,gid,gid) < 0 || setgid(gid) < 0) ? -1 : 0)
85 #define SWITCH_TO_UID(uid) ((setresuid(uid,uid,uid) < 0 || setuid(uid) < 0) ? -1 : 0)
86 #endif /* USE_SETRESUID */
90 #define SWITCH_TO_GID(gid) ((setegid(gid) < 0 || setgid(gid) < 0) ? -1 : 0)
91 #define SWITCH_TO_UID(uid) ((setuid(uid) < 0 || seteuid(uid) < 0 || setuid(uid) < 0) ? -1 : 0)
92 #endif /* USE_SETEUID */
95 #include <atalk/util.h>
96 #include <atalk/logger.h>
97 #include <atalk/cnid_dbd_private.h>
98 #include <atalk/paths.h>
100 #include "db_param.h"
103 #define DBHOME ".AppleDB"
108 static volatile sig_atomic_t sigchild = 0;
110 #define MAXSPAWN 3 /* Max times respawned in.. */
111 #define TESTTIME 42 /* this much seconds apfd client tries to *
112 * to reconnect every 5 secondes, catch it */
114 #define DEFAULTHOST "localhost"
115 #define DEFAULTPORT "4700"
120 time_t tm; /* When respawned last */
121 int count; /* Times respawned in the last TESTTIME secondes */
122 int control_fd; /* file descriptor to child cnid_dbd process */
125 static struct server srv[MAXVOLS];
127 /* Default logging config: log to syslog with level log_note */
128 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
130 static struct server *test_usockfn(char *dir)
133 for (i = 0; i < MAXVOLS; i++) {
134 if (srv[i].name && !strcmp(srv[i].name, dir)) {
141 /* -------------------- */
142 static int send_cred(int socket, int fd)
147 struct cmsghdr *cmsgp = NULL;
152 size = CMSG_SPACE(sizeof fd);
155 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
159 memset(&msgh,0,sizeof (msgh));
162 msgh.msg_name = NULL;
163 msgh.msg_namelen = 0;
168 iov[0].iov_base = &er;
169 iov[0].iov_len = sizeof(er);
171 msgh.msg_control = buf;
172 msgh.msg_controllen = size;
174 cmsgp = CMSG_FIRSTHDR(&msgh);
175 cmsgp->cmsg_level = SOL_SOCKET;
176 cmsgp->cmsg_type = SCM_RIGHTS;
177 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
179 *((int *)CMSG_DATA(cmsgp)) = fd;
180 msgh.msg_controllen = cmsgp->cmsg_len;
183 ret = sendmsg(socket,&msgh, 0);
184 } while ( ret == -1 && errno == EINTR );
186 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
194 /* -------------------- */
195 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
205 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
208 up = test_usockfn(dbdir);
210 /* we already have a process, send our fd */
211 if (send_cred(up->control_fd, rqstfd) < 0) {
218 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
222 /* find an empty slot */
223 for (i = 0; i < MAXVOLS; i++) {
224 if ( !srv[i].name ) {
228 up->name = strdup(dbdir);
233 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
238 /* we have a slot but no process, check for respawn too fast */
239 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
241 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
242 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
243 return -1; /* just exit, dont sleep, because we might have work to do for another client */
245 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
246 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
251 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
252 if (up->count > MAXSPAWN) {
253 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
254 we will just return -1 above */
255 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
260 Create socketpair for comm between parent and child.
261 We use it to pass fds from connecting afpd processes to our
262 cnid_dbd child via fd passing.
264 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
265 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
269 if ((pid = fork()) < 0) {
270 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
276 * Child. Close descriptors and start the daemon. If it fails
277 * just log it. The client process will fail connecting
284 for (i = 0; i < MAXVOLS; i++) {
285 if (srv[i].pid && up != &srv[i]) {
286 close(srv[i].control_fd);
290 sprintf(buf1, "%i", sv[1]);
291 sprintf(buf2, "%i", rqstfd);
293 if (up->count == MAXSPAWN) {
294 /* there's a pb with the db inform child
295 * it will run recover, delete the db whatever
297 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
298 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
301 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
304 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
313 up->control_fd = sv[0];
317 /* ------------------ */
318 static int set_dbdir(char *dbdir, int len)
325 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
326 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
330 if (dbdir[len - 1] != '/') {
334 strcpy(dbdir + len, DBHOME);
335 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
336 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
342 /* ------------------ */
343 static uid_t user_to_uid (char *username)
345 struct passwd *this_passwd;
347 /* check for anything */
348 if ( !username || strlen ( username ) < 1 ) return 0;
350 /* grab the /etc/passwd record relating to username */
351 this_passwd = getpwnam ( username );
353 /* return false if there is no structure returned */
354 if (this_passwd == NULL) return 0;
356 /* return proper uid */
357 return this_passwd->pw_uid;
361 /* ------------------ */
362 static gid_t group_to_gid ( char *group)
364 struct group *this_group;
366 /* check for anything */
367 if ( !group || strlen ( group ) < 1 ) return 0;
369 /* grab the /etc/groups record relating to group */
370 this_group = getgrnam ( group );
372 /* return false if there is no structure returned */
373 if (this_group == NULL) return 0;
375 /* return proper gid */
376 return this_group->gr_gid;
380 /* ------------------ */
381 static void catch_child(int sig _U_)
386 /* ----------------------- */
387 static void set_signal(void)
392 signal(SIGPIPE, SIG_IGN);
394 sv.sa_handler = catch_child;
395 sv.sa_flags = SA_NOCLDSTOP;
396 sigemptyset(&sv.sa_mask);
397 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
398 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
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 dbdir[MAXPATHLEN + 1];
414 char *dbdpn = _PATH_CNID_DBD;
415 char *host = DEFAULTHOST;
416 char *port = DEFAULTPORT;
417 struct db_param *dbp;
425 char *loglevel = NULL;
426 char *logfile = NULL;
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, 0)) {
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 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
548 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
549 ret = read(rqstfd, &len, sizeof(int));
555 LOG(log_severe, logtype_cnid, "error read: %s", strerror(errno));
558 else if (ret != sizeof(int)) {
559 LOG(log_error, logtype_cnid, "short read: got %d", ret);
563 * checks for buffer overruns. The client libatalk side does it too
564 * before handing the dir path over but who trusts clients?
566 if (!len || len +DBHOMELEN +2 > MAXPATHLEN) {
567 LOG(log_error, logtype_cnid, "wrong len parameter: %d", len);
571 actual_len = read(rqstfd, dbdir, len);
572 if (actual_len < 0) {
573 LOG(log_severe, logtype_cnid, "Read(2) error : %s", strerror(errno));
576 if (actual_len != len) {
577 LOG(log_error, logtype_cnid, "error/short read (dir): %s", strerror(errno));
582 if (set_dbdir(dbdir, len) < 0) {
586 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
587 LOG(log_error, logtype_cnid, "Error reading config file");
590 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);