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"
46 struct module *modchain = NULL;
47 static struct timer *timers = NULL;
48 static struct child *children = NULL;
51 static volatile int childrendone = 0;
53 struct timer *timercallback(double at, void (*func)(int, void *), void *data)
57 new = smalloc(sizeof(*new));
69 void canceltimer(struct timer *timer)
71 if(timer->next != NULL)
72 timer->next->prev = timer->prev;
73 if(timer->prev != NULL)
74 timer->prev->next = timer->next;
77 timer->func(1, timer->data);
81 void childcallback(pid_t pid, void (*func)(pid_t, int, void *), void *data)
85 new = smalloc(sizeof(*new));
97 static void preinit(int hup)
101 for(mod = modchain; mod != NULL; mod = mod->next)
105 if(!hup && ((mod->conf.vars != NULL) || (mod->conf.cmds != NULL)))
106 confregmod(&mod->conf);
110 static void init(int hup)
114 for(mod = modchain; mod != NULL; mod = mod->next)
116 if(mod->init && mod->init(hup))
118 flog(LOG_CRIT, "initialization of \"%s\" failed", mod->name);
124 static void terminate(void)
128 for(mod = modchain; mod != NULL; mod = mod->next)
135 static void handler(int signum)
141 extern int numfnetnodes, numtransfers, numdcpeers;
153 while((pid = waitpid(-1, &status, WNOHANG)) > 0)
155 for(child = children; child != NULL; child = child->next)
157 if(child->pid == pid)
160 child->status = status;
167 flog(LOG_NOTICE, "forking and dumping core upon SIGUSR1");
172 flog(LOG_NOTICE, "dumping memstats to /tmp/dc-mem upon SIGUSR2");
173 if((dumpfile = fopen("/tmp/dc-mem", "w")) == NULL) {
174 flog(LOG_ERR, "could not dump stats: %s", strerror(errno));
177 fprintf(dumpfile, "%i %i %i\n", numfnetnodes, numtransfers, numdcpeers);
183 pid_t forksess(uid_t user, struct authhandle *auth, void (*ccbfunc)(pid_t, int, void *), void *data, ...)
192 int maxfd, type, numfiles;
195 struct passwd *pwent;
202 if((pwent = getpwuid(user)) == NULL)
204 flog(LOG_WARNING, "no passwd entry for uid %i, cannot fork session", user);
208 if((geteuid() != 0) && (user != geteuid()))
210 flog(LOG_WARNING, "cannot fork non-owning session when not running as root (EUID is %i, target UID is %i)", geteuid(), user);
214 va_start(args, data);
218 while((type = va_arg(args, int)) != FD_END)
220 files = srealloc(files, sizeof(*files) * (numfiles + 1));
221 files[numfiles].fd = va_arg(args, int);
222 if(files[numfiles].fd > maxfd)
223 maxfd = files[numfiles].fd;
224 acc = va_arg(args, int);
229 flog(LOG_CRIT, "could not create pipe(!): %s", strerror(errno));
230 for(i = 0; i < numfiles; i++)
234 ibuf = va_arg(args, int *);
238 files[numfiles].tfd = cpipe[0];
241 files[numfiles].tfd = cpipe[1];
243 } else if(type == FD_FILE) {
244 buf = va_arg(args, char *);
245 if((files[numfiles].tfd = open(buf, acc)) < 0)
247 flog(LOG_CRIT, "could not open file \"%s\": %s", buf, strerror(errno));
248 for(i = 0; i < numfiles; i++)
253 if(files[numfiles].tfd > maxfd)
254 maxfd = files[numfiles].tfd;
258 sigemptyset(&sigset);
259 sigaddset(&sigset, SIGCHLD);
260 sigprocmask(SIG_BLOCK, &sigset, NULL);
261 if((pid = fork()) < 0)
263 flog(LOG_WARNING, "could not fork(!) in forksess(): %s", strerror(errno));
264 for(i = 0; i < numfiles; i++)
266 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
270 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
271 signal(SIGPIPE, SIG_DFL);
272 signal(SIGCHLD, SIG_DFL);
273 signal(SIGINT, SIG_DFL);
274 signal(SIGTERM, SIG_DFL);
275 signal(SIGHUP, SIG_DFL);
276 for(i = 0; i < numfiles; i++)
278 if(dup2(files[i].tfd, maxfd + i + 1) < 0)
280 files[i].tfd = maxfd + i + 1;
282 for(i = 0; i < numfiles; i++)
284 if(dup2(files[i].tfd, files[i].fd) < 0)
288 for(i = 0; i < FD_SETSIZE; i++)
292 for(o = 0; o < numfiles; o++)
304 signal(SIGHUP, SIG_IGN);
306 if((authopensess(auth)) != AUTH_SUCCESS)
308 flog(LOG_WARNING, "could not open session for user %s: %s", pwent->pw_name, (errno == 0)?"Unknown error - should be logged above":strerror(errno));
311 if((pid = fork()) < 0)
320 if(initgroups(pwent->pw_name, pwent->pw_gid))
322 flog(LOG_WARNING, "could not initgroups: %s", strerror(errno));
325 if(setgid(pwent->pw_gid))
327 flog(LOG_WARNING, "could not setgid: %s", strerror(errno));
330 if(setuid(pwent->pw_uid))
332 flog(LOG_WARNING, "could not setuid: %s", strerror(errno));
336 putenv(sprintf2("HOME=%s", pwent->pw_dir));
337 putenv(sprintf2("SHELL=%s", pwent->pw_shell));
338 putenv(sprintf2("USER=%s", pwent->pw_name));
339 putenv(sprintf2("LOGNAME=%s", pwent->pw_name));
340 putenv(sprintf2("PATH=%s/bin:/usr/local/bin:/bin:/usr/bin", pwent->pw_dir));
341 chdir(pwent->pw_dir);
344 for(i = 0; i < numfiles; i++)
346 while(((ret = waitpid(pid, &status, 0)) != pid) && (ret >= 0));
350 flog(LOG_WARNING, "waitpid(%i) said \"%s\"", pid, strerror(errno));
353 if(!WIFEXITED(status))
355 exit(WEXITSTATUS(status));
357 for(i = 0; i < numfiles; i++)
362 childcallback(pid, ccbfunc, data);
363 sigprocmask(SIG_UNBLOCK, &sigset, NULL);
367 int main(int argc, char **argv)
373 FILE *pfstream, *confstream;
374 int delay, immsyslog;
376 struct timer *timer, *ntimer;
380 immsyslog = nofork = 0;
381 syslogfac = LOG_DAEMON;
384 while((c = getopt(argc, argv, "p:C:f:hns")) != -1)
395 if(!strcmp(optarg, "auth"))
396 syslogfac = LOG_AUTH;
397 else if(!strcmp(optarg, "authpriv"))
398 syslogfac = LOG_AUTHPRIV;
399 else if(!strcmp(optarg, "cron"))
400 syslogfac = LOG_CRON;
401 else if(!strcmp(optarg, "daemon"))
402 syslogfac = LOG_DAEMON;
403 else if(!strcmp(optarg, "ftp"))
405 else if(!strcmp(optarg, "kern"))
406 syslogfac = LOG_KERN;
407 else if(!strcmp(optarg, "lpr"))
409 else if(!strcmp(optarg, "mail"))
410 syslogfac = LOG_MAIL;
411 else if(!strcmp(optarg, "news"))
412 syslogfac = LOG_NEWS;
413 else if(!strcmp(optarg, "syslog"))
414 syslogfac = LOG_SYSLOG;
415 else if(!strcmp(optarg, "user"))
416 syslogfac = LOG_USER;
417 else if(!strcmp(optarg, "uucp"))
418 syslogfac = LOG_UUCP;
419 else if(!strncmp(optarg, "local", 5) && (strlen(optarg) == 6))
420 syslogfac = LOG_LOCAL0 + (optarg[5] - '0');
422 fprintf(stderr, "unknown syslog facility %s, using daemon\n", optarg);
434 printf("usage: doldacond [-hns] [-C configfile] [-p pidfile] [-f facility]\n");
438 setlocale(LC_ALL, "");
445 signal(SIGPIPE, SIG_IGN);
446 signal(SIGHUP, handler);
447 signal(SIGINT, handler);
448 signal(SIGTERM, handler);
449 signal(SIGCHLD, handler);
450 signal(SIGUSR1, handler);
451 signal(SIGUSR2, handler);
453 if(configfile == NULL)
455 if((configfile = findconfigfile()) == NULL)
457 flog(LOG_CRIT, "could not find a configuration file");
464 if((pfstream = fopen(pidfile, "w")) == NULL)
466 flog(LOG_CRIT, "could not open specified PID file %s: %s", pidfile, strerror(errno));
470 if((confstream = fopen(configfile, "r")) == NULL)
472 flog(LOG_CRIT, "could not open configuration file %s: %s", configfile, strerror(errno));
475 readconfig(confstream);
482 flog(LOG_INFO, "daemonized");
485 if(pfstream != NULL) {
486 fprintf(pfstream, "%i\n", getpid());
495 if((confstream = fopen(configfile, "r")) == NULL)
497 flog(LOG_ERR, "could not open configuration file %s: %s (ignoring HUP)", configfile, strerror(errno));
500 readconfig(confstream);
506 delay = 1000; /* -1; */
507 for(mod = modchain; mod != NULL; mod = mod->next)
509 if(mod->run && mod->run())
517 for(timer = timers; timer != NULL; timer = timer->next)
519 if((delay == -1) || ((int)((timer->at - now) * 1000.0) < delay))
520 delay = (int)((timer->at - now) * 1000.0);
530 for(timer = timers; timer != NULL; timer = ntimer)
532 ntimer = timer->next;
535 if(timer->prev != NULL)
536 timer->prev->next = timer->next;
537 if(timer->next != NULL)
538 timer->next->prev = timer->prev;
540 timers = timer->next;
541 timer->func(0, timer->data);
546 for(child = children; child != NULL; child = child->next)
550 child->callback(child->pid, child->status, child->data);
551 if(child == children)
552 children = child->next;
553 if(child->prev != NULL)
554 child->prev->next = child->next;
555 if(child->next != NULL)
556 child->next->prev = child->prev;
561 } while(child != NULL);
563 flog(LOG_INFO, "terminating...");