Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ifupdown for busybox |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 3 | * Copyright (c) 2002 Glenn McGrath <bug1@optushome.com.au> |
| 4 | * |
| 5 | * Based on ifupdown v 0.6.4 by Anthony Towns |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 6 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au> |
| 7 | * |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 8 | * Changes to upstream version |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 9 | * Remove checks for kernel version, assume kernel version 2.2.0 or better. |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 10 | * Lines in the interfaces file cannot wrap. |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 11 | * To adhere to the FHS, the default state file is /var/run/ifstate. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/utsname.h> |
| 30 | #include <sys/wait.h> |
| 31 | |
| 32 | #include <ctype.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <fnmatch.h> |
| 36 | #include <getopt.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
Glenn L McGrath | 9af8a72 | 2002-11-11 07:03:02 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 44 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 45 | #define MAX_OPT_DEPTH 10 |
| 46 | #define EUNBALBRACK 10001 |
| 47 | #define EUNDEFVAR 10002 |
| 48 | #define EUNBALPER 10000 |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 49 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 50 | typedef struct interface_defn_s interface_defn_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 51 | |
| 52 | typedef int (execfn)(char *command); |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 53 | typedef int (command_set)(interface_defn_t *ifd, execfn *e); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 54 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 55 | typedef struct method_s { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 56 | char *name; |
| 57 | command_set *up; |
| 58 | command_set *down; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 59 | } method_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 60 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 61 | typedef struct address_family_s { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 62 | char *name; |
| 63 | int n_methods; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 64 | method_t *method; |
| 65 | } address_family_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 66 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 67 | typedef struct mapping_defn_s { |
| 68 | struct mapping_defn_s *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 69 | |
| 70 | int max_matches; |
| 71 | int n_matches; |
| 72 | char **match; |
| 73 | |
| 74 | char *script; |
| 75 | |
| 76 | int max_mappings; |
| 77 | int n_mappings; |
| 78 | char **mapping; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 79 | } mapping_defn_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 80 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 81 | typedef struct variable_s { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 82 | char *name; |
| 83 | char *value; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 84 | } variable_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 85 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 86 | struct interface_defn_s { |
| 87 | struct interface_defn_s *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 88 | |
| 89 | char *iface; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 90 | address_family_t *address_family; |
| 91 | method_t *method; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 92 | |
| 93 | int automatic; |
| 94 | |
| 95 | int max_options; |
| 96 | int n_options; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 97 | variable_t *option; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 100 | typedef struct interfaces_file_s { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 101 | llist_t *autointerfaces; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 102 | interface_defn_t *ifaces; |
| 103 | mapping_defn_t *mappings; |
| 104 | } interfaces_file_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 105 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 106 | static char no_act = 0; |
| 107 | static char verbose = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 108 | static char **environ = NULL; |
| 109 | |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 110 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 111 | static int count_netmask_bits(char *dotted_quad) |
| 112 | { |
| 113 | unsigned int a, b, c, d; |
| 114 | unsigned int res, result; |
| 115 | /* Found a netmask... Check if it is dotted quad */ |
| 116 | if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
| 117 | return -1; |
| 118 | res = (a & 0x55) + ((a >> 1) & 0x55); |
| 119 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 120 | result = (res & 0x0F) + ((res >> 4) & 0x0F); |
| 121 | |
| 122 | res = (b & 0x55) + ((b >> 1) & 0x55); |
| 123 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 124 | result += (res & 0x0F) + ((res >> 4) & 0x0F); |
| 125 | |
| 126 | res = (c & 0x55) + ((c >> 1) & 0x55); |
| 127 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 128 | result += (res & 0x0F) + ((res >> 4) & 0x0F); |
| 129 | |
| 130 | res = (d & 0x55) + ((d >> 1) & 0x55); |
| 131 | res = (res & 0x33) + ((res >> 2) & 0x33); |
| 132 | result += (res & 0x0F) + ((res >> 4) & 0x0F); |
| 133 | return ((int)result); |
| 134 | } |
| 135 | #endif |
| 136 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 137 | static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length) |
| 138 | { |
| 139 | if (*pos + str_length >= *len) { |
| 140 | char *newbuf; |
| 141 | |
| 142 | newbuf = xrealloc(*buf, *len * 2 + str_length + 1); |
| 143 | *buf = newbuf; |
| 144 | *len = *len * 2 + str_length + 1; |
| 145 | } |
| 146 | |
| 147 | while (str_length-- >= 1) { |
| 148 | (*buf)[(*pos)++] = *str; |
| 149 | str++; |
| 150 | } |
| 151 | (*buf)[*pos] = '\0'; |
| 152 | } |
| 153 | |
| 154 | static int strncmpz(char *l, char *r, size_t llen) |
| 155 | { |
| 156 | int i = strncmp(l, r, llen); |
| 157 | |
| 158 | if (i == 0) { |
| 159 | return(-r[llen]); |
| 160 | } else { |
| 161 | return(i); |
| 162 | } |
| 163 | } |
| 164 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 165 | static char *get_var(char *id, size_t idlen, interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 166 | { |
| 167 | int i; |
| 168 | |
| 169 | if (strncmpz(id, "iface", idlen) == 0) { |
| 170 | return (ifd->iface); |
| 171 | } else { |
| 172 | for (i = 0; i < ifd->n_options; i++) { |
| 173 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) { |
| 174 | return (ifd->option[i].value); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return(NULL); |
| 180 | } |
| 181 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 182 | static char *parse(char *command, interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 183 | { |
| 184 | |
| 185 | char *result = NULL; |
| 186 | size_t pos = 0, len = 0; |
| 187 | size_t old_pos[MAX_OPT_DEPTH] = { 0 }; |
| 188 | int okay[MAX_OPT_DEPTH] = { 1 }; |
| 189 | int opt_depth = 1; |
| 190 | |
| 191 | while (*command) { |
| 192 | switch (*command) { |
| 193 | |
| 194 | default: |
| 195 | addstr(&result, &len, &pos, command, 1); |
| 196 | command++; |
| 197 | break; |
| 198 | case '\\': |
| 199 | if (command[1]) { |
| 200 | addstr(&result, &len, &pos, command + 1, 1); |
| 201 | command += 2; |
| 202 | } else { |
| 203 | addstr(&result, &len, &pos, command, 1); |
| 204 | command++; |
| 205 | } |
| 206 | break; |
| 207 | case '[': |
| 208 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { |
| 209 | old_pos[opt_depth] = pos; |
| 210 | okay[opt_depth] = 1; |
| 211 | opt_depth++; |
| 212 | command += 2; |
| 213 | } else { |
| 214 | addstr(&result, &len, &pos, "[", 1); |
| 215 | command++; |
| 216 | } |
| 217 | break; |
| 218 | case ']': |
| 219 | if (command[1] == ']' && opt_depth > 1) { |
| 220 | opt_depth--; |
| 221 | if (!okay[opt_depth]) { |
| 222 | pos = old_pos[opt_depth]; |
| 223 | result[pos] = '\0'; |
| 224 | } |
| 225 | command += 2; |
| 226 | } else { |
| 227 | addstr(&result, &len, &pos, "]", 1); |
| 228 | command++; |
| 229 | } |
| 230 | break; |
| 231 | case '%': |
| 232 | { |
| 233 | char *nextpercent; |
| 234 | char *varvalue; |
| 235 | |
| 236 | command++; |
| 237 | nextpercent = strchr(command, '%'); |
| 238 | if (!nextpercent) { |
| 239 | errno = EUNBALPER; |
| 240 | free(result); |
| 241 | return (NULL); |
| 242 | } |
| 243 | |
| 244 | varvalue = get_var(command, nextpercent - command, ifd); |
| 245 | |
| 246 | if (varvalue) { |
| 247 | addstr(&result, &len, &pos, varvalue, xstrlen(varvalue)); |
| 248 | } else { |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 249 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 250 | /* Sigh... Add a special case for 'ip' to convert from |
| 251 | * dotted quad to bit count style netmasks. */ |
| 252 | if (strncmp(command, "bnmask", 6)==0) { |
| 253 | int res; |
| 254 | varvalue = get_var("netmask", 7, ifd); |
| 255 | if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { |
| 256 | char argument[255]; |
| 257 | sprintf(argument, "%d", res); |
| 258 | addstr(&result, &len, &pos, argument, xstrlen(argument)); |
| 259 | command = nextpercent + 1; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 264 | okay[opt_depth - 1] = 0; |
| 265 | } |
| 266 | |
| 267 | command = nextpercent + 1; |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (opt_depth > 1) { |
| 274 | errno = EUNBALBRACK; |
| 275 | free(result); |
| 276 | return(NULL); |
| 277 | } |
| 278 | |
| 279 | if (!okay[0]) { |
| 280 | errno = EUNDEFVAR; |
| 281 | free(result); |
| 282 | return(NULL); |
| 283 | } |
| 284 | |
| 285 | return(result); |
| 286 | } |
| 287 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 288 | static int execute(char *command, interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 289 | { |
| 290 | char *out; |
| 291 | int ret; |
| 292 | |
| 293 | out = parse(command, ifd); |
| 294 | if (!out) { |
| 295 | return(0); |
| 296 | } |
| 297 | |
| 298 | ret = (*exec) (out); |
| 299 | |
| 300 | free(out); |
| 301 | return(ret); |
| 302 | } |
| 303 | |
| 304 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPX |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 305 | static int static_up_ipx(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 306 | { |
| 307 | if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) { |
| 308 | return(0); |
| 309 | } |
| 310 | return(1); |
| 311 | } |
| 312 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 313 | static int static_down_ipx(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 314 | { |
| 315 | if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) { |
| 316 | return(0); |
| 317 | } |
| 318 | return(1); |
| 319 | } |
| 320 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 321 | static int dynamic_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 322 | { |
| 323 | if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) { |
| 324 | return(0); |
| 325 | } |
| 326 | return(1); |
| 327 | } |
| 328 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 329 | static int dynamic_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 330 | { |
| 331 | if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) { |
| 332 | return(0); |
| 333 | } |
| 334 | return(1); |
| 335 | } |
| 336 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 337 | static method_t methods_ipx[] = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 338 | { "dynamic", dynamic_up, dynamic_down, }, |
| 339 | { "static", static_up_ipx, static_down_ipx, }, |
| 340 | }; |
| 341 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 342 | address_family_t addr_ipx = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 343 | "ipx", |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 344 | sizeof(methods_ipx) / sizeof(method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 345 | methods_ipx |
| 346 | }; |
| 347 | #endif /* IFUP_FEATURE_IPX */ |
| 348 | |
| 349 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 350 | static int loopback_up6(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 351 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 352 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 353 | if (!execute("ip link set %iface% up", ifd, exec)) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 354 | return(0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 355 | if (!execute("ip addr add ::1 dev %iface%", ifd, exec)) |
| 356 | return(0); |
| 357 | #else |
| 358 | if (!execute("ifconfig %iface% add ::1", ifd, exec)) |
| 359 | return(0); |
| 360 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 361 | return(1); |
| 362 | } |
| 363 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 364 | static int loopback_down6(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 365 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 366 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 367 | if (!execute("ip link set %iface% down", ifd, exec)) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 368 | return(0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 369 | #else |
| 370 | if (!execute("ifconfig %iface% del ::1", ifd, exec)) |
| 371 | return(0); |
| 372 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 373 | return(1); |
| 374 | } |
| 375 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 376 | static int static_up6(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 377 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 378 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 379 | if (!execute("ip link set %iface% up", ifd, exec)) |
| 380 | return(0); |
| 381 | if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) |
| 382 | return(0); |
| 383 | if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) |
| 384 | return(0); |
| 385 | #else |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 386 | if (!execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec)) { |
| 387 | return(0); |
| 388 | } |
| 389 | if (!execute("ifconfig %iface% add %address%/%netmask%", ifd, exec)) { |
| 390 | return(0); |
| 391 | } |
| 392 | if (!execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec)) { |
| 393 | return(0); |
| 394 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 395 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 396 | return(1); |
| 397 | } |
| 398 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 399 | static int static_down6(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 400 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 401 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 402 | if (!execute("ip link set %iface% down", ifd, exec)) |
| 403 | return(0); |
| 404 | #else |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 405 | if (!execute("ifconfig %iface% down", ifd, exec)) { |
| 406 | return(0); |
| 407 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 408 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 409 | return(1); |
| 410 | } |
| 411 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 412 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 413 | static int v4tunnel_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 414 | { |
| 415 | if (!execute("ip tunnel add %iface% mode sit remote %endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec)) { |
| 416 | return(0); |
| 417 | } |
| 418 | if (!execute("ip link set %iface% up", ifd, exec)) { |
| 419 | return(0); |
| 420 | } |
| 421 | if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) { |
| 422 | return(0); |
| 423 | } |
| 424 | if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) { |
| 425 | return(0); |
| 426 | } |
| 427 | return(1); |
| 428 | } |
| 429 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 430 | static int v4tunnel_down(interface_defn_t * ifd, execfn * exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 431 | { |
| 432 | if (!execute("ip tunnel del %iface%", ifd, exec)) { |
| 433 | return(0); |
| 434 | } |
| 435 | return(1); |
| 436 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 437 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 438 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 439 | static method_t methods6[] = { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 440 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 441 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 442 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 443 | { "static", static_up6, static_down6, }, |
| 444 | { "loopback", loopback_up6, loopback_down6, }, |
| 445 | }; |
| 446 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 447 | address_family_t addr_inet6 = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 448 | "inet6", |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 449 | sizeof(methods6) / sizeof(method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 450 | methods6 |
| 451 | }; |
| 452 | #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */ |
| 453 | |
| 454 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 455 | static int loopback_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 456 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 457 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 458 | if (!execute("ip link set %iface% up", ifd, exec)) |
| 459 | return(0); |
Eric Andersen | 46c203a | 2003-02-03 12:44:59 +0000 | [diff] [blame^] | 460 | if (!execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec)) |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 461 | return(0); |
| 462 | #else |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 463 | if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) { |
| 464 | return(0); |
| 465 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 466 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 467 | return(1); |
| 468 | } |
| 469 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 470 | static int loopback_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 471 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 472 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 473 | if (!execute("ip addr flush dev %iface%", ifd, exec)) |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 474 | return(0); |
| 475 | if (!execute("ip link set %iface% down", ifd, exec)) |
| 476 | return(0); |
| 477 | #else |
Glenn L McGrath | 1d65826 | 2002-12-07 00:48:54 +0000 | [diff] [blame] | 478 | if (!execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 479 | return(0); |
| 480 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 481 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 482 | return(1); |
| 483 | } |
| 484 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 485 | static int static_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 486 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 487 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 488 | if (!execute("ip link set %iface% up", ifd, exec)) |
| 489 | return(0); |
Eric Andersen | 46c203a | 2003-02-03 12:44:59 +0000 | [diff] [blame^] | 490 | if (!execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] dev %iface%", ifd, exec)) |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 491 | return(0); |
| 492 | if (!execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec)) |
| 493 | return(0); |
| 494 | #else |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 495 | if (!execute("ifconfig %iface% %address% netmask %netmask% [[broadcast %broadcast%]] [[pointopoint %pointopoint%]] [[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up", |
| 496 | ifd, exec)) { |
| 497 | return(0); |
| 498 | } |
| 499 | if (!execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec)) { |
| 500 | return(0); |
| 501 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 502 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 503 | return(1); |
| 504 | } |
| 505 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 506 | static int static_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 507 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 508 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 509 | //if (!execute("[[ ip route del default via %gateway% dev %iface% ]]", ifd, exec)) |
| 510 | // return(0); |
| 511 | if (!execute("ip addr flush dev %iface%", ifd, exec)) |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 512 | return(0); |
| 513 | if (!execute("ip link set %iface% down", ifd, exec)) |
| 514 | return(0); |
| 515 | #else |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 516 | if (!execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec)) { |
| 517 | return(0); |
| 518 | } |
| 519 | if (!execute("ifconfig %iface% down", ifd, exec)) { |
| 520 | return(0); |
| 521 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 522 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 523 | return(1); |
| 524 | } |
| 525 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 526 | static int execable(char *program) |
| 527 | { |
| 528 | struct stat buf; |
| 529 | if (0 == stat(program, &buf)) { |
| 530 | if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) { |
| 531 | return(1); |
| 532 | } |
| 533 | } |
| 534 | return(0); |
| 535 | } |
| 536 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 537 | static int dhcp_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 538 | { |
| 539 | if (execable("/sbin/dhclient")) { |
| 540 | if (!execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)) { |
| 541 | return(0); |
| 542 | } |
| 543 | } else if (execable("/sbin/pump")) { |
| 544 | if (!execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)) { |
| 545 | return(0); |
| 546 | } |
| 547 | } else if (execable("/sbin/udhcpc")) { |
| 548 | if (!execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)) { |
| 549 | return 0; |
| 550 | } |
| 551 | } else if (execable("/sbin/dhcpcd")) { |
| 552 | if (!execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%", ifd, exec)) { |
| 553 | return(0); |
| 554 | } |
| 555 | } |
| 556 | return(1); |
| 557 | } |
| 558 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 559 | static int dhcp_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 560 | { |
| 561 | if (execable("/sbin/dhclient")) { |
Glenn L McGrath | d143107 | 2002-11-19 09:58:56 +0000 | [diff] [blame] | 562 | if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 563 | return(0); |
| 564 | } |
| 565 | } else if (execable("/sbin/pump")) { |
| 566 | if (!execute("pump -i %iface% -k", ifd, exec)) { |
| 567 | return(0); |
| 568 | } |
| 569 | } else if (execable("/sbin/udhcpc")) { |
Glenn L McGrath | d143107 | 2002-11-19 09:58:56 +0000 | [diff] [blame] | 570 | if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 571 | return(0); |
| 572 | } |
| 573 | } else if (execable("/sbin/dhcpcd")) { |
| 574 | if (!execute("dhcpcd -k %iface%", ifd, exec)) { |
| 575 | return(0); |
| 576 | } |
| 577 | } |
| 578 | if (!execute("ifconfig %iface% down", ifd, exec)) { |
| 579 | return(0); |
| 580 | } |
| 581 | return(1); |
| 582 | } |
| 583 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 584 | static int bootp_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 585 | { |
| 586 | if (!execute("bootpc [[--bootfile %bootfile%]] --dev %iface% [[--server %server%]] [[--hwaddr %hwaddr%]] --returniffail --serverbcast", ifd, exec)) { |
| 587 | return 0; |
| 588 | } |
| 589 | return 1; |
| 590 | } |
| 591 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 592 | static int bootp_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 593 | { |
| 594 | if (!execute("ifconfig down %iface%", ifd, exec)) { |
| 595 | return 0; |
| 596 | } |
| 597 | return 1; |
| 598 | } |
| 599 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 600 | static int ppp_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 601 | { |
| 602 | if (!execute("pon [[%provider%]]", ifd, exec)) { |
| 603 | return 0; |
| 604 | } |
| 605 | return 1; |
| 606 | } |
| 607 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 608 | static int ppp_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 609 | { |
| 610 | if (!execute("poff [[%provider%]]", ifd, exec)) { |
| 611 | return 0; |
| 612 | } |
| 613 | return 1; |
| 614 | } |
| 615 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 616 | static int wvdial_up(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 617 | { |
| 618 | if (!execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)) { |
| 619 | return 0; |
| 620 | } |
| 621 | return 1; |
| 622 | } |
| 623 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 624 | static int wvdial_down(interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 625 | { |
| 626 | if (!execute ("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -s 2", ifd, exec)) { |
| 627 | return 0; |
| 628 | } |
| 629 | return 1; |
| 630 | } |
| 631 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 632 | static method_t methods[] = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 633 | { "wvdial", wvdial_up, wvdial_down, }, |
| 634 | { "ppp", ppp_up, ppp_down, }, |
| 635 | { "static", static_up, static_down, }, |
| 636 | { "bootp", bootp_up, bootp_down, }, |
| 637 | { "dhcp", dhcp_up, dhcp_down, }, |
| 638 | { "loopback", loopback_up, loopback_down, }, |
| 639 | }; |
| 640 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 641 | address_family_t addr_inet = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 642 | "inet", |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 643 | sizeof(methods) / sizeof(method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 644 | methods |
| 645 | }; |
| 646 | |
| 647 | #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */ |
| 648 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 649 | static char *next_word(char **buf) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 650 | { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 651 | unsigned short length; |
| 652 | char *word; |
| 653 | |
| 654 | if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 655 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 658 | /* Skip over leading whitespace */ |
| 659 | word = *buf + strspn(*buf, " \t\n"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 660 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 661 | /* Find the length of this word */ |
| 662 | length = strcspn(word, " \t\n"); |
| 663 | if (length == 0) { |
| 664 | return(NULL); |
| 665 | } |
| 666 | *buf = word + length; |
| 667 | **buf = '\0'; |
| 668 | (*buf)++; |
| 669 | |
| 670 | return word; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 673 | static address_family_t *get_address_family(address_family_t *af[], char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 674 | { |
| 675 | int i; |
| 676 | |
| 677 | for (i = 0; af[i]; i++) { |
| 678 | if (strcmp(af[i]->name, name) == 0) { |
| 679 | return af[i]; |
| 680 | } |
| 681 | } |
| 682 | return NULL; |
| 683 | } |
| 684 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 685 | static method_t *get_method(address_family_t *af, char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 686 | { |
| 687 | int i; |
| 688 | |
| 689 | for (i = 0; i < af->n_methods; i++) { |
| 690 | if (strcmp(af->method[i].name, name) == 0) { |
| 691 | return &af->method[i]; |
| 692 | } |
| 693 | } |
| 694 | return(NULL); |
| 695 | } |
| 696 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 697 | static int duplicate_if(interface_defn_t *ifa, interface_defn_t *ifb) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 698 | { |
| 699 | if (strcmp(ifa->iface, ifb->iface) != 0) { |
| 700 | return(0); |
| 701 | } |
| 702 | if (ifa->address_family != ifb->address_family) { |
| 703 | return(0); |
| 704 | } |
| 705 | return(1); |
| 706 | } |
| 707 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 708 | static const llist_t *find_list_string(const llist_t *list, const char *string) |
| 709 | { |
| 710 | while (list) { |
| 711 | if (strcmp(list->data, string) == 0) { |
| 712 | return(list); |
| 713 | } |
| 714 | list = list->link; |
| 715 | } |
| 716 | return(NULL); |
| 717 | } |
| 718 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 719 | static interfaces_file_t *read_interfaces(char *filename) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 720 | { |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 721 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 722 | mapping_defn_t *currmap = NULL; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 723 | #endif |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 724 | interface_defn_t *currif = NULL; |
| 725 | interfaces_file_t *defn; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 726 | FILE *f; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 727 | char *firstword; |
| 728 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 729 | |
| 730 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
| 731 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 732 | defn = xmalloc(sizeof(interfaces_file_t)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 733 | defn->autointerfaces = NULL; |
| 734 | defn->mappings = NULL; |
| 735 | defn->ifaces = NULL; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 736 | |
| 737 | f = xfopen(filename, "r"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 738 | |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 739 | while ((buf = get_line_from_file(f)) != NULL) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 740 | char *buf_ptr = buf; |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 741 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 742 | /* Ignore comments */ |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 743 | if (buf[0] == '#') { |
| 744 | continue; |
| 745 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 746 | |
| 747 | firstword = next_word(&buf_ptr); |
| 748 | if (firstword == NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 749 | continue; /* blank line */ |
| 750 | } |
| 751 | |
| 752 | if (strcmp(firstword, "mapping") == 0) { |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 753 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 754 | currmap = xmalloc(sizeof(mapping_defn_t)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 755 | currmap->max_matches = 0; |
| 756 | currmap->n_matches = 0; |
| 757 | currmap->match = NULL; |
| 758 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 759 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 760 | if (currmap->max_matches == currmap->n_matches) { |
| 761 | currmap->max_matches = currmap->max_matches * 2 + 1; |
| 762 | currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); |
| 763 | } |
| 764 | |
| 765 | currmap->match[currmap->n_matches++] = xstrdup(firstword); |
| 766 | } |
| 767 | currmap->max_mappings = 0; |
| 768 | currmap->n_mappings = 0; |
| 769 | currmap->mapping = NULL; |
| 770 | currmap->script = NULL; |
| 771 | { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 772 | mapping_defn_t **where = &defn->mappings; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 773 | while (*where != NULL) { |
| 774 | where = &(*where)->next; |
| 775 | } |
| 776 | *where = currmap; |
| 777 | currmap->next = NULL; |
| 778 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 779 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 780 | currently_processing = MAPPING; |
| 781 | } else if (strcmp(firstword, "iface") == 0) { |
| 782 | { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 783 | char *iface_name; |
| 784 | char *address_family_name; |
| 785 | char *method_name; |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 786 | address_family_t *addr_fams[] = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 787 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
| 788 | &addr_inet, |
| 789 | #endif |
| 790 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
| 791 | &addr_inet6, |
| 792 | #endif |
| 793 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPX |
| 794 | &addr_ipx, |
| 795 | #endif |
| 796 | NULL |
| 797 | }; |
| 798 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 799 | currif = xmalloc(sizeof(interface_defn_t)); |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 800 | iface_name = next_word(&buf_ptr); |
| 801 | address_family_name = next_word(&buf_ptr); |
| 802 | method_name = next_word(&buf_ptr); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 803 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 804 | if (buf_ptr == NULL) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 805 | error_msg("too few parameters for line \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 806 | return NULL; |
| 807 | } |
| 808 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 809 | if (buf_ptr[0] != '\0') { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 810 | error_msg("too many parameters \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | currif->iface = xstrdup(iface_name); |
| 815 | |
| 816 | currif->address_family = get_address_family(addr_fams, address_family_name); |
| 817 | if (!currif->address_family) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 818 | error_msg("unknown address type \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 819 | return NULL; |
| 820 | } |
| 821 | |
| 822 | currif->method = get_method(currif->address_family, method_name); |
| 823 | if (!currif->method) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 824 | error_msg("unknown method \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 825 | return NULL; |
| 826 | } |
| 827 | |
| 828 | currif->automatic = 1; |
| 829 | currif->max_options = 0; |
| 830 | currif->n_options = 0; |
| 831 | currif->option = NULL; |
| 832 | |
| 833 | |
| 834 | { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 835 | interface_defn_t **where = &defn->ifaces; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 836 | |
| 837 | while (*where != NULL) { |
| 838 | if (duplicate_if(*where, currif)) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 839 | error_msg("duplicate interface \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 840 | return NULL; |
| 841 | } |
| 842 | where = &(*where)->next; |
| 843 | } |
| 844 | |
| 845 | *where = currif; |
| 846 | currif->next = NULL; |
| 847 | } |
| 848 | } |
| 849 | currently_processing = IFACE; |
| 850 | } else if (strcmp(firstword, "auto") == 0) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 851 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 852 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 853 | /* Check the interface isnt already listed */ |
| 854 | if (find_list_string(defn->autointerfaces, firstword)) { |
| 855 | perror_msg_and_die("interface declared auto twice \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 858 | /* Add the interface to the list */ |
| 859 | defn->autointerfaces = llist_add_to(defn->autointerfaces, strdup(firstword)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 860 | } |
| 861 | currently_processing = NONE; |
| 862 | } else { |
| 863 | switch (currently_processing) { |
| 864 | case IFACE: |
| 865 | { |
| 866 | int i; |
| 867 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 868 | if (xstrlen(buf_ptr) == 0) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 869 | error_msg("option with empty value \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 870 | return NULL; |
| 871 | } |
| 872 | |
| 873 | if (strcmp(firstword, "up") != 0 |
| 874 | && strcmp(firstword, "down") != 0 |
| 875 | && strcmp(firstword, "pre-up") != 0 |
| 876 | && strcmp(firstword, "post-down") != 0) { |
| 877 | for (i = 0; i < currif->n_options; i++) { |
| 878 | if (strcmp(currif->option[i].name, firstword) == 0) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 879 | error_msg("duplicate option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 880 | return NULL; |
| 881 | } |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | if (currif->n_options >= currif->max_options) { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 886 | variable_t *opt; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 887 | |
| 888 | currif->max_options = currif->max_options + 10; |
| 889 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); |
| 890 | currif->option = opt; |
| 891 | } |
| 892 | currif->option[currif->n_options].name = xstrdup(firstword); |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 893 | currif->option[currif->n_options].value = xstrdup(next_word(&buf_ptr)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 894 | if (!currif->option[currif->n_options].name) { |
| 895 | perror(filename); |
| 896 | return NULL; |
| 897 | } |
| 898 | if (!currif->option[currif->n_options].value) { |
| 899 | perror(filename); |
| 900 | return NULL; |
| 901 | } |
| 902 | currif->n_options++; |
| 903 | break; |
| 904 | case MAPPING: |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 905 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 906 | if (strcmp(firstword, "script") == 0) { |
| 907 | if (currmap->script != NULL) { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 908 | error_msg("duplicate script in mapping \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 909 | return NULL; |
| 910 | } else { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 911 | currmap->script = xstrdup(next_word(&buf_ptr)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 912 | } |
| 913 | } else if (strcmp(firstword, "map") == 0) { |
| 914 | if (currmap->max_mappings == currmap->n_mappings) { |
| 915 | currmap->max_mappings = currmap->max_mappings * 2 + 1; |
| 916 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); |
| 917 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 918 | currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 919 | currmap->n_mappings++; |
| 920 | } else { |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 921 | error_msg("misplaced option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 922 | return NULL; |
| 923 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 924 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 925 | break; |
| 926 | case NONE: |
| 927 | default: |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 928 | error_msg("misplaced option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 929 | return NULL; |
| 930 | } |
| 931 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 932 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 933 | } |
| 934 | if (ferror(f) != 0) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 935 | perror_msg_and_die("%s", filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 936 | } |
| 937 | fclose(f); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 938 | |
| 939 | return defn; |
| 940 | } |
| 941 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 942 | static char *setlocalenv(char *format, char *name, char *value) |
| 943 | { |
| 944 | char *result; |
| 945 | char *here; |
| 946 | char *there; |
| 947 | |
| 948 | result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1); |
| 949 | |
| 950 | sprintf(result, format, name, value); |
| 951 | |
| 952 | for (here = there = result; *there != '=' && *there; there++) { |
| 953 | if (*there == '-') |
| 954 | *there = '_'; |
| 955 | if (isalpha(*there)) |
| 956 | *there = toupper(*there); |
| 957 | |
| 958 | if (isalnum(*there) || *there == '_') { |
| 959 | *here = *there; |
| 960 | here++; |
| 961 | } |
| 962 | } |
| 963 | memmove(here, there, xstrlen(there) + 1); |
| 964 | |
| 965 | return result; |
| 966 | } |
| 967 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 968 | static void set_environ(interface_defn_t *iface, char *mode) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 969 | { |
| 970 | char **environend; |
| 971 | int i; |
| 972 | const int n_env_entries = iface->n_options + 5; |
| 973 | char **ppch; |
| 974 | |
| 975 | if (environ != NULL) { |
| 976 | for (ppch = environ; *ppch; ppch++) { |
| 977 | free(*ppch); |
| 978 | *ppch = NULL; |
| 979 | } |
| 980 | free(environ); |
| 981 | environ = NULL; |
| 982 | } |
| 983 | environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ )); |
| 984 | environend = environ; |
| 985 | *environend = NULL; |
| 986 | |
| 987 | for (i = 0; i < iface->n_options; i++) { |
| 988 | if (strcmp(iface->option[i].name, "up") == 0 |
| 989 | || strcmp(iface->option[i].name, "down") == 0 |
| 990 | || strcmp(iface->option[i].name, "pre-up") == 0 |
| 991 | || strcmp(iface->option[i].name, "post-down") == 0) { |
| 992 | continue; |
| 993 | } |
| 994 | *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); |
| 995 | *environend = NULL; |
| 996 | } |
| 997 | |
| 998 | *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface); |
| 999 | *environend = NULL; |
| 1000 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
| 1001 | *environend = NULL; |
| 1002 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); |
| 1003 | *environend = NULL; |
| 1004 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); |
| 1005 | *environend = NULL; |
| 1006 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
| 1007 | *environend = NULL; |
| 1008 | } |
| 1009 | |
| 1010 | static int doit(char *str) |
| 1011 | { |
| 1012 | if (verbose || no_act) { |
| 1013 | error_msg("%s", str); |
| 1014 | } |
| 1015 | if (!no_act) { |
| 1016 | pid_t child; |
| 1017 | int status; |
| 1018 | |
| 1019 | fflush(NULL); |
| 1020 | switch (child = fork()) { |
| 1021 | case -1: /* failure */ |
| 1022 | return 0; |
| 1023 | case 0: /* child */ |
| 1024 | execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ); |
| 1025 | exit(127); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1026 | } |
| 1027 | waitpid(child, &status, 0); |
| 1028 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1029 | return 0; |
| 1030 | } |
| 1031 | } |
| 1032 | return (1); |
| 1033 | } |
| 1034 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1035 | static int execute_all(interface_defn_t *ifd, execfn *exec, const char *opt) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1036 | { |
| 1037 | int i; |
Glenn L McGrath | 9af8a72 | 2002-11-11 07:03:02 +0000 | [diff] [blame] | 1038 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1039 | |
| 1040 | for (i = 0; i < ifd->n_options; i++) { |
| 1041 | if (strcmp(ifd->option[i].name, opt) == 0) { |
| 1042 | if (!(*exec) (ifd->option[i].value)) { |
| 1043 | return 0; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
Glenn L McGrath | 9af8a72 | 2002-11-11 07:03:02 +0000 | [diff] [blame] | 1048 | buf = xmalloc(xstrlen(opt) + 19); |
| 1049 | sprintf(buf, "/etc/network/if-%s.d", opt); |
Glenn L McGrath | 2e51a14 | 2003-01-20 23:50:59 +0000 | [diff] [blame] | 1050 | run_parts(&buf, 2); |
Glenn L McGrath | 9af8a72 | 2002-11-11 07:03:02 +0000 | [diff] [blame] | 1051 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1052 | return (1); |
| 1053 | } |
| 1054 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1055 | static int iface_up(interface_defn_t *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1056 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1057 | set_environ(iface, "start"); |
| 1058 | if (!execute_all(iface, doit, "pre-up")) { |
| 1059 | return (0); |
| 1060 | } |
| 1061 | if (!iface->method->up(iface, doit)) { |
| 1062 | return (0); |
| 1063 | } |
| 1064 | if (!execute_all(iface, doit, "up")) { |
| 1065 | return (0); |
| 1066 | } |
| 1067 | |
| 1068 | return (1); |
| 1069 | } |
| 1070 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1071 | static int iface_down(interface_defn_t *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1072 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1073 | set_environ(iface, "stop"); |
| 1074 | if (!execute_all(iface, doit, "down")) { |
| 1075 | return (0); |
| 1076 | } |
| 1077 | if (!iface->method->down(iface, doit)) { |
| 1078 | return (0); |
| 1079 | } |
| 1080 | if (!execute_all(iface, doit, "post-down")) { |
| 1081 | return (0); |
| 1082 | } |
| 1083 | return (1); |
| 1084 | } |
| 1085 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1086 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1087 | static int popen2(FILE **in, FILE **out, char *command, ...) |
| 1088 | { |
| 1089 | va_list ap; |
| 1090 | char *argv[11] = { command }; |
| 1091 | int argc; |
| 1092 | int infd[2], outfd[2]; |
| 1093 | pid_t pid; |
| 1094 | |
| 1095 | argc = 1; |
| 1096 | va_start(ap, command); |
| 1097 | while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) { |
| 1098 | argc++; |
| 1099 | } |
| 1100 | argv[argc] = NULL; /* make sure */ |
| 1101 | va_end(ap); |
| 1102 | |
| 1103 | if (pipe(infd) != 0) { |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | if (pipe(outfd) != 0) { |
| 1108 | close(infd[0]); |
| 1109 | close(infd[1]); |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | fflush(NULL); |
| 1114 | switch (pid = fork()) { |
| 1115 | case -1: /* failure */ |
| 1116 | close(infd[0]); |
| 1117 | close(infd[1]); |
| 1118 | close(outfd[0]); |
| 1119 | close(outfd[1]); |
| 1120 | return 0; |
| 1121 | case 0: /* child */ |
| 1122 | dup2(infd[0], 0); |
| 1123 | dup2(outfd[1], 1); |
| 1124 | close(infd[0]); |
| 1125 | close(infd[1]); |
| 1126 | close(outfd[0]); |
| 1127 | close(outfd[1]); |
| 1128 | execvp(command, argv); |
| 1129 | exit(127); |
| 1130 | default: /* parent */ |
| 1131 | *in = fdopen(infd[1], "w"); |
| 1132 | *out = fdopen(outfd[0], "r"); |
| 1133 | close(infd[0]); |
| 1134 | close(outfd[1]); |
| 1135 | return pid; |
| 1136 | } |
| 1137 | /* unreached */ |
| 1138 | } |
| 1139 | |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1140 | static int run_mapping(char *physical, char *logical, int len, mapping_defn_t * map) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1141 | { |
| 1142 | FILE *in, *out; |
| 1143 | int i, status; |
| 1144 | pid_t pid; |
| 1145 | |
| 1146 | |
| 1147 | pid = popen2(&in, &out, map->script, physical, NULL); |
| 1148 | if (pid == 0) { |
| 1149 | return 0; |
| 1150 | } |
| 1151 | for (i = 0; i < map->n_mappings; i++) { |
| 1152 | fprintf(in, "%s\n", map->mapping[i]); |
| 1153 | } |
| 1154 | fclose(in); |
| 1155 | waitpid(pid, &status, 0); |
| 1156 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 1157 | if (fgets(logical, len, out)) { |
| 1158 | char *pch = logical + xstrlen(logical) - 1; |
| 1159 | |
| 1160 | while (pch >= logical && isspace(*pch)) |
| 1161 | *(pch--) = '\0'; |
| 1162 | } |
| 1163 | } |
| 1164 | fclose(out); |
| 1165 | |
| 1166 | return 1; |
| 1167 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1168 | #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1169 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1170 | static llist_t *find_iface_state(llist_t *state_list, const char *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1171 | { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1172 | unsigned short iface_len = xstrlen(iface); |
| 1173 | llist_t *search = state_list; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1174 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1175 | while (search) { |
| 1176 | if ((strncmp(search->data, iface, iface_len) == 0) && |
| 1177 | (search->data[iface_len] == '=')) { |
| 1178 | return(search); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1179 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1180 | search = search->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1181 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1182 | return(NULL); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | extern int ifupdown_main(int argc, char **argv) |
| 1186 | { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1187 | int (*cmds) (interface_defn_t *) = NULL; |
| 1188 | interfaces_file_t *defn; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1189 | FILE *state_fp = NULL; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1190 | llist_t *state_list = NULL; |
| 1191 | llist_t *target_list = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1192 | char *interfaces = "/etc/network/interfaces"; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1193 | const char *statefile = "/var/run/ifstate"; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1194 | |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 1195 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1196 | int run_mappings = 1; |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 1197 | #endif |
| 1198 | int do_all = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1199 | int force = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1200 | int i; |
| 1201 | |
| 1202 | if (applet_name[2] == 'u') { |
| 1203 | /* ifup command */ |
| 1204 | cmds = iface_up; |
| 1205 | } else { |
| 1206 | /* ifdown command */ |
| 1207 | cmds = iface_down; |
| 1208 | } |
| 1209 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1210 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1211 | while ((i = getopt(argc, argv, "i:hvnamf")) != -1) { |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1212 | #else |
| 1213 | while ((i = getopt(argc, argv, "i:hvnaf")) != -1) { |
| 1214 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1215 | switch (i) { |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1216 | case 'i': /* interfaces */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1217 | interfaces = xstrdup(optarg); |
| 1218 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1219 | case 'v': /* verbose */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1220 | verbose = 1; |
| 1221 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1222 | case 'a': /* all */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1223 | do_all = 1; |
| 1224 | break; |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1225 | case 'n': /* no-act */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1226 | no_act = 1; |
| 1227 | break; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1228 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1229 | case 'm': /* no-mappings */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1230 | run_mappings = 0; |
| 1231 | break; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1232 | #endif |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 1233 | case 'f': /* force */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1234 | force = 1; |
| 1235 | break; |
| 1236 | default: |
| 1237 | show_usage(); |
| 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | if (argc - optind > 0) { |
| 1243 | if (do_all) { |
| 1244 | show_usage(); |
| 1245 | } |
| 1246 | } else { |
| 1247 | if (!do_all) { |
| 1248 | show_usage(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | defn = read_interfaces(interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1253 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1254 | if (no_act) { |
| 1255 | state_fp = fopen(statefile, "r"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1258 | /* Create a list of interfaces to work on */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1259 | if (do_all) { |
| 1260 | if (cmds == iface_up) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1261 | target_list = defn->autointerfaces; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1262 | } else if (cmds == iface_down) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1263 | const llist_t *list = state_list; |
| 1264 | while (list) { |
| 1265 | target_list = llist_add_to(target_list, strdup(list->data)); |
| 1266 | list = list->link; |
| 1267 | } |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1268 | target_list = defn->autointerfaces; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1269 | } |
| 1270 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1271 | target_list = llist_add_to(target_list, argv[optind]); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1275 | /* Update the interfaces */ |
| 1276 | while (target_list) { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1277 | interface_defn_t *currif; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1278 | char *iface; |
| 1279 | char *liface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1280 | char *pch; |
| 1281 | int okay = 0; |
| 1282 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1283 | iface = strdup(target_list->data); |
| 1284 | target_list = target_list->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1285 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1286 | pch = strchr(iface, '='); |
| 1287 | if (pch) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1288 | *pch = '\0'; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1289 | liface = strdup(pch + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1290 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1291 | liface = strdup(iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1292 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1293 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1294 | if (!force) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1295 | const llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (cmds == iface_up) { |
| 1298 | /* ifup */ |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1299 | if (iface_state) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1300 | error_msg("interface %s already configured", iface); |
| 1301 | continue; |
| 1302 | } |
| 1303 | } else { |
| 1304 | /* ifdown */ |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1305 | if (iface_state) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1306 | error_msg("interface %s not configured", iface); |
| 1307 | continue; |
| 1308 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1309 | } |
| 1310 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1311 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1312 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1313 | if ((cmds == iface_up) && run_mappings) { |
Glenn L McGrath | 0325a1c | 2002-12-07 07:45:42 +0000 | [diff] [blame] | 1314 | mapping_defn_t *currmap; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1315 | |
| 1316 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
| 1317 | |
| 1318 | for (i = 0; i < currmap->n_matches; i++) { |
| 1319 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
| 1320 | continue; |
| 1321 | if (verbose) { |
| 1322 | error_msg("Running mapping script %s on %s", currmap->script, liface); |
| 1323 | } |
| 1324 | run_mapping(iface, liface, sizeof(liface), currmap); |
| 1325 | break; |
| 1326 | } |
| 1327 | } |
| 1328 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1329 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1330 | |
| 1331 | for (currif = defn->ifaces; currif; currif = currif->next) { |
| 1332 | if (strcmp(liface, currif->iface) == 0) { |
| 1333 | char *oldiface = currif->iface; |
| 1334 | |
| 1335 | okay = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1336 | currif->iface = iface; |
| 1337 | |
| 1338 | if (verbose) { |
| 1339 | error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name); |
| 1340 | } |
| 1341 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1342 | /* Call the cmds function pointer, does either iface_up() or iface_down() */ |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1343 | if (cmds(currif) == -1) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1344 | printf |
| 1345 | ("Don't seem to be have all the variables for %s/%s.\n", |
| 1346 | liface, currif->address_family->name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1347 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1348 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1349 | currif->iface = oldiface; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (!okay && !force) { |
| 1354 | error_msg("Ignoring unknown interface %s=%s.", iface, liface); |
| 1355 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1356 | llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1357 | |
| 1358 | if (cmds == iface_up) { |
| 1359 | char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1); |
| 1360 | sprintf(newiface, "%s=%s", iface, liface); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1361 | if (iface_state == NULL) { |
| 1362 | state_list = llist_add_to(state_list, newiface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1363 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1364 | free(iface_state->data); |
| 1365 | iface_state->data = newiface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } else if (cmds == iface_down) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1368 | /* Remove an interface from the linked list */ |
| 1369 | if (iface_state) { |
| 1370 | /* This needs to be done better */ |
| 1371 | free(iface_state->data); |
| 1372 | free(iface_state->link); |
| 1373 | if (iface_state->link) { |
| 1374 | iface_state->data = iface_state->link->data; |
| 1375 | iface_state->link = iface_state->link->link; |
| 1376 | } else { |
| 1377 | iface_state->data = NULL; |
| 1378 | iface_state->link = NULL; |
| 1379 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1385 | /* Actually write the new state */ |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1386 | if (!no_act) { |
| 1387 | |
| 1388 | if (state_fp) |
| 1389 | fclose(state_fp); |
| 1390 | state_fp = xfopen(statefile, "a+"); |
| 1391 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1392 | if (ftruncate(fileno(state_fp), 0) < 0) { |
| 1393 | error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno)); |
| 1394 | } |
| 1395 | |
| 1396 | rewind(state_fp); |
| 1397 | |
| 1398 | while (state_list) { |
| 1399 | if (state_list->data) { |
| 1400 | fputs(state_list->data, state_fp); |
| 1401 | fputc('\n', state_fp); |
| 1402 | } |
| 1403 | state_list = state_list->link; |
| 1404 | } |
| 1405 | fflush(state_fp); |
| 1406 | } |
| 1407 | |
| 1408 | /* Cleanup */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1409 | if (state_fp != NULL) { |
| 1410 | fclose(state_fp); |
| 1411 | state_fp = NULL; |
| 1412 | } |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |