17
17
#line 3166 "ifupdown.nw"
20
#line 3184 "ifupdown.nw"
20
char *statefile = "/var/run/network/ifstate";
21
char *tmpstatefile = "/var/run/network/.ifstate.tmp";
22
#line 3185 "ifupdown.nw"
21
23
static void usage(char *execname);
22
24
static void help(char *execname);
23
25
static void version(char *execname);
24
#line 3561 "ifupdown.nw"
25
static int lookfor_iface(char **ifaces, int n_ifaces, char *iface);
26
#line 3579 "ifupdown.nw"
27
static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces,
29
#line 3604 "ifupdown.nw"
26
#line 3563 "ifupdown.nw"
27
static const char *read_state(const char *argv0, const char *iface);
28
static void read_all_state(const char *argv0, char ***ifaces, int *n_ifaces);
29
static void update_state(const char *argv0, const char *iface, const char *liface);
30
#line 3682 "ifupdown.nw"
30
31
static int lock_fd (int fd);
31
#line 3190 "ifupdown.nw"
32
#line 3191 "ifupdown.nw"
32
33
static void usage(char *execname) {
33
34
fprintf(stderr, "%s: Use --help for help\n", execname);
36
#line 3197 "ifupdown.nw"
37
#line 3198 "ifupdown.nw"
37
38
static void version(char *execname) {
38
39
printf("%s version " IFUPDOWN_VERSION "\n", execname);
39
40
printf("Copyright (c) 1999-2005 Anthony Towns\n\n");
63
64
printf("\t--force\t\t\tforce de/configuration\n");
66
#line 3565 "ifupdown.nw"
67
static int lookfor_iface(char **ifaces, int n_ifaces, char *iface) {
69
for (i = 0; i < n_ifaces; i++) {
70
if (strncmp(iface, ifaces[i], strlen(iface)) == 0) {
71
if (ifaces[i][strlen(iface)] == '=') {
67
#line 3574 "ifupdown.nw"
69
read_state (const char *argv0, const char *iface)
74
#line 3703 "ifupdown.nw"
79
state_fp = fopen(statefile, no_act ? "r" : "a+");
80
if (state_fp == NULL) {
83
"%s: failed to open statefile %s: %s\n",
84
argv0, statefile, strerror(errno));
94
if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
95
|| fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
97
"%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
98
argv0, statefile, strerror(errno));
102
if (lock_fd (fileno(state_fp)) < 0) {
104
"%s: failed to lock statefile %s: %s\n",
105
argv0, statefile, strerror(errno));
110
#line 3581 "ifupdown.nw"
111
while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
113
#line 3740 "ifupdown.nw"
116
pch = buf + strlen(buf) - 1;
117
while(pch > buf && isspace(*pch)) pch--;
121
while(isspace(*pch)) pch++;
78
123
#line 3584 "ifupdown.nw"
79
static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces,
82
assert(*max_ifaces >= *n_ifaces);
83
if (*max_ifaces == *n_ifaces) {
84
*max_ifaces = (*max_ifaces * 2) + 1;
85
*ifaces = realloc(*ifaces, sizeof(**ifaces) * *max_ifaces);
86
if (*ifaces == NULL) {
92
(*ifaces)[(*n_ifaces)++] = new_iface;
94
#line 3608 "ifupdown.nw"
124
if (strncmp(iface, pch, strlen(iface)) == 0) {
125
if (pch[strlen(iface)] == '=') {
126
ret = pch + strlen(iface) + 1;
133
#line 3751 "ifupdown.nw"
135
if (state_fp != NULL) {
140
#line 3594 "ifupdown.nw"
143
#line 3602 "ifupdown.nw"
145
read_all_state (const char *argv0, char ***ifaces, int *n_ifaces)
148
#line 3703 "ifupdown.nw"
153
state_fp = fopen(statefile, no_act ? "r" : "a+");
154
if (state_fp == NULL) {
157
"%s: failed to open statefile %s: %s\n",
158
argv0, statefile, strerror(errno));
168
if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
169
|| fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
171
"%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
172
argv0, statefile, strerror(errno));
176
if (lock_fd (fileno(state_fp)) < 0) {
178
"%s: failed to lock statefile %s: %s\n",
179
argv0, statefile, strerror(errno));
184
#line 3607 "ifupdown.nw"
188
while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
190
#line 3740 "ifupdown.nw"
193
pch = buf + strlen(buf) - 1;
194
while(pch > buf && isspace(*pch)) pch--;
198
while(isspace(*pch)) pch++;
200
#line 3613 "ifupdown.nw"
202
*ifaces = realloc (*ifaces, sizeof (**ifaces) * *n_ifaces);
203
(*ifaces)[(*n_ifaces)-1] = strdup (pch);
207
#line 3751 "ifupdown.nw"
209
if (state_fp != NULL) {
213
#line 3619 "ifupdown.nw"
215
#line 3629 "ifupdown.nw"
216
static void update_state(const char *argv0, const char *iface, const char *state)
221
#line 3703 "ifupdown.nw"
226
state_fp = fopen(statefile, no_act ? "r" : "a+");
227
if (state_fp == NULL) {
230
"%s: failed to open statefile %s: %s\n",
231
argv0, statefile, strerror(errno));
241
if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
242
|| fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
244
"%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
245
argv0, statefile, strerror(errno));
249
if (lock_fd (fileno(state_fp)) < 0) {
251
"%s: failed to lock statefile %s: %s\n",
252
argv0, statefile, strerror(errno));
257
#line 3635 "ifupdown.nw"
261
tmp_fp = fopen(tmpstatefile, "w");
262
if (tmp_fp == NULL) {
264
"%s: failed to open temporary statefile %s: %s\n",
265
argv0, tmpstatefile, strerror(errno));
269
while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
271
#line 3740 "ifupdown.nw"
274
pch = buf + strlen(buf) - 1;
275
while(pch > buf && isspace(*pch)) pch--;
279
while(isspace(*pch)) pch++;
281
#line 3649 "ifupdown.nw"
282
if (strncmp(iface, pch, strlen(iface)) == 0) {
283
if (pch[strlen(iface)] == '=') {
285
fprintf (tmp_fp, "%s=%s\n",
294
fprintf (tmp_fp, "%s\n", pch);
298
fprintf (tmp_fp, "%s=%s\n", iface, state);
301
if (rename (tmpstatefile, statefile)) {
303
"%s: failed to overwrite statefile %s: %s\n",
304
argv0, statefile, strerror(errno));
309
#line 3751 "ifupdown.nw"
311
if (state_fp != NULL) {
315
#line 3676 "ifupdown.nw"
317
#line 3686 "ifupdown.nw"
95
318
static int lock_fd (int fd) {
96
319
struct flock lock;
274
490
#line 2999 "ifupdown.nw"
276
#line 3630 "ifupdown.nw"
278
state_fp = fopen(statefile, no_act ? "r" : "a+");
279
if (state_fp == NULL && !no_act) {
281
"%s: failed to open statefile %s: %s\n",
282
argv[0], statefile, strerror(errno));
286
if (state_fp != NULL) {
293
if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
294
|| fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
296
"%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
297
argv[0], statefile, strerror(errno));
301
if (lock_fd (fileno(state_fp)) < 0) {
303
"%s: failed to lock statefile %s: %s\n",
304
argv[0], statefile, strerror(errno));
311
while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
314
pch = buf + strlen(buf) - 1;
315
while(pch > buf && isspace(*pch)) pch--;
319
while(isspace(*pch)) pch++;
321
add_to_state(&state, &n_state, &max_state, strdup(pch));
325
#line 3508 "ifupdown.nw"
492
#line 3512 "ifupdown.nw"
328
495
#line 3087 "ifupdown.nw"
329
496
(cmds == iface_up)
330
#line 3509 "ifupdown.nw"
497
#line 3513 "ifupdown.nw"
332
499
allowup_defn *autos = find_allowup(defn, "auto");
333
500
target_iface = autos ? autos->interfaces : NULL;
529
726
if (!okay && !force) {
530
727
fprintf(stderr, "Ignoring unknown interface %s=%s.\n",
729
update_state (argv[0], iface, NULL);
534
#line 3745 "ifupdown.nw"
732
#line 3793 "ifupdown.nw"
536
int already_up = lookfor_iface(state, n_state, iface);
539
735
#line 3087 "ifupdown.nw"
540
736
(cmds == iface_up)
541
#line 3748 "ifupdown.nw"
737
#line 3794 "ifupdown.nw"
544
malloc(strlen(iface) + 1 + strlen(liface) + 1);
545
sprintf(newiface, "%s=%s", iface, liface);
547
if (already_up == -1) {
739
if (current_state == NULL) {
548
740
if (failed == 1) {
549
741
printf("Failed to bring up %s.\n", liface);
742
update_state (argv[0], iface, NULL);
551
add_to_state(&state, &n_state, &max_state, newiface);
744
update_state (argv[0], iface, liface);
554
free(state[already_up]);
555
state[already_up] = newiface;
747
update_state (argv[0], iface, liface);
558
750
#line 3091 "ifupdown.nw"
559
751
(cmds == iface_down)
560
#line 3763 "ifupdown.nw"
752
#line 3805 "ifupdown.nw"
562
if (already_up != -1) {
563
state[already_up] = state[--n_state];
754
update_state (argv[0], iface, NULL);
569
#line 3452 "ifupdown.nw"
572
#line 3379 "ifupdown.nw"
574
#line 3689 "ifupdown.nw"
575
if (state_fp != NULL && !no_act) {
578
if (ftruncate(fileno(state_fp), 0) < 0)
581
"%s: failed to truncate statefile %s: %s\n",
582
argv[0], statefile, strerror(errno));
587
for (i = 0; i < n_state; i++) {
588
fprintf(state_fp, "%s\n", state[i]);
592
#line 3380 "ifupdown.nw"
595
#line 3681 "ifupdown.nw"
596
if (state_fp != NULL) {
759
#line 3456 "ifupdown.nw"
762
#line 3381 "ifupdown.nw"
601
766
#line 3001 "ifupdown.nw"