--- ansh/src/anshd.c 2011/10/04 22:37:46 1.1.1.1 +++ ansh/src/anshd.c 2011/10/10 13:56:30 1.1.1.1.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ - * $Id: anshd.c,v 1.1.1.1 2011/10/04 22:37:46 misho Exp $ + * $Id: anshd.c,v 1.1.1.1.2.3 2011/10/10 13:56:30 misho Exp $ * *************************************************************************/ #include "global.h" @@ -12,9 +12,10 @@ intptr_t Kill; -int bpfLEN, Verbose, Crypted = 1; +int Verbose; +u_int Crypted = 1; proc_head_t pH; -int Daemon = 1; +int bpfLEN, Timeout, Daemon = 1; extern char compiled[], compiledby[], compilehost[]; @@ -25,9 +26,9 @@ Usage() "=== %s === %s@%s ===\n\n" " Syntax: anshd [options]\n\n" "\t-d \tBind to host interface, like 'em0' (default is first host interface)\n" - "\t-i \tService ID (default is 42)\n" "\t-U \tRun service with other user\n" "\t-C \tRun service into chroot directory\n" + "\t-t \tTimeout of login if no activity (default is 0 sec)\n" "\t-u\t\tSwitch to unencrypted traffic between hosts\n" "\t-b\t\tRun into batch mode (default is daemon mode)\n" "\t-v\t\tVerbose (more -v, more verbosity ...)\n" @@ -42,27 +43,41 @@ sig(int s) switch (s) { case SIGHUP: - LOG("Got SIGHUP!\n"); + VERB(1) LOG("Got SIGHUP!\n"); break; case SIGTERM: - LOG("Got SIGTERM!\n"); Kill++; + VERB(1) LOG("Got SIGTERM!\n"); break; case SIGPIPE: - LOG("Got SIGPIPE!\n"); + VERB(1) LOG("Got SIGPIPE!\n"); break; case SIGCHLD: + VERB(1) LOG("Got SIGCHLD!\n"); while (waitpid(-1, &state, WNOHANG) > 0); break; } } +static void * +hook_error(void *root, void *arg) +{ +/* sched_root_task_t *r = root; */ + + if (!root) + return (void*) -1; + + if (arg == (void*) EINTR) + return (void*) -1; + + return NULL; +} + int main(int argc, char **argv) { struct passwd *pass; int fd, h = 0, uid = 0, gid = 0; - long id = ANSH_ID; char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/", szDev[STRSIZ] = { 0 }; struct sigaction sact; sched_root_task_t *root = NULL; @@ -70,7 +85,7 @@ main(int argc, char **argv) Get1stEth(szDev, STRSIZ); - while ((ch = getopt(argc, argv, "hvulbd:U:C:")) != -1) + while ((ch = getopt(argc, argv, "hvubt:d:U:C:")) != -1) switch (ch) { case 'U': pass = getpwnam(optarg); @@ -91,8 +106,8 @@ main(int argc, char **argv) } else strlcpy(szChroot, optarg, sizeof szChroot); break; - case 'i': - id = strtol(optarg, NULL, 0); + case 't': + Timeout = abs(strtol(optarg, NULL, 0)); break; case 'd': strlcpy(szDev, optarg, sizeof szDev); @@ -152,17 +167,25 @@ main(int argc, char **argv) } } + if (ioCreatePIDFile(PIDFILE_ANSHD, 42)) { + ERR("Error:: already started anshd service ...\n"); + closelog(); + return 1; + } + h = PrepareL2(szDev, &bpfLEN); if (h == -1) { ERR("Error:: Descriptor not opened ... abort!\n"); + unlink(PIDFILE_ANSHD); closelog(); return 2; } SLIST_INIT(&pH); - if (!(proc = InitProc(h, NULL, id, bpfLEN))) { + if (!(proc = InitProc(h, NULL, ANSH_ID, bpfLEN))) { ERR("Error:: Not enough memory ...\n"); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 3; } @@ -170,11 +193,13 @@ main(int argc, char **argv) root = schedBegin(); if (!root) { ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError()); - DestroyProc(id); + DestroyProc(ANSH_ID); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 4; - } + } else + root->root_hooks.hook_root.error = hook_error; chdir("/"); chroot(szChroot); @@ -182,15 +207,16 @@ main(int argc, char **argv) setgid(gid); setuid(uid); - if (schedRead(root, pktRx, (void*) id, h)) { + if (schedRead(root, pktRx, (void*) ANSH_ID, h)) { schedRun(root, &Kill); } else ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError()); VERB(1) LOG("Finish process."); schedEnd(&root); - DestroyProc(id); + DestroyProc(ANSH_ID); close(h); + unlink(PIDFILE_ANSHD); closelog(); return 0; }