2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
43 #include "sysevents.h"
50 struct module *modchain = NULL;
51 static struct timer *timers = NULL;
52 static struct child *children = NULL;
55 static volatile int childrendone = 0;
57 struct timer *timercallback(double at, void (*func)(int, void *), void *data)
61 new = smalloc(sizeof(*new));
73 void canceltimer(struct timer *timer)
75 if(timer->next != NULL)
76 timer->next->prev = timer->prev;
77 if(timer->prev != NULL)
78 timer->prev->next = timer->next;
81 timer->func(1, timer->data);
85 void childcallback(pid_t pid, void (*func)(pid_t, int, void *), void *data)
89 new = smalloc(sizeof(*new));
101 static void preinit(int hup)
105 for(mod = modchain; mod != NULL; mod = mod->next)
109 if(!hup && ((mod->conf.vars != NULL) || (mod->conf.cmds != NULL)))
110 confregmod(&mod->conf);
114 static void init(int hup)
118 for(mod = modchain; mod != NULL; mod = mod->next)
120 if(mod->init && mod->init(hup))
122 flog(LOG_CRIT, "initialization of \"%s\" failed", mod->name);
128 static void terminate(void)
132 for(mod = modchain; mod != NULL; mod = mod->next)
139 static void handler(int signum)
145 extern int numfnetnodes, numtransfers, numdcpeers;
157 while((pid = waitpid(-1, &status, WNOHANG)) > 0)
159 for(child = children; child != NULL; child = child->next)
161 if(child->pid == pid)
164 child->status = status;
171 flog(LOG_NOTICE, "forking and dumping core upon SIGUSR1");
176 flog(LOG_NOTICE, "dumping memstats to /tmp/dc-mem upon SIGUSR2");
177 if((dumpfile = fopen("/tmp/dc-mem", "w")) == NULL) {
178 flog(LOG_ERR, "could not dump stats: %s", strerror(errno));
181 fprintf(dumpfile, "%i %i %i\n", numfnetnodes, numtransfers, numdcpeers);
187 pid_t forksess(uid_t user, struct authhandle *auth, void (*ccbfunc)(pid_t, int, void *), void *data, ...)
196 int maxfd, type, numfiles;
199 struct passwd *pwent;
206 if((pwent = getpwuid(user)) == NULL)
208 flog(LOG_WARNING, "no passwd entry for uid %i, cannot fork session", user);
212 if((geteuid() != 0) && (user != geteuid()))
214 flog(LOG_WARNING, "cannot fork non-owning session when not running as root (EUID is %i, target UID is %i)", geteuid(), user);
218 va_start(args, data);
222 while((type = va_arg(args, int)) != FD_END)
224 files = srealloc(files, sizeof(*files) * (numfiles + 1));
225 files[numfiles].fd = va_arg(args, int);
226 if(files[numfiles].fd > maxfd)
227 maxfd = files[numfiles].fd;
228 acc = va_arg(args, int);
233 flog(LOG_CRIT, "could not create pipe(!): %s", strerror(errno));
234 for(i = 0; i < numfiles; i++)
238 ibuf = va_arg(args, int *);
242 files[numfiles].tfd = cpipe[0];
245 files[numfiles].tfd = cpipe[1];
247 } else if(type == FD_FILE) {
248 buf = va_arg(args, char *);
249 if((files[numfiles].tfd = open(buf, acc)) < 0)
251 flog(LOG_CRIT, "could not open file \"%s\": %s", buf, strerror(errno));
252 for(i = 0; i < numfiles; i++)
257 if(files[numfiles].tfd > maxfd)
258 maxfd = files[numfiles].tfd;
262 sigemptyset(&sigset);
263 sigaddset(&sigset, SIGCHLD);
264 sigprocmask(SIG_BLOCK, &sigset, NULL);
265 if((pid = fork()) < 0)
267 flog(LOG_WARNING, "could not fork(!) in forksess(): %s", strerror(errno));
268 for(i = 0; i < numfiles; i++)
270 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
274 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
275 signal(SIGPIPE, SIG_DFL);
276 signal(SIGCHLD, SIG_DFL);
277 signal(SIGINT, SIG_DFL);
278 signal(SIGTERM, SIG_DFL);
279 signal(SIGHUP, SIG_DFL);
280 for(i = 0; i < numfiles; i++)
282 if(dup2(files[i].tfd, maxfd + i + 1) < 0)
284 files[i].tfd = maxfd + i + 1;
286 for(i = 0; i < numfiles; i++)
288 if(dup2(files[i].tfd, files[i].fd) < 0)
292 for(i = 0; i < FD_SETSIZE; i++)
296 for(o = 0; o < numfiles; o++)
308 signal(SIGHUP, SIG_IGN);
311 keyctl_join_session_keyring(NULL);
312 keyctl_chown(KEY_SPEC_SESSION_KEYRING, pwent->pw_uid, pwent->pw_gid);
314 if((authopensess(auth)) != AUTH_SUCCESS)
316 flog(LOG_WARNING, "could not open session for user %s: %s", pwent->pw_name, (errno == 0)?"Unknown error - should be logged above":strerror(errno));
319 if((pid = fork()) < 0)
328 if(initgroups(pwent->pw_name, pwent->pw_gid))
330 flog(LOG_WARNING, "could not initgroups: %s", strerror(errno));
333 if(setgid(pwent->pw_gid))
335 flog(LOG_WARNING, "could not setgid: %s", strerror(errno));
338 if(setuid(pwent->pw_uid))
340 flog(LOG_WARNING, "could not setuid: %s", strerror(errno));
343 putenv(sprintf2("HOME=%s", pwent->pw_dir));
344 putenv(sprintf2("SHELL=%s", pwent->pw_shell));
345 putenv(sprintf2("PATH=%s/bin:/usr/local/bin:/bin:/usr/bin", pwent->pw_dir));
347 putenv(sprintf2("USER=%s", pwent->pw_name));
348 putenv(sprintf2("LOGNAME=%s", pwent->pw_name));
349 chdir(pwent->pw_dir);
352 for(i = 0; i < numfiles; i++)
354 while(((ret = waitpid(pid, &status, 0)) != pid) && (ret >= 0));
358 flog(LOG_WARNING, "waitpid(%i) said \"%s\"", pid, strerror(errno));
361 if(!WIFEXITED(status))
363 exit(WEXITSTATUS(status));
365 for(i = 0; i < numfiles; i++)
370 childcallback(pid, ccbfunc, data);
371 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
375 int main(int argc, char **argv)
381 FILE *pfstream, *confstream;
382 int delay, immsyslog;
389 immsyslog = nofork = 0;
390 syslogfac = LOG_DAEMON;
393 while((c = getopt(argc, argv, "p:C:f:hnsV")) != -1)
404 if(!strcmp(optarg, "auth"))
405 syslogfac = LOG_AUTH;
406 else if(!strcmp(optarg, "authpriv"))
407 syslogfac = LOG_AUTHPRIV;
408 else if(!strcmp(optarg, "cron"))
409 syslogfac = LOG_CRON;
410 else if(!strcmp(optarg, "daemon"))
411 syslogfac = LOG_DAEMON;
412 else if(!strcmp(optarg, "ftp"))
414 else if(!strcmp(optarg, "kern"))
415 syslogfac = LOG_KERN;
416 else if(!strcmp(optarg, "lpr"))
418 else if(!strcmp(optarg, "mail"))
419 syslogfac = LOG_MAIL;
420 else if(!strcmp(optarg, "news"))
421 syslogfac = LOG_NEWS;
422 else if(!strcmp(optarg, "syslog"))
423 syslogfac = LOG_SYSLOG;
424 else if(!strcmp(optarg, "user"))
425 syslogfac = LOG_USER;
426 else if(!strcmp(optarg, "uucp"))
427 syslogfac = LOG_UUCP;
428 else if(!strncmp(optarg, "local", 5) && (strlen(optarg) == 6))
429 syslogfac = LOG_LOCAL0 + (optarg[5] - '0');
431 fprintf(stderr, "unknown syslog facility %s, using daemon\n", optarg);
440 printf("%s", RELEASEINFO);
446 printf("usage: doldacond [-hnsV] [-C configfile] [-p pidfile] [-f facility]\n");
450 setlocale(LC_ALL, "");
457 signal(SIGPIPE, SIG_IGN);
458 signal(SIGHUP, handler);
459 signal(SIGINT, handler);
460 signal(SIGTERM, handler);
461 signal(SIGCHLD, handler);
462 signal(SIGUSR1, handler);
463 signal(SIGUSR2, handler);
465 if(configfile == NULL)
467 if((configfile = findfile("doldacond.conf", NULL, 0)) == NULL)
469 flog(LOG_CRIT, "could not find a configuration file");
476 if((pfstream = fopen(pidfile, "w")) == NULL)
478 flog(LOG_CRIT, "could not open specified PID file %s: %s", pidfile, strerror(errno));
482 if((confstream = fopen(configfile, "r")) == NULL)
484 flog(LOG_CRIT, "could not open configuration file %s: %s", configfile, strerror(errno));
487 readconfig(confstream);
494 flog(LOG_INFO, "daemonized");
497 if(pfstream != NULL) {
498 fprintf(pfstream, "%i\n", getpid());
501 flog(LOG_INFO, "startup took %f seconds", ntime() - now);
508 if((confstream = fopen(configfile, "r")) == NULL)
510 flog(LOG_ERR, "could not open configuration file %s: %s (ignoring HUP)", configfile, strerror(errno));
513 readconfig(confstream);
519 delay = 1000; /* -1; */
520 for(mod = modchain; mod != NULL; mod = mod->next)
522 if(mod->run && mod->run())
530 for(timer = timers; timer != NULL; timer = timer->next)
532 if((delay == -1) || ((int)((timer->at - now) * 1000.0) < delay))
533 delay = (int)((timer->at - now) * 1000.0);
545 for(timer = timers; timer != NULL; timer = timer->next)
549 if(timer->prev != NULL)
550 timer->prev->next = timer->next;
551 if(timer->next != NULL)
552 timer->next->prev = timer->prev;
554 timers = timer->next;
555 timer->func(0, timer->data);
559 } while(timer != NULL);
562 for(child = children; child != NULL; child = child->next)
566 child->callback(child->pid, child->status, child->data);
567 if(child == children)
568 children = child->next;
569 if(child->prev != NULL)
570 child->prev->next = child->next;
571 if(child->next != NULL)
572 child->next->prev = child->prev;
577 } while(child != NULL);
579 flog(LOG_INFO, "terminating...");