comparison toys/pending/dhcp.c @ 1396:e0c9c5424864 draft

Isaac Dunham spotted that dhcp was also reimplementing daemon().
author Rob Landley <rob@landley.net>
date Sun, 20 Jul 2014 21:34:49 -0500
parents 85f297591693
children 856b544f8fce
comparison
equal deleted inserted replaced
1395:5149632b0d07 1396:e0c9c5424864
309 xioctl(fd, SIOCGIFHWADDR, &req); 309 xioctl(fd, SIOCGIFHWADDR, &req);
310 memcpy(mac, req.ifr_hwaddr.sa_data, 6); 310 memcpy(mac, req.ifr_hwaddr.sa_data, 6);
311 dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 311 dbg("MAC %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
312 } 312 }
313 close(fd); 313 close(fd);
314 return 0;
315 }
316
317 static int dhcp_daemon(void)
318 {
319 int fd = open("/dev/null", O_RDWR);
320 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666);
321
322 if (xfork()) exit(0);
323
324 setsid();
325 dup2(fd, 0);
326 dup2(fd, 1);
327 dup2(fd, 2);
328 if (fd > 2) xclose(fd);
329
330 return 0; 314 return 0;
331 } 315 }
332 316
333 /* 317 /*
334 *logs messeges to syslog or console 318 *logs messeges to syslog or console
1366 infomsg(infomode, "Lease failed. Exiting"); 1350 infomsg(infomode, "Lease failed. Exiting");
1367 goto ret_with_sockfd; 1351 goto ret_with_sockfd;
1368 } 1352 }
1369 if (flag_chk(FLAG_b)) { 1353 if (flag_chk(FLAG_b)) {
1370 infomsg(infomode, "Lease failed. Going Daemon mode"); 1354 infomsg(infomode, "Lease failed. Going Daemon mode");
1371 dhcp_daemon(); 1355 daemon(0, 0);
1372 if (flag_chk(FLAG_p)) write_pid(TT.pidfile); 1356 if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
1373 toys.optflags &= ~FLAG_b; 1357 toys.optflags &= ~FLAG_b;
1374 toys.optflags |= FLAG_f; 1358 toys.optflags |= FLAG_f;
1375 } 1359 }
1376 timeout = flag_get(FLAG_A, TT.tryagain, 20); 1360 timeout = flag_get(FLAG_A, TT.tryagain, 20);
1503 if (flag_chk(FLAG_R)) release(); 1487 if (flag_chk(FLAG_R)) release();
1504 goto ret_with_sockfd; 1488 goto ret_with_sockfd;
1505 } 1489 }
1506 toys.optflags &= ~FLAG_n; 1490 toys.optflags &= ~FLAG_n;
1507 if (!flag_chk(FLAG_f)) { 1491 if (!flag_chk(FLAG_f)) {
1508 dhcp_daemon(); 1492 daemon(0, 0);
1509 toys.optflags |= FLAG_f; 1493 toys.optflags |= FLAG_f;
1510 if (flag_chk(FLAG_p)) write_pid(TT.pidfile); 1494 if (flag_chk(FLAG_p)) write_pid(TT.pidfile);
1511 } 1495 }
1512 waited = 0; 1496 waited = 0;
1513 continue; 1497 continue;