dhcpc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /* vi: set sw=4 ts=4: */
  2. /* dhcpc.c
  3. *
  4. * udhcp DHCP client
  5. *
  6. * Russ Dill <Russ.Dill@asu.edu> July 2001
  7. *
  8. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  9. */
  10. #include <getopt.h>
  11. #include <syslog.h>
  12. /* Override ENABLE_FEATURE_PIDFILE - ifupdown needs our pidfile to always exist */
  13. #define WANT_PIDFILE 1
  14. #include "common.h"
  15. #include "dhcpd.h"
  16. #include "dhcpc.h"
  17. #include "options.h"
  18. static int timeout; /* = 0. Must be signed */
  19. static uint32_t requested_ip; /* = 0 */
  20. static uint32_t server_addr;
  21. static int sockfd = -1;
  22. #define LISTEN_NONE 0
  23. #define LISTEN_KERNEL 1
  24. #define LISTEN_RAW 2
  25. static smallint listen_mode;
  26. static smallint state;
  27. /* struct client_config_t client_config is in bb_common_bufsiz1 */
  28. /* just a little helper */
  29. static void change_listen_mode(int new_mode)
  30. {
  31. DEBUG("entering %s listen mode",
  32. new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
  33. if (sockfd >= 0) {
  34. close(sockfd);
  35. sockfd = -1;
  36. }
  37. listen_mode = new_mode;
  38. }
  39. /* perform a renew */
  40. static void perform_renew(void)
  41. {
  42. bb_info_msg("Performing a DHCP renew");
  43. switch (state) {
  44. case BOUND:
  45. change_listen_mode(LISTEN_KERNEL);
  46. case RENEWING:
  47. case REBINDING:
  48. state = RENEW_REQUESTED;
  49. break;
  50. case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
  51. udhcp_run_script(NULL, "deconfig");
  52. case REQUESTING:
  53. case RELEASED:
  54. change_listen_mode(LISTEN_RAW);
  55. state = INIT_SELECTING;
  56. break;
  57. case INIT_SELECTING:
  58. break;
  59. }
  60. }
  61. /* perform a release */
  62. static void perform_release(void)
  63. {
  64. char buffer[sizeof("255.255.255.255")];
  65. struct in_addr temp_addr;
  66. /* send release packet */
  67. if (state == BOUND || state == RENEWING || state == REBINDING) {
  68. temp_addr.s_addr = server_addr;
  69. strcpy(buffer, inet_ntoa(temp_addr));
  70. temp_addr.s_addr = requested_ip;
  71. bb_info_msg("Unicasting a release of %s to %s",
  72. inet_ntoa(temp_addr), buffer);
  73. send_release(server_addr, requested_ip); /* unicast */
  74. udhcp_run_script(NULL, "deconfig");
  75. }
  76. bb_info_msg("Entering released state");
  77. change_listen_mode(LISTEN_NONE);
  78. state = RELEASED;
  79. timeout = INT_MAX;
  80. }
  81. static void client_background(void)
  82. {
  83. #if !BB_MMU
  84. bb_error_msg("cannot background in uclinux (yet)");
  85. /* ... mainly because udhcpc calls client_background()
  86. * in _the _middle _of _udhcpc _run_, not at the start!
  87. * If that will be properly disabled for NOMMU, client_background()
  88. * will work on NOMMU too */
  89. #else
  90. bb_daemonize(0);
  91. logmode &= ~LOGMODE_STDIO;
  92. /* rewrite pidfile, as our pid is different now */
  93. write_pidfile(client_config.pidfile);
  94. #endif
  95. /* Do not fork again. */
  96. client_config.foreground = 1;
  97. client_config.background_if_no_lease = 0;
  98. }
  99. static uint8_t* alloc_dhcp_option(int code, const char *str, int extra)
  100. {
  101. uint8_t *storage;
  102. int len = strlen(str);
  103. if (len > 255) len = 255;
  104. storage = xzalloc(len + extra + OPT_DATA);
  105. storage[OPT_CODE] = code;
  106. storage[OPT_LEN] = len + extra;
  107. memcpy(storage + extra + OPT_DATA, str, len);
  108. return storage;
  109. }
  110. int udhcpc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  111. int udhcpc_main(int argc ATTRIBUTE_UNUSED, char **argv)
  112. {
  113. uint8_t *temp, *message;
  114. char *str_c, *str_V, *str_h, *str_F, *str_r;
  115. USE_FEATURE_UDHCP_PORT(char *str_P;)
  116. llist_t *list_O = NULL;
  117. #if ENABLE_FEATURE_UDHCPC_ARPING
  118. char *str_W;
  119. #endif
  120. int tryagain_timeout = 20;
  121. int discover_timeout = 3;
  122. int discover_retries = 3;
  123. uint32_t xid = 0;
  124. uint32_t lease_seconds = 0; /* can be given as 32-bit quantity */
  125. int packet_num;
  126. /* t1, t2... what a wonderful names... */
  127. unsigned t1 = t1; /* for gcc */
  128. unsigned t2 = t2;
  129. unsigned timestamp_got_lease = timestamp_got_lease;
  130. unsigned opt;
  131. int max_fd;
  132. int retval;
  133. int len;
  134. struct timeval tv;
  135. struct in_addr temp_addr;
  136. struct dhcpMessage packet;
  137. fd_set rfds;
  138. enum {
  139. OPT_c = 1 << 0,
  140. OPT_C = 1 << 1,
  141. OPT_V = 1 << 2,
  142. OPT_f = 1 << 3,
  143. OPT_b = 1 << 4,
  144. OPT_H = 1 << 5,
  145. OPT_h = 1 << 6,
  146. OPT_F = 1 << 7,
  147. OPT_i = 1 << 8,
  148. OPT_n = 1 << 9,
  149. OPT_p = 1 << 10,
  150. OPT_q = 1 << 11,
  151. OPT_R = 1 << 12,
  152. OPT_r = 1 << 13,
  153. OPT_s = 1 << 14,
  154. OPT_T = 1 << 15,
  155. OPT_t = 1 << 16,
  156. OPT_v = 1 << 17,
  157. OPT_S = 1 << 18,
  158. OPT_A = 1 << 19,
  159. #if ENABLE_FEATURE_UDHCPC_ARPING
  160. OPT_a = 1 << 20,
  161. OPT_W = 1 << 21,
  162. #endif
  163. OPT_P = 1 << 22,
  164. };
  165. #if ENABLE_GETOPT_LONG
  166. static const char udhcpc_longopts[] ALIGN1 =
  167. "clientid\0" Required_argument "c"
  168. "clientid-none\0" No_argument "C"
  169. "vendorclass\0" Required_argument "V"
  170. "foreground\0" No_argument "f"
  171. "background\0" No_argument "b"
  172. "hostname\0" Required_argument "H"
  173. "fqdn\0" Required_argument "F"
  174. "interface\0" Required_argument "i"
  175. "now\0" No_argument "n"
  176. "pidfile\0" Required_argument "p"
  177. "quit\0" No_argument "q"
  178. "release\0" No_argument "R"
  179. "request\0" Required_argument "r"
  180. "script\0" Required_argument "s"
  181. "timeout\0" Required_argument "T"
  182. "version\0" No_argument "v"
  183. "retries\0" Required_argument "t"
  184. "tryagain\0" Required_argument "A"
  185. "syslog\0" No_argument "S"
  186. #if ENABLE_FEATURE_UDHCPC_ARPING
  187. "arping\0" No_argument "a"
  188. #endif
  189. "request-option\0" Required_argument "O"
  190. #if ENABLE_FEATURE_UDHCP_PORT
  191. "client-port\0" Required_argument "P"
  192. #endif
  193. ;
  194. #endif
  195. /* Default options. */
  196. #if ENABLE_FEATURE_UDHCP_PORT
  197. SERVER_PORT = 67;
  198. CLIENT_PORT = 68;
  199. #endif
  200. client_config.interface = "eth0";
  201. client_config.script = DEFAULT_SCRIPT;
  202. /* Parse command line */
  203. /* Cc: mutually exclusive; O: list; -T,-t,-A take numeric param */
  204. opt_complementary = "c--C:C--c:O::T+:t+:A+";
  205. #if ENABLE_GETOPT_LONG
  206. applet_long_options = udhcpc_longopts;
  207. #endif
  208. opt = getopt32(argv, "c:CV:fbH:h:F:i:np:qRr:s:T:t:vSA:"
  209. USE_FEATURE_UDHCPC_ARPING("aW:")
  210. USE_FEATURE_UDHCP_PORT("P:")
  211. "O:"
  212. , &str_c, &str_V, &str_h, &str_h, &str_F
  213. , &client_config.interface, &client_config.pidfile, &str_r
  214. , &client_config.script
  215. , &discover_timeout, &discover_retries, &tryagain_timeout
  216. USE_FEATURE_UDHCPC_ARPING(, &str_W)
  217. USE_FEATURE_UDHCP_PORT(, &str_P)
  218. , &list_O
  219. );
  220. if (opt & OPT_c)
  221. client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, str_c, 0);
  222. //if (opt & OPT_C)
  223. if (opt & OPT_V)
  224. client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, str_V, 0);
  225. if (opt & OPT_f)
  226. client_config.foreground = 1;
  227. if (opt & OPT_b)
  228. client_config.background_if_no_lease = 1;
  229. if (opt & (OPT_h|OPT_H))
  230. client_config.hostname = alloc_dhcp_option(DHCP_HOST_NAME, str_h, 0);
  231. if (opt & OPT_F) {
  232. client_config.fqdn = alloc_dhcp_option(DHCP_FQDN, str_F, 3);
  233. /* Flags: 0000NEOS
  234. S: 1 => Client requests Server to update A RR in DNS as well as PTR
  235. O: 1 => Server indicates to client that DNS has been updated regardless
  236. E: 1 => Name data is DNS format, i.e. <4>host<6>domain<4>com<0> not "host.domain.com"
  237. N: 1 => Client requests Server to not update DNS
  238. */
  239. client_config.fqdn[OPT_DATA + 0] = 0x1;
  240. /* client_config.fqdn[OPT_DATA + 1] = 0; - redundant */
  241. /* client_config.fqdn[OPT_DATA + 2] = 0; - redundant */
  242. }
  243. // if (opt & OPT_i) client_config.interface = ...
  244. if (opt & OPT_n)
  245. client_config.abort_if_no_lease = 1;
  246. // if (opt & OPT_p) client_config.pidfile = ...
  247. if (opt & OPT_q)
  248. client_config.quit_after_lease = 1;
  249. if (opt & OPT_R)
  250. client_config.release_on_quit = 1;
  251. if (opt & OPT_r)
  252. requested_ip = inet_addr(str_r);
  253. // if (opt & OPT_s) client_config.script = ...
  254. // if (opt & OPT_T) discover_timeout = xatoi_u(str_T);
  255. // if (opt & OPT_t) discover_retries = xatoi_u(str_t);
  256. // if (opt & OPT_A) tryagain_timeout = xatoi_u(str_A);
  257. if (opt & OPT_v) {
  258. puts("version "BB_VER);
  259. return 0;
  260. }
  261. if (opt & OPT_S) {
  262. openlog(applet_name, LOG_PID, LOG_LOCAL0);
  263. logmode |= LOGMODE_SYSLOG;
  264. }
  265. #if ENABLE_FEATURE_UDHCP_PORT
  266. if (opt & OPT_P) {
  267. CLIENT_PORT = xatou16(str_P);
  268. SERVER_PORT = CLIENT_PORT - 1;
  269. }
  270. #endif
  271. while (list_O) {
  272. int n = index_in_strings(dhcp_option_strings, list_O->data);
  273. if (n < 0)
  274. bb_error_msg_and_die("unknown option '%s'", list_O->data);
  275. n = dhcp_options[n].code;
  276. client_config.opt_mask[n >> 3] |= 1 << (n & 7);
  277. list_O = list_O->link;
  278. }
  279. if (read_interface(client_config.interface, &client_config.ifindex,
  280. NULL, client_config.arp))
  281. return 1;
  282. /* Make sure fd 0,1,2 are open */
  283. bb_sanitize_stdio();
  284. /* Equivalent of doing a fflush after every \n */
  285. setlinebuf(stdout);
  286. /* Create pidfile */
  287. write_pidfile(client_config.pidfile);
  288. /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
  289. /* Goes to stdout and possibly syslog */
  290. bb_info_msg("%s (v"BB_VER") started", applet_name);
  291. /* if not set, and not suppressed, setup the default client ID */
  292. if (!client_config.clientid && !(opt & OPT_C)) {
  293. client_config.clientid = alloc_dhcp_option(DHCP_CLIENT_ID, "", 7);
  294. client_config.clientid[OPT_DATA] = 1;
  295. memcpy(client_config.clientid + OPT_DATA+1, client_config.arp, 6);
  296. }
  297. if (!client_config.vendorclass)
  298. client_config.vendorclass = alloc_dhcp_option(DHCP_VENDOR, "udhcp "BB_VER, 0);
  299. /* setup the signal pipe */
  300. udhcp_sp_setup();
  301. state = INIT_SELECTING;
  302. udhcp_run_script(NULL, "deconfig");
  303. change_listen_mode(LISTEN_RAW);
  304. packet_num = 0;
  305. /* Main event loop. select() waits on signal pipe and possibly
  306. * on sockfd.
  307. * "continue" statements in code below jump to the top of the loop.
  308. */
  309. for (;;) {
  310. tv.tv_sec = timeout;
  311. tv.tv_usec = 0;
  312. if (listen_mode != LISTEN_NONE && sockfd < 0) {
  313. if (listen_mode == LISTEN_KERNEL)
  314. sockfd = listen_socket(/*INADDR_ANY,*/ CLIENT_PORT, client_config.interface);
  315. else
  316. sockfd = raw_socket(client_config.ifindex);
  317. }
  318. max_fd = udhcp_sp_fd_set(&rfds, sockfd);
  319. retval = 0; /* If we already timed out, fall through, else... */
  320. if (tv.tv_sec > 0) {
  321. DEBUG("Waiting on select...");
  322. retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
  323. }
  324. if (retval < 0) {
  325. /* EINTR? signal was caught, don't panic */
  326. if (errno != EINTR) {
  327. /* Else: an error occured, panic! */
  328. bb_perror_msg_and_die("select");
  329. }
  330. continue;
  331. }
  332. /* If timeout dropped to zero, time to become active:
  333. * resend discover/renew/whatever
  334. */
  335. if (retval == 0) {
  336. switch (state) {
  337. case INIT_SELECTING:
  338. if (packet_num < discover_retries) {
  339. if (packet_num == 0)
  340. xid = random_xid();
  341. /* send discover packet */
  342. send_discover(xid, requested_ip); /* broadcast */
  343. timeout = discover_timeout;
  344. packet_num++;
  345. continue;
  346. }
  347. udhcp_run_script(NULL, "leasefail");
  348. if (client_config.background_if_no_lease) {
  349. bb_info_msg("No lease, forking to background");
  350. client_background();
  351. } else if (client_config.abort_if_no_lease) {
  352. bb_info_msg("No lease, failing");
  353. retval = 1;
  354. goto ret;
  355. }
  356. /* wait to try again */
  357. timeout = tryagain_timeout;
  358. packet_num = 0;
  359. continue;
  360. case RENEW_REQUESTED:
  361. case REQUESTING:
  362. if (packet_num < discover_retries) {
  363. /* send request packet */
  364. if (state == RENEW_REQUESTED)
  365. send_renew(xid, server_addr, requested_ip); /* unicast */
  366. else send_selecting(xid, server_addr, requested_ip); /* broadcast */
  367. timeout = ((packet_num == 2) ? 10 : 2);
  368. packet_num++;
  369. continue;
  370. }
  371. /* timed out, go back to init state */
  372. if (state == RENEW_REQUESTED)
  373. udhcp_run_script(NULL, "deconfig");
  374. change_listen_mode(LISTEN_RAW);
  375. state = INIT_SELECTING;
  376. timeout = 0;
  377. packet_num = 0;
  378. continue;
  379. case BOUND:
  380. /* Lease is starting to run out, time to enter renewing state */
  381. change_listen_mode(LISTEN_KERNEL);
  382. DEBUG("Entering renew state");
  383. state = RENEWING;
  384. /* fall right through */
  385. case RENEWING:
  386. /* Either set a new T1, or enter REBINDING state */
  387. if ((t2 - t1) > (lease_seconds / (4*60*60) + 1)) {
  388. /* send a request packet */
  389. send_renew(xid, server_addr, requested_ip); /* unicast */
  390. t1 += (t2 - t1) / 2;
  391. timeout = t1 - ((int)monotonic_sec() - timestamp_got_lease);
  392. continue;
  393. }
  394. /* Timed out, enter rebinding state */
  395. DEBUG("Entering rebinding state");
  396. state = REBINDING;
  397. timeout = (t2 - t1);
  398. continue;
  399. case REBINDING:
  400. /* Lease is *really* about to run out,
  401. * try to find DHCP server using broadcast */
  402. if ((lease_seconds - t2) > (lease_seconds / (4*60*60) + 1)) {
  403. /* send a request packet */
  404. send_renew(xid, 0, requested_ip); /* broadcast */
  405. t2 += (lease_seconds - t2) / 2;
  406. timeout = t2 - ((int)monotonic_sec() - timestamp_got_lease);
  407. continue;
  408. }
  409. /* Timed out, enter init state */
  410. bb_info_msg("Lease lost, entering init state");
  411. udhcp_run_script(NULL, "deconfig");
  412. change_listen_mode(LISTEN_RAW);
  413. state = INIT_SELECTING;
  414. timeout = 0;
  415. packet_num = 0;
  416. continue;
  417. /* case RELEASED: */
  418. }
  419. /* yah, I know, *you* say it would never happen */
  420. timeout = INT_MAX;
  421. continue; /* back to main loop */
  422. }
  423. /* select() didn't timeout, something did happen. */
  424. /* Is is a packet? */
  425. if (listen_mode != LISTEN_NONE && FD_ISSET(sockfd, &rfds)) {
  426. /* A packet is ready, read it */
  427. if (listen_mode == LISTEN_KERNEL)
  428. len = udhcp_recv_packet(&packet, sockfd);
  429. else
  430. len = get_raw_packet(&packet, sockfd);
  431. if (len == -1) { /* error is severe, reopen socket */
  432. DEBUG("error on read, %s, reopening socket", strerror(errno));
  433. change_listen_mode(listen_mode); /* just close and reopen */
  434. }
  435. if (len < 0) continue;
  436. if (packet.xid != xid) {
  437. DEBUG("Ignoring XID %x (our xid is %x)",
  438. (unsigned)packet.xid, (unsigned)xid);
  439. continue;
  440. }
  441. /* Ignore packets that aren't for us */
  442. if (memcmp(packet.chaddr, client_config.arp, 6)) {
  443. DEBUG("Packet does not have our chaddr - ignoring");
  444. continue;
  445. }
  446. message = get_option(&packet, DHCP_MESSAGE_TYPE);
  447. if (message == NULL) {
  448. bb_error_msg("cannot get message type from packet - ignoring");
  449. continue;
  450. }
  451. switch (state) {
  452. case INIT_SELECTING:
  453. /* Must be a DHCPOFFER to one of our xid's */
  454. if (*message == DHCPOFFER) {
  455. /* TODO: why we don't just fetch server's IP from IP header? */
  456. temp = get_option(&packet, DHCP_SERVER_ID);
  457. if (!temp) {
  458. bb_error_msg("no server ID in message");
  459. continue;
  460. /* still selecting - this server looks bad */
  461. }
  462. /* can be misaligned, thus memcpy */
  463. memcpy(&server_addr, temp, 4);
  464. xid = packet.xid;
  465. requested_ip = packet.yiaddr;
  466. /* enter requesting state */
  467. state = REQUESTING;
  468. timeout = 0;
  469. packet_num = 0;
  470. }
  471. continue;
  472. case RENEW_REQUESTED:
  473. case REQUESTING:
  474. case RENEWING:
  475. case REBINDING:
  476. if (*message == DHCPACK) {
  477. temp = get_option(&packet, DHCP_LEASE_TIME);
  478. if (!temp) {
  479. bb_error_msg("no lease time with ACK, using 1 hour lease");
  480. lease_seconds = 60 * 60;
  481. } else {
  482. /* can be misaligned, thus memcpy */
  483. memcpy(&lease_seconds, temp, 4);
  484. lease_seconds = ntohl(lease_seconds);
  485. }
  486. #if ENABLE_FEATURE_UDHCPC_ARPING
  487. if (opt & OPT_a) {
  488. if (!arpping(packet.yiaddr,
  489. (uint32_t) 0,
  490. client_config.arp,
  491. client_config.interface)
  492. ) {
  493. bb_info_msg("offered address is in use "
  494. "(got ARP reply), declining");
  495. send_decline(xid, server_addr, packet.yiaddr);
  496. if (state != REQUESTING)
  497. udhcp_run_script(NULL, "deconfig");
  498. change_listen_mode(LISTEN_RAW);
  499. state = INIT_SELECTING;
  500. requested_ip = 0;
  501. timeout = tryagain_timeout;
  502. packet_num = 0;
  503. continue; /* back to main loop */
  504. }
  505. }
  506. #endif
  507. /* enter bound state */
  508. t1 = lease_seconds / 2;
  509. /* little fixed point for n * .875 */
  510. t2 = (lease_seconds * 7) >> 3;
  511. temp_addr.s_addr = packet.yiaddr;
  512. bb_info_msg("Lease of %s obtained, lease time %u",
  513. inet_ntoa(temp_addr), (unsigned)lease_seconds);
  514. timestamp_got_lease = monotonic_sec();
  515. timeout = t1;
  516. requested_ip = packet.yiaddr;
  517. udhcp_run_script(&packet,
  518. ((state == RENEWING || state == REBINDING) ? "renew" : "bound"));
  519. state = BOUND;
  520. change_listen_mode(LISTEN_NONE);
  521. if (client_config.quit_after_lease) {
  522. if (client_config.release_on_quit)
  523. perform_release();
  524. goto ret0;
  525. }
  526. if (!client_config.foreground)
  527. client_background();
  528. continue; /* back to main loop */
  529. }
  530. if (*message == DHCPNAK) {
  531. /* return to init state */
  532. bb_info_msg("Received DHCP NAK");
  533. udhcp_run_script(&packet, "nak");
  534. if (state != REQUESTING)
  535. udhcp_run_script(NULL, "deconfig");
  536. change_listen_mode(LISTEN_RAW);
  537. sleep(3); /* avoid excessive network traffic */
  538. state = INIT_SELECTING;
  539. requested_ip = 0;
  540. timeout = 0;
  541. packet_num = 0;
  542. }
  543. continue;
  544. /* case BOUND, RELEASED: - ignore all packets */
  545. }
  546. continue; /* back to main loop */
  547. }
  548. /* select() didn't timeout, something did happen.
  549. * But it wasn't a packet. It's a signal pipe then. */
  550. {
  551. int signo = udhcp_sp_read(&rfds);
  552. switch (signo) {
  553. case SIGUSR1:
  554. perform_renew();
  555. /* start things over */
  556. packet_num = 0;
  557. /* Kill any timeouts because the user wants this to hurry along */
  558. timeout = 0;
  559. break;
  560. case SIGUSR2:
  561. perform_release();
  562. break;
  563. case SIGTERM:
  564. bb_info_msg("Received SIGTERM");
  565. if (client_config.release_on_quit)
  566. perform_release();
  567. goto ret0;
  568. }
  569. }
  570. } /* for (;;) - main loop ends */
  571. ret0:
  572. retval = 0;
  573. ret:
  574. /*if (client_config.pidfile) - remove_pidfile has it's own check */
  575. remove_pidfile(client_config.pidfile);
  576. return retval;
  577. }