0002-ifconfig-Improve-the-support-for-GNU-Hurd.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. From 9a90d9b9119906df23cb2db1503cb0f099942dd9 Mon Sep 17 00:00:00 2001
  2. From: Mats Erik Andersson <gnu@gisladisker.se>
  3. Date: Sat, 18 Jul 2015 01:12:41 +0200
  4. Subject: [PATCH 02/60] ifconfig: Improve the support for GNU/Hurd.
  5. Use system specific code instead of generic code.
  6. This provides abilities similar to other systems.
  7. ---
  8. ChangeLog | 17 +++
  9. ifconfig/system.c | 10 +-
  10. ifconfig/system.h | 2 +
  11. ifconfig/system/Makefile.am | 4 +-
  12. ifconfig/system/generic.c | 14 +-
  13. ifconfig/system/hurd.c | 292 ++++++++++++++++++++++++++++++++++++
  14. ifconfig/system/hurd.h | 50 ++++++
  15. 7 files changed, 381 insertions(+), 8 deletions(-)
  16. create mode 100644 ifconfig/system/hurd.c
  17. create mode 100644 ifconfig/system/hurd.h
  18. diff --git a/ChangeLog b/ChangeLog
  19. index 7f799daa..18a584ba 100644
  20. --- a/ChangeLog
  21. +++ b/ChangeLog
  22. @@ -1,3 +1,20 @@
  23. +2015-07-18 Mats Erik Andersson <gnu@gisladisker.se>
  24. +
  25. + ifconfig: Improve the support for GNU/Hurd.
  26. + Add system specific abilities, including parsing of command
  27. + line and hardware addresses.
  28. +
  29. + * ifconfig/system.h [__GNU__]: Include "system/hurd.h".
  30. + * ifconfig/system.c [__GNU__]: Include "system/hurd.c".
  31. + * ifconfig/system/Makefile.am (noinst_HEADERS): Add the
  32. + files `hurd.h' and `hurd.c'.
  33. + * ifconfig/system/hurd.h, ifconfig/system/hurd.c: New files.
  34. +
  35. + * ifconfig/system/generic.c: Include <unused-parameter.h>.
  36. + (system_parse_opt, system_parse_opt_rest, system_configure):
  37. + Apply _GL_UNUSED_PARAMETER to all arguments, as these functions
  38. + are placeholders and no-ops.
  39. +
  40. 2015-07-17 Mats Erik Andersson <gnu@gisladisker.se>
  41. ifconfig: Sporadically appearing regression.
  42. diff --git a/ifconfig/system.c b/ifconfig/system.c
  43. index 30677e41..e108dc2e 100644
  44. --- a/ifconfig/system.c
  45. +++ b/ifconfig/system.c
  46. @@ -25,10 +25,12 @@
  47. # include "system/solaris.c"
  48. #elif defined __QNX__
  49. # include "system/qnx.c"
  50. -# elif defined __DragonFly__ || defined __FreeBSD__ || \
  51. - defined __FreeBSD_kernel__ || \
  52. - defined __NetBSD__ || defined __OpenBSD__
  53. -# include "system/bsd.c"
  54. +#elif defined __DragonFly__ || defined __FreeBSD__ || \
  55. + defined __FreeBSD_kernel__ || \
  56. + defined __NetBSD__ || defined __OpenBSD__
  57. +# include "system/bsd.c"
  58. +#elif defined __GNU__
  59. +# include "system/hurd.c"
  60. #else
  61. # include "system/generic.c"
  62. #endif
  63. diff --git a/ifconfig/system.h b/ifconfig/system.h
  64. index 8521ad95..66878d3a 100644
  65. --- a/ifconfig/system.h
  66. +++ b/ifconfig/system.h
  67. @@ -97,6 +97,8 @@ extern struct if_nameindex* (*system_if_nameindex) (void);
  68. defined __FreeBSD_kernel__ || \
  69. defined __NetBSD__ || defined __OpenBSD__
  70. # include "system/bsd.h"
  71. +# elif defined __GNU__
  72. +# include "system/hurd.h"
  73. # else
  74. # include "system/generic.h"
  75. # endif
  76. diff --git a/ifconfig/system/Makefile.am b/ifconfig/system/Makefile.am
  77. index 954c6774..62a9f1c4 100644
  78. --- a/ifconfig/system/Makefile.am
  79. +++ b/ifconfig/system/Makefile.am
  80. @@ -26,8 +26,10 @@ noinst_HEADERS = \
  81. linux.h \
  82. solaris.h \
  83. qnx.h \
  84. + hurd.h \
  85. bsd.c \
  86. generic.c \
  87. linux.c \
  88. solaris.c \
  89. - qnx.c
  90. + qnx.c \
  91. + hurd.c
  92. diff --git a/ifconfig/system/generic.c b/ifconfig/system/generic.c
  93. index 9a2bda55..20a78bde 100644
  94. --- a/ifconfig/system/generic.c
  95. +++ b/ifconfig/system/generic.c
  96. @@ -22,6 +22,8 @@
  97. #include <config.h>
  98. #include "../ifconfig.h"
  99. +
  100. +#include <unused-parameter.h>
  101. /* Output format stuff. */
  102. @@ -36,19 +38,25 @@ const char *system_help;
  103. struct argp_child system_argp_child;
  104. int
  105. -system_parse_opt (struct ifconfig **ifp, char option, char *optarg)
  106. +system_parse_opt (struct ifconfig **ifp _GL_UNUSED_PARAMETER,
  107. + char option _GL_UNUSED_PARAMETER,
  108. + char *optarg _GL_UNUSED_PARAMETER)
  109. {
  110. return 0;
  111. }
  112. int
  113. -system_parse_opt_rest (struct ifconfig **ifp, int argc, char *argv[])
  114. +system_parse_opt_rest (struct ifconfig **ifp _GL_UNUSED_PARAMETER,
  115. + int argc _GL_UNUSED_PARAMETER,
  116. + char *argv[] _GL_UNUSED_PARAMETER)
  117. {
  118. return 0;
  119. }
  120. int
  121. -system_configure (int sfd, struct ifreq *ifr, struct system_ifconfig *ifs)
  122. +system_configure (int sfd _GL_UNUSED_PARAMETER,
  123. + struct ifreq *ifr _GL_UNUSED_PARAMETER,
  124. + struct system_ifconfig *ifs _GL_UNUSED_PARAMETER)
  125. {
  126. return 0;
  127. }
  128. diff --git a/ifconfig/system/hurd.c b/ifconfig/system/hurd.c
  129. new file mode 100644
  130. index 00000000..3bd19775
  131. --- /dev/null
  132. +++ b/ifconfig/system/hurd.c
  133. @@ -0,0 +1,292 @@
  134. +/* hurd.c -- Code for ifconfig specific to GNU/Hurd.
  135. + Copyright (C) 2015 Free Software Foundation, Inc.
  136. +
  137. + This file is part of GNU Inetutils.
  138. +
  139. + GNU Inetutils is free software: you can redistribute it and/or modify
  140. + it under the terms of the GNU General Public License as published by
  141. + the Free Software Foundation, either version 3 of the License, or (at
  142. + your option) any later version.
  143. +
  144. + GNU Inetutils is distributed in the hope that it will be useful, but
  145. + WITHOUT ANY WARRANTY; without even the implied warranty of
  146. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  147. + General Public License for more details.
  148. +
  149. + You should have received a copy of the GNU General Public License
  150. + along with this program. If not, see `http://www.gnu.org/licenses/'. */
  151. +
  152. +/* Mostly written by Marcus Brinkmann.
  153. + Adaptions to GNU/Hurd by Mats Erik Andersson. */
  154. +
  155. +#include <config.h>
  156. +
  157. +#include <stdlib.h>
  158. +#include <sys/ioctl.h>
  159. +#include <net/if_arp.h>
  160. +#include "../ifconfig.h"
  161. +
  162. +#include <unused-parameter.h>
  163. +
  164. +
  165. +/* Output format stuff. */
  166. +
  167. +const char *system_default_format = "gnu";
  168. +
  169. +
  170. +/* Argument parsing stuff. */
  171. +
  172. +const char *system_help = "NAME [ADDR]\
  173. + [broadcast BRDADDR] [netmask MASK]\
  174. + [mtu N] [up|down] [FLAGS]";
  175. +
  176. +struct argp_child system_argp_child;
  177. +
  178. +int
  179. +system_parse_opt (struct ifconfig **ifp _GL_UNUSED_PARAMETER,
  180. + char option _GL_UNUSED_PARAMETER,
  181. + char *optarg _GL_UNUSED_PARAMETER)
  182. +{
  183. + return 0;
  184. +}
  185. +
  186. +int
  187. +system_parse_opt_rest (struct ifconfig **ifp, int argc, char *argv[])
  188. +{
  189. + int i = 0, mask, rev;
  190. + enum {
  191. + EXPECT_NOTHING,
  192. + EXPECT_AF,
  193. + EXPECT_BROADCAST,
  194. + EXPECT_NETMASK,
  195. + EXPECT_METRIC,
  196. + EXPECT_MTU
  197. + } expect = EXPECT_AF;
  198. +
  199. + *ifp = parse_opt_new_ifs (argv[0]);
  200. +
  201. + while (++i < argc)
  202. + {
  203. + switch (expect)
  204. + {
  205. + case EXPECT_BROADCAST:
  206. + parse_opt_set_brdaddr (*ifp, argv[i]);
  207. + break;
  208. +
  209. + case EXPECT_NETMASK:
  210. + parse_opt_set_netmask (*ifp, argv[i]);
  211. + break;
  212. +
  213. + case EXPECT_MTU:
  214. + parse_opt_set_mtu (*ifp, argv[i]);
  215. + break;
  216. +
  217. + /* XXX: 2015-07-18, GNU/Hurd does not yet support
  218. + ioctl(SIOCSIFMETRIC), but we let the code
  219. + handle this standard ability anyway!
  220. + */
  221. + case EXPECT_METRIC:
  222. + parse_opt_set_metric (*ifp, argv[i]);
  223. + break;
  224. +
  225. + case EXPECT_AF:
  226. + expect = EXPECT_NOTHING;
  227. + if (!strcmp (argv[i], "inet"))
  228. + continue;
  229. + else if (!strcmp (argv[i], "inet6"))
  230. + {
  231. + error (0, 0, "%s is not a supported address family", argv[i]);
  232. + return 0;
  233. + }
  234. + break;
  235. +
  236. + case EXPECT_NOTHING:
  237. + break;
  238. + }
  239. +
  240. + if (expect != EXPECT_NOTHING)
  241. + expect = EXPECT_NOTHING;
  242. + else if (!strcmp (argv[i], "broadcast"))
  243. + expect = EXPECT_BROADCAST;
  244. + else if (!strcmp (argv[i], "netmask"))
  245. + expect = EXPECT_NETMASK;
  246. + else if (!strcmp (argv[i], "metric"))
  247. + expect = EXPECT_METRIC;
  248. + else if (!strcmp (argv[i], "mtu"))
  249. + expect = EXPECT_MTU;
  250. + else if (!strcmp (argv[i], "up"))
  251. + parse_opt_set_flag (*ifp, IFF_UP | IFF_RUNNING, 0);
  252. + else if (!strcmp (argv[i], "down"))
  253. + parse_opt_set_flag (*ifp, IFF_UP, 1);
  254. + else if (((mask = if_nameztoflag (argv[i], &rev))
  255. + & ~IU_IFF_CANTCHANGE) != 0)
  256. + parse_opt_set_flag (*ifp, mask, rev);
  257. + else
  258. + {
  259. + if (!((*ifp)->valid & IF_VALID_ADDR))
  260. + parse_opt_set_address (*ifp, argv[i]);
  261. + else if (!((*ifp)->valid & IF_VALID_DSTADDR))
  262. + parse_opt_set_dstaddr (*ifp, argv[i]);
  263. + }
  264. + }
  265. +
  266. + switch (expect)
  267. + {
  268. + case EXPECT_BROADCAST:
  269. + error (0, 0, "option `broadcast' requires an argument");
  270. + break;
  271. +
  272. + case EXPECT_NETMASK:
  273. + error (0, 0, "option `netmask' requires an argument");
  274. + break;
  275. +
  276. + case EXPECT_METRIC:
  277. + error (0, 0, "option `metric' requires an argument");
  278. + break;
  279. +
  280. + case EXPECT_MTU:
  281. + error (0, 0, "option `mtu' requires an argument");
  282. + break;
  283. +
  284. + case EXPECT_AF:
  285. + case EXPECT_NOTHING:
  286. + return 1;
  287. + }
  288. +
  289. + return 0;
  290. +}
  291. +
  292. +int
  293. +system_configure (int sfd _GL_UNUSED_PARAMETER,
  294. + struct ifreq *ifr _GL_UNUSED_PARAMETER,
  295. + struct system_ifconfig *ifs _GL_UNUSED_PARAMETER)
  296. +{
  297. + return 0;
  298. +}
  299. +
  300. +struct if_nameindex* (*system_if_nameindex) (void) = if_nameindex;
  301. +
  302. +static void
  303. +print_hwaddr_ether (format_data_t form _GL_UNUSED_PARAMETER,
  304. + unsigned char *data)
  305. +{
  306. + *column += printf ("%02X:%02X:%02X:%02X:%02X:%02X",
  307. + data[0], data[1], data[2], data[3], data[4], data[5]);
  308. + had_output = 1;
  309. +}
  310. +
  311. +struct arphrd_symbol
  312. +{
  313. + const char *name;
  314. + const char *title;
  315. + int value;
  316. + void (*print_hwaddr) (format_data_t form, unsigned char *data);
  317. +} arphrd_symbols[] =
  318. + {
  319. +#ifdef ARPHRD_ETHER /* Ethernet 10/100Mbps. */
  320. + { "ETHER", "Ethernet", ARPHRD_ETHER, print_hwaddr_ether},
  321. +#endif
  322. +#ifdef ARPHRD_LOOPBACK /* Loopback device. */
  323. + { "LOOPBACK", "Local Loopback", ARPHRD_LOOPBACK, NULL},
  324. +#endif
  325. + /* XXX: The image debian-hurd-20150424 returns the value 4
  326. + instead of expected ARPHRD_LOOPBACK. This has been
  327. + discussed in the list debian-hurd, where I was asked
  328. + to resist the temptation of a work around!
  329. + */
  330. + { NULL, NULL, 0, NULL}
  331. + };
  332. +
  333. +struct arphrd_symbol *
  334. +arphrd_findvalue (int value)
  335. +{
  336. + struct arphrd_symbol *arp = arphrd_symbols;
  337. + while (arp->name != NULL)
  338. + {
  339. + if (arp->value == value)
  340. + break;
  341. + arp++;
  342. + }
  343. + if (arp->name)
  344. + return arp;
  345. + else
  346. + return NULL;
  347. +}
  348. +
  349. +void
  350. +system_fh_hwaddr_query (format_data_t form, int argc, char *argv[])
  351. +{
  352. +#ifdef SIOCGIFHWADDR
  353. + struct arphrd_symbol *arp;
  354. +
  355. + if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
  356. + select_arg (form, argc, argv, 1);
  357. +
  358. + arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
  359. + select_arg (form, argc, argv, (arp && arp->print_hwaddr) ? 0 : 1);
  360. +#else
  361. + select_arg (form, argc, argv, 1);
  362. +#endif
  363. +}
  364. +
  365. +void
  366. +system_fh_hwaddr (format_data_t form, int argc _GL_UNUSED_PARAMETER,
  367. + char *argv[] _GL_UNUSED_PARAMETER)
  368. +{
  369. +#ifdef SIOCGIFHWADDR
  370. + if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
  371. + error (EXIT_FAILURE, errno,
  372. + "SIOCGIFHWADDR failed for interface `%s'",
  373. + form->ifr->ifr_name);
  374. + else
  375. + {
  376. + struct arphrd_symbol *arp;
  377. +
  378. + arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
  379. + if (arp && arp->print_hwaddr)
  380. + arp->print_hwaddr (form,
  381. + (unsigned char *) form->ifr->ifr_hwaddr.sa_data);
  382. + else
  383. + put_string (form, "(hwaddr unknown)");
  384. + }
  385. +#else
  386. + *column += printf ("(not available)");
  387. + had_output = 1;
  388. +#endif
  389. +}
  390. +
  391. +void
  392. +system_fh_hwtype_query (format_data_t form, int argc, char *argv[])
  393. +{
  394. +#ifdef SIOCGIFHWADDR
  395. + if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) >= 0)
  396. + select_arg (form, argc, argv, 0);
  397. + else
  398. +#endif
  399. + select_arg (form, argc, argv, 1);
  400. +}
  401. +
  402. +void
  403. +system_fh_hwtype (format_data_t form, int argc _GL_UNUSED_PARAMETER,
  404. + char *argv[] _GL_UNUSED_PARAMETER)
  405. +{
  406. +#ifdef SIOCGIFHWADDR
  407. + if (ioctl (form->sfd, SIOCGIFHWADDR, form->ifr) < 0)
  408. + error (EXIT_FAILURE, errno,
  409. + "SIOCGIFHWADDR failed for interface `%s'",
  410. + form->ifr->ifr_name);
  411. + else
  412. + {
  413. + struct arphrd_symbol *arp;
  414. +
  415. + arp = arphrd_findvalue (form->ifr->ifr_hwaddr.sa_family);
  416. + if (arp)
  417. + put_string (form, arp->title);
  418. + else
  419. + put_string (form, "(hwtype unknown)");
  420. + }
  421. +#else
  422. + *column += printf ("(not available)");
  423. + had_output = 1;
  424. +#endif
  425. +}
  426. diff --git a/ifconfig/system/hurd.h b/ifconfig/system/hurd.h
  427. new file mode 100644
  428. index 00000000..bab14565
  429. --- /dev/null
  430. +++ b/ifconfig/system/hurd.h
  431. @@ -0,0 +1,50 @@
  432. +/*
  433. + Copyright (C) 2015 Free Software Foundation, Inc.
  434. +
  435. + This file is part of GNU Inetutils.
  436. +
  437. + GNU Inetutils is free software: you can redistribute it and/or modify
  438. + it under the terms of the GNU General Public License as published by
  439. + the Free Software Foundation, either version 3 of the License, or (at
  440. + your option) any later version.
  441. +
  442. + GNU Inetutils is distributed in the hope that it will be useful, but
  443. + WITHOUT ANY WARRANTY; without even the implied warranty of
  444. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  445. + General Public License for more details.
  446. +
  447. + You should have received a copy of the GNU General Public License
  448. + along with this program. If not, see `http://www.gnu.org/licenses/'. */
  449. +
  450. +/* Written by Mats Erik Andersson. */
  451. +
  452. +#ifndef IFCONFIG_SYSTEM_HURD_H
  453. +# define IFCONFIG_SYSTEM_HURD_H
  454. +
  455. +# include "../printif.h"
  456. +# include "../options.h"
  457. +
  458. +
  459. +/* Option support. */
  460. +
  461. +struct system_ifconfig
  462. +{
  463. + int valid;
  464. +};
  465. +
  466. +
  467. +/* Output format support. */
  468. +
  469. +# define SYSTEM_FORMAT_HANDLER \
  470. + { "hurd", fh_nothing}, \
  471. + { "hwaddr?", system_fh_hwaddr_query}, \
  472. + { "hwaddr", system_fh_hwaddr}, \
  473. + { "hwtype?", system_fh_hwtype_query}, \
  474. + { "hwtype", system_fh_hwtype},
  475. +
  476. +void system_fh_hwaddr_query (format_data_t form, int argc, char *argv[]);
  477. +void system_fh_hwaddr (format_data_t form, int argc, char *argv[]);
  478. +void system_fh_hwtype_query (format_data_t form, int argc, char *argv[]);
  479. +void system_fh_hwtype (format_data_t form, int argc, char *argv[]);
  480. +
  481. +#endif /* !IFCONFIG_SYSTEM_HURD_H */
  482. --
  483. 2.26.0.292.g33ef6b2f38