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>
102 #define DBHOME ".AppleDB"
107 static volatile sig_atomic_t sigchild = 0;
109 #define MAXSPAWN 3 /* Max times respawned in.. */
110 #define TESTTIME 42 /* this much seconds apfd client tries to *
111 * to reconnect every 5 secondes, catch it */
113 #define DEFAULTHOST "localhost"
114 #define DEFAULTPORT "4700"
119 time_t tm; /* When respawned last */
120 int count; /* Times respawned in the last TESTTIME secondes */
121 int control_fd; /* file descriptor to child cnid_dbd process */
124 static struct server srv[MAXVOLS];
126 /* Default logging config: log to syslog with level log_note */
127 static char logconfig[MAXPATHLEN + 21 + 1] = "default log_note";
129 static struct server *test_usockfn(char *dir)
132 for (i = 0; i < MAXVOLS; i++) {
133 if (srv[i].name && !strcmp(srv[i].name, dir)) {
140 /* -------------------- */
141 static int send_cred(int socket, int fd)
146 struct cmsghdr *cmsgp = NULL;
151 size = CMSG_SPACE(sizeof fd);
154 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
158 memset(&msgh,0,sizeof (msgh));
161 msgh.msg_name = NULL;
162 msgh.msg_namelen = 0;
167 iov[0].iov_base = &er;
168 iov[0].iov_len = sizeof(er);
170 msgh.msg_control = buf;
171 msgh.msg_controllen = size;
173 cmsgp = CMSG_FIRSTHDR(&msgh);
174 cmsgp->cmsg_level = SOL_SOCKET;
175 cmsgp->cmsg_type = SCM_RIGHTS;
176 cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
178 *((int *)CMSG_DATA(cmsgp)) = fd;
179 msgh.msg_controllen = cmsgp->cmsg_len;
182 ret = sendmsg(socket,&msgh, 0);
183 } while ( ret == -1 && errno == EINTR );
185 LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
193 /* -------------------- */
194 static int maybe_start_dbd(char *dbdpn, char *dbdir, char *usockfn)
204 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: dbdir: '%s', UNIX socket file: '%s'",
207 up = test_usockfn(dbdir);
209 /* we already have a process, send our fd */
210 if (send_cred(up->control_fd, rqstfd) < 0) {
217 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: no cnid_dbd for that volume yet. Starting one ...");
221 /* find an empty slot */
222 for (i = 0; i < MAXVOLS; i++) {
223 if ( !srv[i].name ) {
227 up->name = strdup(dbdir);
232 LOG(log_error, logtype_cnid, "no free slot for cnid_dbd child. Configured maximum: %d. Do you have so many volumes?", MAXVOLS);
237 /* we have a slot but no process, check for respawn too fast */
238 if ( (t < (up->tm + TESTTIME)) /* We're in the respawn time window */
240 (up->count > MAXSPAWN) ) { /* ...and already tried to fork too often */
241 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn too fast just exiting");
242 return -1; /* just exit, dont sleep, because we might have work to do for another client */
244 if ( t >= (up->tm + TESTTIME) ) { /* out of respawn too fast windows reset the count */
245 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn window ended");
250 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: respawn count now is: %u", up->count);
251 if (up->count > MAXSPAWN) {
252 /* We spawned too fast. From now until the first time we tried + TESTTIME seconds
253 we will just return -1 above */
254 LOG(log_maxdebug, logtype_cnid, "maybe_start_dbd: reached MAXSPAWN threshhold");
259 Create socketpair for comm between parent and child.
260 We use it to pass fds from connecting afpd processes to our
261 cnid_dbd child via fd passing.
263 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
264 LOG(log_error, logtype_cnid, "error in socketpair: %s", strerror(errno));
268 if ((pid = fork()) < 0) {
269 LOG(log_error, logtype_cnid, "error in fork: %s", strerror(errno));
275 * Child. Close descriptors and start the daemon. If it fails
276 * just log it. The client process will fail connecting
283 for (i = 0; i < MAXVOLS; i++) {
284 if (srv[i].pid && up != &srv[i]) {
285 close(srv[i].control_fd);
289 sprintf(buf1, "%i", sv[1]);
290 sprintf(buf2, "%i", rqstfd);
292 if (up->count == MAXSPAWN) {
293 /* there's a pb with the db inform child
294 * it will run recover, delete the db whatever
296 LOG(log_error, logtype_cnid, "try with -d %s", up->name);
297 ret = execlp(dbdpn, dbdpn, "-d", dbdir, buf1, buf2, logconfig, NULL);
300 ret = execlp(dbdpn, dbdpn, dbdir, buf1, buf2, logconfig, NULL);
303 LOG(log_error, logtype_cnid, "Fatal error in exec: %s", strerror(errno));
312 up->control_fd = sv[0];
316 /* ------------------ */
317 static int set_dbdir(char *dbdir, int len)
324 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755) < 0) {
325 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
329 if (dbdir[len - 1] != '/') {
333 strcpy(dbdir + len, DBHOME);
334 if (stat(dbdir, &st) < 0 && mkdir(dbdir, 0755 ) < 0) {
335 LOG(log_error, logtype_cnid, "set_dbdir: mkdir failed for %s", dbdir);
341 /* ------------------ */
342 static uid_t user_to_uid (char *username)
344 struct passwd *this_passwd;
346 /* check for anything */
347 if ( !username || strlen ( username ) < 1 ) return 0;
349 /* grab the /etc/passwd record relating to username */
350 this_passwd = getpwnam ( username );
352 /* return false if there is no structure returned */
353 if (this_passwd == NULL) return 0;
355 /* return proper uid */
356 return this_passwd->pw_uid;
360 /* ------------------ */
361 static gid_t group_to_gid ( char *group)
363 struct group *this_group;
365 /* check for anything */
366 if ( !group || strlen ( group ) < 1 ) return 0;
368 /* grab the /etc/groups record relating to group */
369 this_group = getgrnam ( group );
371 /* return false if there is no structure returned */
372 if (this_group == NULL) return 0;
374 /* return proper gid */
375 return this_group->gr_gid;
379 /* ------------------ */
380 static void catch_child(int sig _U_)
385 /* ----------------------- */
386 static void set_signal(void)
391 signal(SIGPIPE, SIG_IGN);
393 sv.sa_handler = catch_child;
394 sv.sa_flags = SA_NOCLDSTOP;
395 sigemptyset(&sv.sa_mask);
396 if (sigaction(SIGCHLD, &sv, NULL) < 0) {
397 LOG(log_error, logtype_cnid, "cnid_metad: sigaction: %s", strerror(errno));
400 /* block everywhere but in pselect */
402 sigaddset(&set, SIGCHLD);
403 sigprocmask(SIG_BLOCK, &set, NULL);
406 /* ------------------ */
407 int main(int argc, char *argv[])
409 char dbdir[MAXPATHLEN + 1];
413 char *dbdpn = _PATH_CNID_DBD;
414 char *host = DEFAULTHOST;
415 char *port = DEFAULTPORT;
416 struct db_param *dbp;
424 char *loglevel = NULL;
425 char *logfile = NULL;
428 set_processname("cnid_metad");
430 while (( cc = getopt( argc, argv, "ds:p:h:u:g:l:f:")) != -1 ) {
436 host = strdup(optarg);
439 uid = user_to_uid (optarg);
441 LOG(log_error, logtype_cnid, "main: bad user %s", optarg);
446 gid =group_to_gid (optarg);
448 LOG(log_error, logtype_cnid, "main: bad group %s", optarg);
453 port = strdup(optarg);
456 dbdpn = strdup(optarg);
459 loglevel = strdup(optarg);
462 logfile = strdup(optarg);
471 strlcpy(logconfig + 8, loglevel, 13);
473 strcat(logconfig, " ");
476 strlcat(logconfig, logfile, MAXPATHLEN);
482 LOG(log_error, logtype_cnid, "main: bad arguments");
486 /* Check PID lockfile and become a daemon */
487 switch(server_lock("cnid_metad", _PATH_LOCKDIR, 0)) {
492 default: /* server */
496 if ((srvfd = tsockfd_create(host, port, 10)) < 0)
501 LOG(log_debug, logtype_cnid, "Setting uid/gid to %i/%i", uid, gid);
503 if (SWITCH_TO_GID(gid) < 0) {
504 LOG(log_info, logtype_cnid, "unable to switch to group %d", gid);
509 if (SWITCH_TO_UID(uid) < 0) {
510 LOG(log_info, logtype_cnid, "unable to switch to user %d", uid);
519 sigprocmask(SIG_SETMASK, NULL, &set);
520 sigdelset(&set, SIGCHLD);
523 rqstfd = usockfd_check(srvfd, &set);
524 /* Collect zombie processes and log what happened to them */
525 if (sigchild) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
526 for (i = 0; i < MAXVOLS; i++) {
527 if (srv[i].pid == pid) {
529 close(srv[i].control_fd);
533 if (WIFEXITED(status)) {
534 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with exit code %i",
535 pid, WEXITSTATUS(status));
537 else if (WIFSIGNALED(status)) {
538 LOG(log_info, logtype_cnid, "cnid_dbd pid %i exited with signal %i",
539 pid, WTERMSIG(status));
546 /* TODO: Check out read errors, broken pipe etc. in libatalk. Is
547 SIGIPE ignored there? Answer: Ignored for dsi, but not for asp ... */
548 ret = read(rqstfd, &len, sizeof(int));
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 = read(rqstfd, dbdir, len);
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 if (set_dbdir(dbdir, len) < 0) {
585 if ((dbp = db_param_read(dbdir, METAD)) == NULL) {
586 LOG(log_error, logtype_cnid, "Error reading config file");
589 maybe_start_dbd(dbdpn, dbdir, dbp->usock_file);