/* * Initialize all "candidate" interfaces that are in the system * configuration list. A "candidate" is up, not loopback and not * point to point. */ void deviceInitAll(void) { struct ifaddrs *ifap, *ifa; if (getifaddrs(&ifap) != 0) mopLogErr("deviceInitAll: socket"); for (ifa = ifap; ifa; ifa = ifa->ifa_next) { #ifdef AF_LINK struct sockaddr_dl *sdl; sdl = (struct sockaddr_dl *)ifa->ifa_addr; if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER || sdl->sdl_alen != 6) continue; #endif if ((ifa->ifa_flags & (IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP) continue; deviceInitOne(ifa->ifa_name); } freeifaddrs(ifap); }
int main(int argc, char **argv) { int c, pid; extern char version[]; while ((c = getopt(argc, argv, "34adfs:v")) != -1) { switch (c) { case '3': Not3Flag++; break; case '4': Not4Flag++; break; case 'a': AllFlag++; break; case 'd': DebugFlag++; break; case 'f': ForegroundFlag++; break; case 's': MopdDir = optarg; break; case 'v': VersionFlag++; break; default: Usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (VersionFlag) { fprintf(stdout,"%s: version %s\n", getprogname(), version); exit(0); } if ((AllFlag && argc != 0) || (!AllFlag && argc == 0) || (Not3Flag && Not4Flag)) Usage(); /* All error reporting is done through syslogs. */ openlog("mopd", LOG_PID, LOG_DAEMON); if ((!ForegroundFlag) && DebugFlag) fprintf(stdout, "%s: not running as daemon, -d given.\n", getprogname()); if ((!ForegroundFlag) && (!DebugFlag)) { pid = fork(); if (pid > 0) /* Parent exits, leaving child in background. */ exit(0); else if (pid == -1) { syslog(LOG_ERR, "cannot fork"); exit(0); } /* Fade into the background */ daemon(0, 0); pidfile(NULL); } syslog(LOG_INFO, "%s %s started.", getprogname(), version); if (AllFlag) deviceInitAll(); else { while (argc--) deviceInitOne(*argv++); } Loop(); /* NOTREACHED */ return (0); }
int main(int argc, char *argv[]) { int c; char *interface; struct passwd *pw; extern char version[]; while ((c = getopt(argc, argv, "34adfv")) != -1) switch (c) { case '3': Not3Flag = 1; break; case '4': Not4Flag = 1; break; case 'a': AllFlag = 1; break; case 'd': DebugFlag++; break; case 'f': ForegroundFlag = 1; break; case 'v': VersionFlag = 1; break; default: Usage(); /* NOTREACHED */ } if (VersionFlag) { fprintf(stdout,"%s: version %s\n", __progname, version); exit(0); } interface = argv[optind++]; if ((AllFlag && interface) || (!AllFlag && interface == 0) || (argc > optind) || (Not3Flag && Not4Flag)) Usage(); /* All error reporting is done through syslogs. */ openlog(__progname, LOG_PID | LOG_CONS, LOG_DAEMON); tzset(); if ((pw = getpwnam("_mopd")) == NULL) err(1, "getpwnam"); if ((!ForegroundFlag) && DebugFlag) fprintf(stdout, "%s: not running as daemon, -d given.\n", __progname); if ((!ForegroundFlag) && (!DebugFlag)) if (daemon(0, 0) == -1) err(1, NULL); syslog(LOG_INFO, "%s %s started.", __progname, version); if (AllFlag) deviceInitAll(); else deviceInitOne(interface); if (chroot(MOP_FILE_PATH) == -1) { syslog(LOG_CRIT, "chroot %s: %m", MOP_FILE_PATH); exit(1); } if (chdir("/") == -1) { syslog(LOG_CRIT, "chdir(\"/\"): %m"); exit(1); } if (setgroups(1, &pw->pw_gid) || setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) { syslog(LOG_CRIT, "can't drop privileges: %m"); exit(1); } endpwent(); Loop(); /* NOTREACHED */ }