ifupdown.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ifupdown for busybox
  4. * Copyright (c) 2002 Glenn McGrath
  5. * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Based on ifupdown v 0.6.4 by Anthony Towns
  8. * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
  9. *
  10. * Changes to upstream version
  11. * Remove checks for kernel version, assume kernel version 2.2.0 or better.
  12. * Lines in the interfaces file cannot wrap.
  13. * To adhere to the FHS, the default state file is /var/run/ifstate
  14. * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build
  15. * configuration.
  16. *
  17. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  18. */
  19. #include <sys/utsname.h>
  20. #include <fnmatch.h>
  21. #include "libbb.h"
  22. #define MAX_OPT_DEPTH 10
  23. #define EUNBALBRACK 10001
  24. #define EUNDEFVAR 10002
  25. #define EUNBALPER 10000
  26. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  27. #define MAX_INTERFACE_LENGTH 10
  28. #endif
  29. #define debug_noise(args...) /*fprintf(stderr, args)*/
  30. /* Forward declaration */
  31. struct interface_defn_t;
  32. typedef int execfn(char *command);
  33. struct method_t {
  34. const char *name;
  35. int (*up)(struct interface_defn_t *ifd, execfn *e);
  36. int (*down)(struct interface_defn_t *ifd, execfn *e);
  37. };
  38. struct address_family_t {
  39. const char *name;
  40. int n_methods;
  41. const struct method_t *method;
  42. };
  43. struct mapping_defn_t {
  44. struct mapping_defn_t *next;
  45. int max_matches;
  46. int n_matches;
  47. char **match;
  48. char *script;
  49. int max_mappings;
  50. int n_mappings;
  51. char **mapping;
  52. };
  53. struct variable_t {
  54. char *name;
  55. char *value;
  56. };
  57. struct interface_defn_t {
  58. const struct address_family_t *address_family;
  59. const struct method_t *method;
  60. char *iface;
  61. int max_options;
  62. int n_options;
  63. struct variable_t *option;
  64. };
  65. struct interfaces_file_t {
  66. llist_t *autointerfaces;
  67. llist_t *ifaces;
  68. struct mapping_defn_t *mappings;
  69. };
  70. #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
  71. enum {
  72. OPT_do_all = 0x1,
  73. OPT_no_act = 0x2,
  74. OPT_verbose = 0x4,
  75. OPT_force = 0x8,
  76. OPT_no_mappings = 0x10,
  77. };
  78. #define DO_ALL (option_mask32 & OPT_do_all)
  79. #define NO_ACT (option_mask32 & OPT_no_act)
  80. #define VERBOSE (option_mask32 & OPT_verbose)
  81. #define FORCE (option_mask32 & OPT_force)
  82. #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
  83. static char **my_environ;
  84. static const char *startup_PATH;
  85. #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
  86. static void addstr(char **bufp, const char *str, size_t str_length)
  87. {
  88. /* xasprintf trick will be smaller, but we are often
  89. * called with str_length == 1 - don't want to have
  90. * THAT much of malloc/freeing! */
  91. char *buf = *bufp;
  92. int len = (buf ? strlen(buf) : 0);
  93. str_length++;
  94. buf = xrealloc(buf, len + str_length);
  95. /* copies at most str_length-1 chars! */
  96. safe_strncpy(buf + len, str, str_length);
  97. *bufp = buf;
  98. }
  99. static int strncmpz(const char *l, const char *r, size_t llen)
  100. {
  101. int i = strncmp(l, r, llen);
  102. if (i == 0)
  103. return -r[llen];
  104. return i;
  105. }
  106. static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
  107. {
  108. int i;
  109. if (strncmpz(id, "iface", idlen) == 0) {
  110. static char *label_buf;
  111. //char *result;
  112. free(label_buf);
  113. label_buf = xstrdup(ifd->iface);
  114. // Remove virtual iface suffix - why?
  115. // ubuntu's ifup doesn't do this
  116. //result = strchrnul(label_buf, ':');
  117. //*result = '\0';
  118. return label_buf;
  119. }
  120. if (strncmpz(id, "label", idlen) == 0) {
  121. return ifd->iface;
  122. }
  123. for (i = 0; i < ifd->n_options; i++) {
  124. if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
  125. return ifd->option[i].value;
  126. }
  127. }
  128. return NULL;
  129. }
  130. #if ENABLE_FEATURE_IFUPDOWN_IP
  131. static int count_netmask_bits(const char *dotted_quad)
  132. {
  133. // int result;
  134. // unsigned a, b, c, d;
  135. // /* Found a netmask... Check if it is dotted quad */
  136. // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  137. // return -1;
  138. // if ((a|b|c|d) >> 8)
  139. // return -1; /* one of numbers is >= 256 */
  140. // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
  141. // d = ~d; /* 11110000 -> 00001111 */
  142. /* Shorter version */
  143. int result;
  144. struct in_addr ip;
  145. unsigned d;
  146. if (inet_aton(dotted_quad, &ip) == 0)
  147. return -1; /* malformed dotted IP */
  148. d = ntohl(ip.s_addr); /* IP in host order */
  149. d = ~d; /* 11110000 -> 00001111 */
  150. if (d & (d+1)) /* check that it is in 00001111 form */
  151. return -1; /* no it is not */
  152. result = 32;
  153. while (d) {
  154. d >>= 1;
  155. result--;
  156. }
  157. return result;
  158. }
  159. #endif
  160. static char *parse(const char *command, struct interface_defn_t *ifd)
  161. {
  162. size_t old_pos[MAX_OPT_DEPTH] = { 0 };
  163. int okay[MAX_OPT_DEPTH] = { 1 };
  164. int opt_depth = 1;
  165. char *result = NULL;
  166. while (*command) {
  167. switch (*command) {
  168. default:
  169. addstr(&result, command, 1);
  170. command++;
  171. break;
  172. case '\\':
  173. if (command[1]) {
  174. addstr(&result, command + 1, 1);
  175. command += 2;
  176. } else {
  177. addstr(&result, command, 1);
  178. command++;
  179. }
  180. break;
  181. case '[':
  182. if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
  183. old_pos[opt_depth] = result ? strlen(result) : 0;
  184. okay[opt_depth] = 1;
  185. opt_depth++;
  186. command += 2;
  187. } else {
  188. addstr(&result, "[", 1);
  189. command++;
  190. }
  191. break;
  192. case ']':
  193. if (command[1] == ']' && opt_depth > 1) {
  194. opt_depth--;
  195. if (!okay[opt_depth]) {
  196. result[old_pos[opt_depth]] = '\0';
  197. }
  198. command += 2;
  199. } else {
  200. addstr(&result, "]", 1);
  201. command++;
  202. }
  203. break;
  204. case '%':
  205. {
  206. char *nextpercent;
  207. char *varvalue;
  208. command++;
  209. nextpercent = strchr(command, '%');
  210. if (!nextpercent) {
  211. errno = EUNBALPER;
  212. free(result);
  213. return NULL;
  214. }
  215. varvalue = get_var(command, nextpercent - command, ifd);
  216. if (varvalue) {
  217. #if ENABLE_FEATURE_IFUPDOWN_IP
  218. /* "hwaddress <class> <address>":
  219. * unlike ifconfig, ip doesnt want <class>
  220. * (usually "ether" keyword). Skip it. */
  221. if (strncmp(command, "hwaddress", 9) == 0) {
  222. varvalue = skip_whitespace(skip_non_whitespace(varvalue));
  223. }
  224. #endif
  225. addstr(&result, varvalue, strlen(varvalue));
  226. } else {
  227. #if ENABLE_FEATURE_IFUPDOWN_IP
  228. /* Sigh... Add a special case for 'ip' to convert from
  229. * dotted quad to bit count style netmasks. */
  230. if (strncmp(command, "bnmask", 6) == 0) {
  231. unsigned res;
  232. varvalue = get_var("netmask", 7, ifd);
  233. if (varvalue) {
  234. res = count_netmask_bits(varvalue);
  235. if (res > 0) {
  236. const char *argument = utoa(res);
  237. addstr(&result, argument, strlen(argument));
  238. command = nextpercent + 1;
  239. break;
  240. }
  241. }
  242. }
  243. #endif
  244. okay[opt_depth - 1] = 0;
  245. }
  246. command = nextpercent + 1;
  247. }
  248. break;
  249. }
  250. }
  251. if (opt_depth > 1) {
  252. errno = EUNBALBRACK;
  253. free(result);
  254. return NULL;
  255. }
  256. if (!okay[0]) {
  257. errno = EUNDEFVAR;
  258. free(result);
  259. return NULL;
  260. }
  261. return result;
  262. }
  263. /* execute() returns 1 for success and 0 for failure */
  264. static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
  265. {
  266. char *out;
  267. int ret;
  268. out = parse(command, ifd);
  269. if (!out) {
  270. /* parse error? */
  271. return 0;
  272. }
  273. /* out == "": parsed ok but not all needed variables known, skip */
  274. ret = out[0] ? (*exec)(out) : 1;
  275. free(out);
  276. if (ret != 1) {
  277. return 0;
  278. }
  279. return 1;
  280. }
  281. #endif
  282. #if ENABLE_FEATURE_IFUPDOWN_IPV6
  283. static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
  284. {
  285. #if ENABLE_FEATURE_IFUPDOWN_IP
  286. int result;
  287. result = execute("ip addr add ::1 dev %iface%", ifd, exec);
  288. result += execute("ip link set %iface% up", ifd, exec);
  289. return ((result == 2) ? 2 : 0);
  290. #else
  291. return execute("ifconfig %iface% add ::1", ifd, exec);
  292. #endif
  293. }
  294. static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
  295. {
  296. #if ENABLE_FEATURE_IFUPDOWN_IP
  297. return execute("ip link set %iface% down", ifd, exec);
  298. #else
  299. return execute("ifconfig %iface% del ::1", ifd, exec);
  300. #endif
  301. }
  302. static int static_up6(struct interface_defn_t *ifd, execfn *exec)
  303. {
  304. int result;
  305. #if ENABLE_FEATURE_IFUPDOWN_IP
  306. result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
  307. result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
  308. /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
  309. result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
  310. #else
  311. result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
  312. result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
  313. result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
  314. #endif
  315. return ((result == 3) ? 3 : 0);
  316. }
  317. static int static_down6(struct interface_defn_t *ifd, execfn *exec)
  318. {
  319. #if ENABLE_FEATURE_IFUPDOWN_IP
  320. return execute("ip link set %iface% down", ifd, exec);
  321. #else
  322. return execute("ifconfig %iface% down", ifd, exec);
  323. #endif
  324. }
  325. #if ENABLE_FEATURE_IFUPDOWN_IP
  326. static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
  327. {
  328. int result;
  329. result = execute("ip tunnel add %iface% mode sit remote "
  330. "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
  331. result += execute("ip link set %iface% up", ifd, exec);
  332. result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
  333. result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
  334. return ((result == 4) ? 4 : 0);
  335. }
  336. static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
  337. {
  338. return execute("ip tunnel del %iface%", ifd, exec);
  339. }
  340. #endif
  341. static const struct method_t methods6[] = {
  342. #if ENABLE_FEATURE_IFUPDOWN_IP
  343. { "v4tunnel", v4tunnel_up, v4tunnel_down, },
  344. #endif
  345. { "static", static_up6, static_down6, },
  346. { "loopback", loopback_up6, loopback_down6, },
  347. };
  348. static const struct address_family_t addr_inet6 = {
  349. "inet6",
  350. ARRAY_SIZE(methods6),
  351. methods6
  352. };
  353. #endif /* FEATURE_IFUPDOWN_IPV6 */
  354. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  355. static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
  356. {
  357. #if ENABLE_FEATURE_IFUPDOWN_IP
  358. int result;
  359. result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
  360. result += execute("ip link set %iface% up", ifd, exec);
  361. return ((result == 2) ? 2 : 0);
  362. #else
  363. return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
  364. #endif
  365. }
  366. static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
  367. {
  368. #if ENABLE_FEATURE_IFUPDOWN_IP
  369. int result;
  370. result = execute("ip addr flush dev %iface%", ifd, exec);
  371. result += execute("ip link set %iface% down", ifd, exec);
  372. return ((result == 2) ? 2 : 0);
  373. #else
  374. return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
  375. #endif
  376. }
  377. static int static_up(struct interface_defn_t *ifd, execfn *exec)
  378. {
  379. int result;
  380. #if ENABLE_FEATURE_IFUPDOWN_IP
  381. result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
  382. "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
  383. result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
  384. result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
  385. return ((result == 3) ? 3 : 0);
  386. #else
  387. /* ifconfig said to set iface up before it processes hw %hwaddress%,
  388. * which then of course fails. Thus we run two separate ifconfig */
  389. result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
  390. ifd, exec);
  391. result += execute("ifconfig %iface% %address% netmask %netmask%"
  392. "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
  393. ifd, exec);
  394. result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
  395. return ((result == 3) ? 3 : 0);
  396. #endif
  397. }
  398. static int static_down(struct interface_defn_t *ifd, execfn *exec)
  399. {
  400. int result;
  401. #if ENABLE_FEATURE_IFUPDOWN_IP
  402. result = execute("ip addr flush dev %iface%", ifd, exec);
  403. result += execute("ip link set %iface% down", ifd, exec);
  404. #else
  405. /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
  406. /* Bringing the interface down deletes the routes in itself.
  407. Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
  408. result = 1;
  409. result += execute("ifconfig %iface% down", ifd, exec);
  410. #endif
  411. return ((result == 2) ? 2 : 0);
  412. }
  413. #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  414. struct dhcp_client_t
  415. {
  416. const char *name;
  417. const char *startcmd;
  418. const char *stopcmd;
  419. };
  420. static const struct dhcp_client_t ext_dhcp_clients[] = {
  421. { "dhcpcd",
  422. "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
  423. "dhcpcd -k %iface%",
  424. },
  425. { "dhclient",
  426. "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
  427. "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
  428. },
  429. { "pump",
  430. "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
  431. "pump -i %iface% -k",
  432. },
  433. { "udhcpc",
  434. "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
  435. "[[ -s %script%]][[ %udhcpc_opts%]]",
  436. "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
  437. },
  438. };
  439. #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
  440. #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  441. static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  442. {
  443. unsigned i;
  444. #if ENABLE_FEATURE_IFUPDOWN_IP
  445. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  446. if (!execute("ip link set[[ address %hwaddress%]] %iface% up", ifd, exec))
  447. return 0;
  448. #else
  449. /* needed if we have hwaddress on dhcp iface */
  450. if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
  451. return 0;
  452. #endif
  453. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  454. if (exists_execable(ext_dhcp_clients[i].name))
  455. return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
  456. }
  457. bb_error_msg("no dhcp clients found");
  458. return 0;
  459. }
  460. #elif ENABLE_APP_UDHCPC
  461. static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  462. {
  463. #if ENABLE_FEATURE_IFUPDOWN_IP
  464. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  465. if (!execute("ip link set[[ address %hwaddress%]] %iface% up", ifd, exec))
  466. return 0;
  467. #else
  468. /* needed if we have hwaddress on dhcp iface */
  469. if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
  470. return 0;
  471. #endif
  472. return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
  473. "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
  474. ifd, exec);
  475. }
  476. #else
  477. static int dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
  478. execfn *exec UNUSED_PARAM)
  479. {
  480. return 0; /* no dhcp support */
  481. }
  482. #endif
  483. #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  484. static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  485. {
  486. int result = 0;
  487. unsigned i;
  488. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  489. if (exists_execable(ext_dhcp_clients[i].name)) {
  490. result += execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
  491. if (result)
  492. break;
  493. }
  494. }
  495. if (!result)
  496. bb_error_msg("warning: no dhcp clients found and stopped");
  497. /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
  498. and it may come back up because udhcpc is still shutting down */
  499. usleep(100000);
  500. result += static_down(ifd, exec);
  501. return ((result == 3) ? 3 : 0);
  502. }
  503. #elif ENABLE_APP_UDHCPC
  504. static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  505. {
  506. int result;
  507. result = execute("kill "
  508. "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
  509. /* Also bring the hardware interface down since
  510. killing the dhcp client alone doesn't do it.
  511. This enables consecutive ifup->ifdown->ifup */
  512. /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
  513. and it may come back up because udhcpc is still shutting down */
  514. usleep(100000);
  515. result += static_down(ifd, exec);
  516. return ((result == 3) ? 3 : 0);
  517. }
  518. #else
  519. static int dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
  520. execfn *exec UNUSED_PARAM)
  521. {
  522. return 0; /* no dhcp support */
  523. }
  524. #endif
  525. static int manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
  526. {
  527. return 1;
  528. }
  529. static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
  530. {
  531. return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
  532. "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
  533. " --returniffail --serverbcast", ifd, exec);
  534. }
  535. static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
  536. {
  537. return execute("pon[[ %provider%]]", ifd, exec);
  538. }
  539. static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
  540. {
  541. return execute("poff[[ %provider%]]", ifd, exec);
  542. }
  543. static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
  544. {
  545. return execute("start-stop-daemon --start -x wvdial "
  546. "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
  547. }
  548. static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
  549. {
  550. return execute("start-stop-daemon --stop -x wvdial "
  551. "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
  552. }
  553. static const struct method_t methods[] = {
  554. { "manual", manual_up_down, manual_up_down, },
  555. { "wvdial", wvdial_up, wvdial_down, },
  556. { "ppp", ppp_up, ppp_down, },
  557. { "static", static_up, static_down, },
  558. { "bootp", bootp_up, static_down, },
  559. { "dhcp", dhcp_up, dhcp_down, },
  560. { "loopback", loopback_up, loopback_down, },
  561. };
  562. static const struct address_family_t addr_inet = {
  563. "inet",
  564. ARRAY_SIZE(methods),
  565. methods
  566. };
  567. #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
  568. static char *next_word(char **buf)
  569. {
  570. unsigned length;
  571. char *word;
  572. /* Skip over leading whitespace */
  573. word = skip_whitespace(*buf);
  574. /* Stop on EOL */
  575. if (*word == '\0')
  576. return NULL;
  577. /* Find the length of this word (can't be 0) */
  578. length = strcspn(word, " \t\n");
  579. /* Unless we are already at NUL, store NUL and advance */
  580. if (word[length] != '\0')
  581. word[length++] = '\0';
  582. *buf = word + length;
  583. return word;
  584. }
  585. static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
  586. {
  587. int i;
  588. if (!name)
  589. return NULL;
  590. for (i = 0; af[i]; i++) {
  591. if (strcmp(af[i]->name, name) == 0) {
  592. return af[i];
  593. }
  594. }
  595. return NULL;
  596. }
  597. static const struct method_t *get_method(const struct address_family_t *af, char *name)
  598. {
  599. int i;
  600. if (!name)
  601. return NULL;
  602. /* TODO: use index_in_str_array() */
  603. for (i = 0; i < af->n_methods; i++) {
  604. if (strcmp(af->method[i].name, name) == 0) {
  605. return &af->method[i];
  606. }
  607. }
  608. return NULL;
  609. }
  610. static const llist_t *find_list_string(const llist_t *list, const char *string)
  611. {
  612. if (string == NULL)
  613. return NULL;
  614. while (list) {
  615. if (strcmp(list->data, string) == 0) {
  616. return list;
  617. }
  618. list = list->link;
  619. }
  620. return NULL;
  621. }
  622. static struct interfaces_file_t *read_interfaces(const char *filename)
  623. {
  624. /* Let's try to be compatible.
  625. *
  626. * "man 5 interfaces" says:
  627. * Lines starting with "#" are ignored. Note that end-of-line
  628. * comments are NOT supported, comments must be on a line of their own.
  629. * A line may be extended across multiple lines by making
  630. * the last character a backslash.
  631. *
  632. * Seen elsewhere in example config file:
  633. * A first non-blank "#" character makes the rest of the line
  634. * be ignored. Blank lines are ignored. Lines may be indented freely.
  635. * A "\" character at the very end of the line indicates the next line
  636. * should be treated as a continuation of the current one.
  637. */
  638. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  639. struct mapping_defn_t *currmap = NULL;
  640. #endif
  641. struct interface_defn_t *currif = NULL;
  642. struct interfaces_file_t *defn;
  643. FILE *f;
  644. char *buf;
  645. char *first_word;
  646. char *rest_of_line;
  647. enum { NONE, IFACE, MAPPING } currently_processing = NONE;
  648. defn = xzalloc(sizeof(*defn));
  649. f = xfopen_for_read(filename);
  650. while ((buf = xmalloc_fgetline(f)) != NULL) {
  651. #if ENABLE_DESKTOP
  652. /* Trailing "\" concatenates lines */
  653. char *p;
  654. while ((p = last_char_is(buf, '\\')) != NULL) {
  655. *p = '\0';
  656. rest_of_line = xmalloc_fgetline(f);
  657. if (!rest_of_line)
  658. break;
  659. p = xasprintf("%s%s", buf, rest_of_line);
  660. free(buf);
  661. free(rest_of_line);
  662. buf = p;
  663. }
  664. #endif
  665. rest_of_line = buf;
  666. first_word = next_word(&rest_of_line);
  667. if (!first_word || *first_word == '#') {
  668. free(buf);
  669. continue; /* blank/comment line */
  670. }
  671. if (strcmp(first_word, "mapping") == 0) {
  672. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  673. currmap = xzalloc(sizeof(*currmap));
  674. while ((first_word = next_word(&rest_of_line)) != NULL) {
  675. currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
  676. currmap->match[currmap->n_matches++] = xstrdup(first_word);
  677. }
  678. /*currmap->max_mappings = 0; - done by xzalloc */
  679. /*currmap->n_mappings = 0;*/
  680. /*currmap->mapping = NULL;*/
  681. /*currmap->script = NULL;*/
  682. {
  683. struct mapping_defn_t **where = &defn->mappings;
  684. while (*where != NULL) {
  685. where = &(*where)->next;
  686. }
  687. *where = currmap;
  688. /*currmap->next = NULL;*/
  689. }
  690. debug_noise("Added mapping\n");
  691. #endif
  692. currently_processing = MAPPING;
  693. } else if (strcmp(first_word, "iface") == 0) {
  694. static const struct address_family_t *const addr_fams[] = {
  695. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  696. &addr_inet,
  697. #endif
  698. #if ENABLE_FEATURE_IFUPDOWN_IPV6
  699. &addr_inet6,
  700. #endif
  701. NULL
  702. };
  703. char *iface_name;
  704. char *address_family_name;
  705. char *method_name;
  706. llist_t *iface_list;
  707. currif = xzalloc(sizeof(*currif));
  708. iface_name = next_word(&rest_of_line);
  709. address_family_name = next_word(&rest_of_line);
  710. method_name = next_word(&rest_of_line);
  711. if (method_name == NULL)
  712. bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
  713. /* ship any trailing whitespace */
  714. rest_of_line = skip_whitespace(rest_of_line);
  715. if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
  716. bb_error_msg_and_die("too many parameters \"%s\"", buf);
  717. currif->iface = xstrdup(iface_name);
  718. currif->address_family = get_address_family(addr_fams, address_family_name);
  719. if (!currif->address_family)
  720. bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
  721. currif->method = get_method(currif->address_family, method_name);
  722. if (!currif->method)
  723. bb_error_msg_and_die("unknown method \"%s\"", method_name);
  724. for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
  725. struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
  726. if ((strcmp(tmp->iface, currif->iface) == 0)
  727. && (tmp->address_family == currif->address_family)
  728. ) {
  729. bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
  730. }
  731. }
  732. llist_add_to_end(&(defn->ifaces), (char*)currif);
  733. debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
  734. currently_processing = IFACE;
  735. } else if (strcmp(first_word, "auto") == 0) {
  736. while ((first_word = next_word(&rest_of_line)) != NULL) {
  737. /* Check the interface isnt already listed */
  738. if (find_list_string(defn->autointerfaces, first_word)) {
  739. bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
  740. }
  741. /* Add the interface to the list */
  742. llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
  743. debug_noise("\nauto %s\n", first_word);
  744. }
  745. currently_processing = NONE;
  746. } else {
  747. switch (currently_processing) {
  748. case IFACE:
  749. if (rest_of_line[0] == '\0')
  750. bb_error_msg_and_die("option with empty value \"%s\"", buf);
  751. if (strcmp(first_word, "up") != 0
  752. && strcmp(first_word, "down") != 0
  753. && strcmp(first_word, "pre-up") != 0
  754. && strcmp(first_word, "post-down") != 0
  755. ) {
  756. int i;
  757. for (i = 0; i < currif->n_options; i++) {
  758. if (strcmp(currif->option[i].name, first_word) == 0)
  759. bb_error_msg_and_die("duplicate option \"%s\"", buf);
  760. }
  761. }
  762. if (currif->n_options >= currif->max_options) {
  763. currif->max_options += 10;
  764. currif->option = xrealloc(currif->option,
  765. sizeof(*currif->option) * currif->max_options);
  766. }
  767. debug_noise("\t%s=%s\n", first_word, rest_of_line);
  768. currif->option[currif->n_options].name = xstrdup(first_word);
  769. currif->option[currif->n_options].value = xstrdup(rest_of_line);
  770. currif->n_options++;
  771. break;
  772. case MAPPING:
  773. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  774. if (strcmp(first_word, "script") == 0) {
  775. if (currmap->script != NULL)
  776. bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
  777. currmap->script = xstrdup(next_word(&rest_of_line));
  778. } else if (strcmp(first_word, "map") == 0) {
  779. if (currmap->n_mappings >= currmap->max_mappings) {
  780. currmap->max_mappings = currmap->max_mappings * 2 + 1;
  781. currmap->mapping = xrealloc(currmap->mapping,
  782. sizeof(char *) * currmap->max_mappings);
  783. }
  784. currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
  785. currmap->n_mappings++;
  786. } else {
  787. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  788. }
  789. #endif
  790. break;
  791. case NONE:
  792. default:
  793. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  794. }
  795. }
  796. free(buf);
  797. } /* while (fgets) */
  798. if (ferror(f) != 0) {
  799. /* ferror does NOT set errno! */
  800. bb_error_msg_and_die("%s: I/O error", filename);
  801. }
  802. fclose(f);
  803. return defn;
  804. }
  805. static char *setlocalenv(const char *format, const char *name, const char *value)
  806. {
  807. char *result;
  808. char *here;
  809. char *there;
  810. result = xasprintf(format, name, value);
  811. for (here = there = result; *there != '=' && *there; there++) {
  812. if (*there == '-')
  813. *there = '_';
  814. if (isalpha(*there))
  815. *there = toupper(*there);
  816. if (isalnum(*there) || *there == '_') {
  817. *here = *there;
  818. here++;
  819. }
  820. }
  821. memmove(here, there, strlen(there) + 1);
  822. return result;
  823. }
  824. static void set_environ(struct interface_defn_t *iface, const char *mode)
  825. {
  826. char **environend;
  827. int i;
  828. const int n_env_entries = iface->n_options + 5;
  829. char **ppch;
  830. if (my_environ != NULL) {
  831. for (ppch = my_environ; *ppch; ppch++) {
  832. free(*ppch);
  833. *ppch = NULL;
  834. }
  835. free(my_environ);
  836. }
  837. my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
  838. environend = my_environ;
  839. for (i = 0; i < iface->n_options; i++) {
  840. if (strcmp(iface->option[i].name, "up") == 0
  841. || strcmp(iface->option[i].name, "down") == 0
  842. || strcmp(iface->option[i].name, "pre-up") == 0
  843. || strcmp(iface->option[i].name, "post-down") == 0
  844. ) {
  845. continue;
  846. }
  847. *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
  848. }
  849. *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
  850. *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
  851. *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
  852. *(environend++) = setlocalenv("%s=%s", "MODE", mode);
  853. *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
  854. }
  855. static int doit(char *str)
  856. {
  857. if (option_mask32 & (OPT_no_act|OPT_verbose)) {
  858. puts(str);
  859. }
  860. if (!(option_mask32 & OPT_no_act)) {
  861. pid_t child;
  862. int status;
  863. fflush(NULL);
  864. child = vfork();
  865. switch (child) {
  866. case -1: /* failure */
  867. return 0;
  868. case 0: /* child */
  869. execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
  870. _exit(127);
  871. }
  872. safe_waitpid(child, &status, 0);
  873. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  874. return 0;
  875. }
  876. }
  877. return 1;
  878. }
  879. static int execute_all(struct interface_defn_t *ifd, const char *opt)
  880. {
  881. int i;
  882. char *buf;
  883. for (i = 0; i < ifd->n_options; i++) {
  884. if (strcmp(ifd->option[i].name, opt) == 0) {
  885. if (!doit(ifd->option[i].value)) {
  886. return 0;
  887. }
  888. }
  889. }
  890. buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
  891. /* heh, we don't bother free'ing it */
  892. return doit(buf);
  893. }
  894. static int check(char *str)
  895. {
  896. return str != NULL;
  897. }
  898. static int iface_up(struct interface_defn_t *iface)
  899. {
  900. if (!iface->method->up(iface, check)) return -1;
  901. set_environ(iface, "start");
  902. if (!execute_all(iface, "pre-up")) return 0;
  903. if (!iface->method->up(iface, doit)) return 0;
  904. if (!execute_all(iface, "up")) return 0;
  905. return 1;
  906. }
  907. static int iface_down(struct interface_defn_t *iface)
  908. {
  909. if (!iface->method->down(iface,check)) return -1;
  910. set_environ(iface, "stop");
  911. if (!execute_all(iface, "down")) return 0;
  912. if (!iface->method->down(iface, doit)) return 0;
  913. if (!execute_all(iface, "post-down")) return 0;
  914. return 1;
  915. }
  916. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  917. static int popen2(FILE **in, FILE **out, char *command, char *param)
  918. {
  919. char *argv[3] = { command, param, NULL };
  920. struct fd_pair infd, outfd;
  921. pid_t pid;
  922. xpiped_pair(infd);
  923. xpiped_pair(outfd);
  924. fflush(NULL);
  925. pid = vfork();
  926. switch (pid) {
  927. case -1: /* failure */
  928. bb_perror_msg_and_die("vfork");
  929. case 0: /* child */
  930. /* NB: close _first_, then move fds! */
  931. close(infd.wr);
  932. close(outfd.rd);
  933. xmove_fd(infd.rd, 0);
  934. xmove_fd(outfd.wr, 1);
  935. BB_EXECVP(command, argv);
  936. _exit(127);
  937. }
  938. /* parent */
  939. close(infd.rd);
  940. close(outfd.wr);
  941. *in = fdopen(infd.wr, "w");
  942. *out = fdopen(outfd.rd, "r");
  943. return pid;
  944. }
  945. static char *run_mapping(char *physical, struct mapping_defn_t *map)
  946. {
  947. FILE *in, *out;
  948. int i, status;
  949. pid_t pid;
  950. char *logical = xstrdup(physical);
  951. /* Run the mapping script. Never fails. */
  952. pid = popen2(&in, &out, map->script, physical);
  953. /* Write mappings to stdin of mapping script. */
  954. for (i = 0; i < map->n_mappings; i++) {
  955. fprintf(in, "%s\n", map->mapping[i]);
  956. }
  957. fclose(in);
  958. safe_waitpid(pid, &status, 0);
  959. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  960. /* If the mapping script exited successfully, try to
  961. * grab a line of output and use that as the name of the
  962. * logical interface. */
  963. char *new_logical = xmalloc_fgetline(out);
  964. if (new_logical) {
  965. /* If we are able to read a line of output from the script,
  966. * remove any trailing whitespace and use this value
  967. * as the name of the logical interface. */
  968. char *pch = new_logical + strlen(new_logical) - 1;
  969. while (pch >= new_logical && isspace(*pch))
  970. *(pch--) = '\0';
  971. free(logical);
  972. logical = new_logical;
  973. }
  974. }
  975. fclose(out);
  976. return logical;
  977. }
  978. #endif /* FEATURE_IFUPDOWN_MAPPING */
  979. static llist_t *find_iface_state(llist_t *state_list, const char *iface)
  980. {
  981. unsigned iface_len = strlen(iface);
  982. llist_t *search = state_list;
  983. while (search) {
  984. if ((strncmp(search->data, iface, iface_len) == 0)
  985. && (search->data[iface_len] == '=')
  986. ) {
  987. return search;
  988. }
  989. search = search->link;
  990. }
  991. return NULL;
  992. }
  993. /* read the previous state from the state file */
  994. static llist_t *read_iface_state(void)
  995. {
  996. llist_t *state_list = NULL;
  997. FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
  998. if (state_fp) {
  999. char *start, *end_ptr;
  1000. while ((start = xmalloc_fgets(state_fp)) != NULL) {
  1001. /* We should only need to check for a single character */
  1002. end_ptr = start + strcspn(start, " \t\n");
  1003. *end_ptr = '\0';
  1004. llist_add_to(&state_list, start);
  1005. }
  1006. fclose(state_fp);
  1007. }
  1008. return state_list;
  1009. }
  1010. int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1011. int ifupdown_main(int argc, char **argv)
  1012. {
  1013. int (*cmds)(struct interface_defn_t *);
  1014. struct interfaces_file_t *defn;
  1015. llist_t *target_list = NULL;
  1016. const char *interfaces = "/etc/network/interfaces";
  1017. bool any_failures = 0;
  1018. cmds = iface_down;
  1019. if (applet_name[2] == 'u') {
  1020. /* ifup command */
  1021. cmds = iface_up;
  1022. }
  1023. getopt32(argv, OPTION_STR, &interfaces);
  1024. if (argc - optind > 0) {
  1025. if (DO_ALL) bb_show_usage();
  1026. } else {
  1027. if (!DO_ALL) bb_show_usage();
  1028. }
  1029. debug_noise("reading %s file:\n", interfaces);
  1030. defn = read_interfaces(interfaces);
  1031. debug_noise("\ndone reading %s\n\n", interfaces);
  1032. startup_PATH = getenv("PATH");
  1033. if (!startup_PATH) startup_PATH = "";
  1034. /* Create a list of interfaces to work on */
  1035. if (DO_ALL) {
  1036. target_list = defn->autointerfaces;
  1037. } else {
  1038. llist_add_to_end(&target_list, argv[optind]);
  1039. }
  1040. /* Update the interfaces */
  1041. while (target_list) {
  1042. llist_t *iface_list;
  1043. struct interface_defn_t *currif;
  1044. char *iface;
  1045. char *liface;
  1046. char *pch;
  1047. bool okay = 0;
  1048. int cmds_ret;
  1049. iface = xstrdup(target_list->data);
  1050. target_list = target_list->link;
  1051. pch = strchr(iface, '=');
  1052. if (pch) {
  1053. *pch = '\0';
  1054. liface = xstrdup(pch + 1);
  1055. } else {
  1056. liface = xstrdup(iface);
  1057. }
  1058. if (!FORCE) {
  1059. llist_t *state_list = read_iface_state();
  1060. const llist_t *iface_state = find_iface_state(state_list, iface);
  1061. if (cmds == iface_up) {
  1062. /* ifup */
  1063. if (iface_state) {
  1064. bb_error_msg("interface %s already configured", iface);
  1065. continue;
  1066. }
  1067. } else {
  1068. /* ifdown */
  1069. if (!iface_state) {
  1070. bb_error_msg("interface %s not configured", iface);
  1071. continue;
  1072. }
  1073. }
  1074. llist_free(state_list, free);
  1075. }
  1076. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  1077. if ((cmds == iface_up) && !NO_MAPPINGS) {
  1078. struct mapping_defn_t *currmap;
  1079. for (currmap = defn->mappings; currmap; currmap = currmap->next) {
  1080. int i;
  1081. for (i = 0; i < currmap->n_matches; i++) {
  1082. if (fnmatch(currmap->match[i], liface, 0) != 0)
  1083. continue;
  1084. if (VERBOSE) {
  1085. printf("Running mapping script %s on %s\n", currmap->script, liface);
  1086. }
  1087. liface = run_mapping(iface, currmap);
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. #endif
  1093. iface_list = defn->ifaces;
  1094. while (iface_list) {
  1095. currif = (struct interface_defn_t *) iface_list->data;
  1096. if (strcmp(liface, currif->iface) == 0) {
  1097. char *oldiface = currif->iface;
  1098. okay = 1;
  1099. currif->iface = iface;
  1100. debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
  1101. /* Call the cmds function pointer, does either iface_up() or iface_down() */
  1102. cmds_ret = cmds(currif);
  1103. if (cmds_ret == -1) {
  1104. bb_error_msg("don't seem to have all the variables for %s/%s",
  1105. liface, currif->address_family->name);
  1106. any_failures = 1;
  1107. } else if (cmds_ret == 0) {
  1108. any_failures = 1;
  1109. }
  1110. currif->iface = oldiface;
  1111. }
  1112. iface_list = iface_list->link;
  1113. }
  1114. if (VERBOSE) {
  1115. bb_putchar('\n');
  1116. }
  1117. if (!okay && !FORCE) {
  1118. bb_error_msg("ignoring unknown interface %s", liface);
  1119. any_failures = 1;
  1120. } else if (!NO_ACT) {
  1121. /* update the state file */
  1122. FILE *state_fp;
  1123. llist_t *state;
  1124. llist_t *state_list = read_iface_state();
  1125. llist_t *iface_state = find_iface_state(state_list, iface);
  1126. if (cmds == iface_up) {
  1127. char * const newiface = xasprintf("%s=%s", iface, liface);
  1128. if (iface_state == NULL) {
  1129. llist_add_to_end(&state_list, newiface);
  1130. } else {
  1131. free(iface_state->data);
  1132. iface_state->data = newiface;
  1133. }
  1134. } else {
  1135. /* Remove an interface from state_list */
  1136. llist_unlink(&state_list, iface_state);
  1137. free(llist_pop(&iface_state));
  1138. }
  1139. /* Actually write the new state */
  1140. state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
  1141. state = state_list;
  1142. while (state) {
  1143. if (state->data) {
  1144. fprintf(state_fp, "%s\n", state->data);
  1145. }
  1146. state = state->link;
  1147. }
  1148. fclose(state_fp);
  1149. llist_free(state_list, free);
  1150. }
  1151. }
  1152. return any_failures;
  1153. }