2 * Revision 1.1 1996/03/13 22:27:41 sde1000
15 #include <sys/socket.h>
16 #include <arpa/inet.h>
22 /* Command-line options (possibly config-file options too) */
23 static char *configfile="/etc/secnet/secnet.conf";
24 static char *userid=NULL;
26 static bool_t background=True;
27 static char *pidfile=NULL;
29 /* Structures dealing with poll() call */
30 struct poll_interest {
31 beforepoll_fn *before;
37 struct poll_interest *next;
39 static struct poll_interest *reg=NULL;
40 static uint32_t total_nfds=10;
42 static bool_t finished=False;
44 /* Parse the command line options */
45 static void parse_options(int argc, char **argv)
51 static struct option long_options[] = {
52 {"verbose", 0, 0, 'v'},
53 {"nowarnings", 0, 0, 'w'},
56 {"nodetach", 0, 0, 'n'},
57 {"silent", 0, 0, 'f'},
60 {"config", 1, 0, 'c'},
64 c=getopt_long(argc, argv, "vwdnc:ft:",
65 long_options, &option_index);
73 "Usage: secnet [OPTION]...\n\n"
74 " -f, --silent, --quiet suppress error messages\n"
75 " -w, --nowarnings suppress warnings\n"
76 " -v, --verbose output extra diagnostics\n"
77 " -c, --config=filename specify a configuration file\n"
78 " -n, --nodetach do not run in background\n"
79 " -d, --debug=item,... set debug options\n"
80 " --help display this help and exit\n"
81 " --version output version information and exit\n"
88 fprintf(stderr,"%s\n",version);
93 message_level|=M_INFO|M_WARNING|M_ERROR|M_FATAL;
101 message_level|=M_DEBUG_CONFIG|M_DEBUG_PHASE;
105 message_level=M_FATAL;
110 configfile=safe_strdup(optarg,"config_filename");
112 fatal("secnet: no config filename specified");
119 Message(M_WARNING,"secnet: Unknown getopt code %c\n",c);
123 if (argc-optind != 0) {
124 Message(M_WARNING,"secnet: You gave extra command line parameters, "
125 "which were ignored.\n");
129 static void setup(dict_t *config)
139 l=dict_lookup(config,"system");
141 if (!l || list_elem(l,0)->type!=t_dict) {
142 fatal("configuration does not include a \"system\" dictionary\n");
144 system=list_elem(l,0)->data.dict;
145 loc=list_elem(l,0)->loc;
147 /* Arrange systemwide log facility */
148 l=dict_lookup(system,"log");
150 fatal("configuration does not include a system/log facility\n");
153 log->log(log->st,LOG_DEBUG,"%s: logging started",version);
155 /* Who are we supposed to run as? */
156 userid=dict_read_string(system,"userid",False,"system",loc);
160 if (pw && strcmp(pw->pw_name,userid)==0) {
167 fatal("userid \"%s\" not found\n",userid);
172 pidfile=dict_read_string(system,"pidfile",False,"system",loc);
174 /* Go along site list, starting sites */
175 l=dict_lookup(config,"sites");
177 fatal("configuration did not define any remote sites\n");
180 while ((site=list_elem(l, i++))) {
182 if (site->type!=t_closure) {
183 cfgfatal(site->loc,"system","non-closure in site list");
185 if (site->data.closure->type!=CL_SITE) {
186 cfgfatal(site->loc,"system","non-site closure in site list");
188 s=site->data.closure->interface;
189 s->control(s->st,True);
193 void register_for_poll(void *st, beforepoll_fn *before,
194 afterpoll_fn *after, uint32_t max_nfds, string_t desc)
196 struct poll_interest *i;
198 i=safe_malloc(sizeof(*i),"register_for_poll");
202 i->max_nfds=max_nfds;
205 total_nfds+=max_nfds;
211 static void system_phase_hook(void *sst, uint32_t newphase)
213 if (newphase==PHASE_SHUTDOWN && pidfile) {
214 /* Try to unlink the pidfile; don't care if it fails */
219 static void run(void)
221 struct timeval tv_now;
223 struct poll_interest *i;
224 int rv, nfds, remain, idx;
228 fds=alloca(sizeof(*fds)*total_nfds);
230 fatal("run: couldn't alloca\n");
234 if (gettimeofday(&tv_now, NULL)!=0) {
235 fatal_perror("main loop: gettimeofday");
237 now=(tv_now.tv_sec*1000)+(tv_now.tv_usec/1000);
239 for (i=reg; i; i=i->next) {
240 i->after(i->state, fds+idx, i->nfds, &tv_now, &now);
246 for (i=reg; i; i=i->next) {
248 rv=i->before(i->state, fds+idx, &nfds, &timeout, &tv_now, &now);
250 /* XXX we need to handle this properly: increase the
252 fatal("run: beforepoll_fn (%s) returns %d\n",i->desc,rv);
255 fatal("run: beforepoll_fn (%s) set timeout to %d\n",timeout);
262 rv=poll(fds, idx, timeout);
265 fatal_perror("run: poll");
272 static void droppriv(void)
277 add_hook(PHASE_SHUTDOWN,system_phase_hook,NULL);
279 /* Background now, if we're supposed to: we may be unable to write the
280 pidfile if we don't. */
282 printf("goto background\n");
283 /* Open the pidfile before forking - that way the parent can tell
284 whether it succeeds */
286 pf=fopen(pidfile,"w");
288 fatal_perror("cannot open pidfile \"%s\"",pidfile);
291 Message(M_WARNING,"secnet: no pidfile configured, but "
292 "backgrounding anyway\n");
297 /* Parent process - write pidfile, exit */
298 fprintf(pf,"%d\n",p);
303 /* Child process - all done, just carry on */
308 fatal_perror("cannot fork");
313 pf=fopen(pidfile,"w");
315 fatal_perror("cannot open pidfile \"%s\"",pidfile);
317 fprintf(pf,"%d\n",getpid());
322 /* Drop privilege now, if configured to do so */
324 if (setuid(uid)!=0) {
325 fatal_perror("can't set uid to \"%s\"",userid);
330 int main(int argc, char **argv)
334 enter_phase(PHASE_GETOPTS);
335 parse_options(argc,argv);
337 enter_phase(PHASE_READCONFIG);
338 config=read_conffile(configfile);
340 enter_phase(PHASE_SETUP);
343 enter_phase(PHASE_DROPPRIV);
346 enter_phase(PHASE_RUN);
349 enter_phase(PHASE_SHUTDOWN);