~ubuntu-branches/ubuntu/jaunty/ifupdown/jaunty-201309120846

« back to all changes in this revision

Viewing changes to main.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2006-01-26 15:56:54 UTC
  • Revision ID: james.westby@ubuntu.com-20060126155654-65t7t24rgpsm669d
Tags: 0.6.7ubuntu6
* Rewrite the way that ifup and ifdown read and write the state file.
  Instead of storing it in memory and holding a lock on it (preventing
  concurrent processes from actually being concurrent) use atomic read and
  write functions that only hold the lock for very short periods.

* Write to the state file when we start bringing up or tearing down the
  interface, so we don't ever try to do the same operation at the same
  time.
* Update the state file once the operation is complete to ensure we
  record the actual status of it.

* Restore the dhcp wait, so "ifup eth0" will hang around until DHCP is
  either successful or times out.  Adjust the udev rule again so that
  udevplug doesn't hang around.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#line 3166 "ifupdown.nw"
18
18
int no_act = 0;
19
19
int verbose = 0;
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, 
28
 
                         char *new_iface);
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);
34
35
        exit(1);
35
36
}
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");
47
48
        );
48
49
        exit(0);
49
50
}
50
 
#line 3213 "ifupdown.nw"
 
51
#line 3214 "ifupdown.nw"
51
52
static void help(char *execname) {
52
53
        printf("Usage: %s <options> <ifaces...>\n\n", execname);
53
54
        printf("Options:\n");
63
64
        printf("\t--force\t\t\tforce de/configuration\n");
64
65
        exit(0);
65
66
}
66
 
#line 3565 "ifupdown.nw"
67
 
static int lookfor_iface(char **ifaces, int n_ifaces, char *iface) {
68
 
        int i;
69
 
        for (i = 0; i < n_ifaces; i++) {
70
 
                if (strncmp(iface, ifaces[i], strlen(iface)) == 0) {
71
 
                        if (ifaces[i][strlen(iface)] == '=') {
72
 
                                return i;
73
 
                        }
74
 
                }
75
 
        }
76
 
        return -1;
77
 
}
 
67
#line 3574 "ifupdown.nw"
 
68
static const char *
 
69
read_state (const char *argv0, const char *iface)
 
70
{
 
71
        char *ret = NULL;
 
72
 
 
73
        
 
74
#line 3703 "ifupdown.nw"
 
75
FILE *state_fp;
 
76
char buf[80];
 
77
char *p;
 
78
 
 
79
state_fp = fopen(statefile, no_act ? "r" : "a+");
 
80
if (state_fp == NULL) {
 
81
        if (!no_act) {
 
82
                fprintf(stderr, 
 
83
                        "%s: failed to open statefile %s: %s\n",
 
84
                        argv0, statefile, strerror(errno));
 
85
                exit (1);
 
86
        } else {
 
87
                goto noact;
 
88
        }
 
89
}
 
90
 
 
91
if (!no_act) {
 
92
        int flags;
 
93
 
 
94
        if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
 
95
            || fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
 
96
                fprintf(stderr, 
 
97
                        "%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
 
98
                        argv0, statefile, strerror(errno));
 
99
                exit(1);
 
100
        }
 
101
 
 
102
        if (lock_fd (fileno(state_fp)) < 0) {
 
103
                fprintf(stderr, 
 
104
                        "%s: failed to lock statefile %s: %s\n",
 
105
                        argv0, statefile, strerror(errno));
 
106
                exit(1);
 
107
        }
 
108
}
 
109
 
 
110
#line 3581 "ifupdown.nw"
 
111
        while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
 
112
                
 
113
#line 3740 "ifupdown.nw"
 
114
char *pch;
 
115
 
 
116
pch = buf + strlen(buf) - 1;
 
117
while(pch > buf && isspace(*pch)) pch--;
 
118
*(pch+1) = '\0';
 
119
 
 
120
pch = buf;
 
121
while(isspace(*pch)) pch++;
 
122
 
78
123
#line 3584 "ifupdown.nw"
79
 
static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, 
80
 
                         char *new_iface)
81
 
{
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) {
87
 
                        perror("realloc");
88
 
                        exit(1);
89
 
                }
90
 
        }
91
 
 
92
 
        (*ifaces)[(*n_ifaces)++] = new_iface;
93
 
}
94
 
#line 3608 "ifupdown.nw"
 
124
                if (strncmp(iface, pch, strlen(iface)) == 0) {
 
125
                        if (pch[strlen(iface)] == '=') {
 
126
                                ret = pch + strlen(iface) + 1;
 
127
                                break;
 
128
                        }
 
129
                }
 
130
        }
 
131
 
 
132
        
 
133
#line 3751 "ifupdown.nw"
 
134
noact:
 
135
if (state_fp != NULL) {
 
136
        fclose(state_fp);
 
137
        state_fp = NULL;
 
138
}
 
139
 
 
140
#line 3594 "ifupdown.nw"
 
141
        return ret;
 
142
}
 
143
#line 3602 "ifupdown.nw"
 
144
static void
 
145
read_all_state (const char *argv0, char ***ifaces, int *n_ifaces)
 
146
{
 
147
        
 
148
#line 3703 "ifupdown.nw"
 
149
FILE *state_fp;
 
150
char buf[80];
 
151
char *p;
 
152
 
 
153
state_fp = fopen(statefile, no_act ? "r" : "a+");
 
154
if (state_fp == NULL) {
 
155
        if (!no_act) {
 
156
                fprintf(stderr, 
 
157
                        "%s: failed to open statefile %s: %s\n",
 
158
                        argv0, statefile, strerror(errno));
 
159
                exit (1);
 
160
        } else {
 
161
                goto noact;
 
162
        }
 
163
}
 
164
 
 
165
if (!no_act) {
 
166
        int flags;
 
167
 
 
168
        if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
 
169
            || fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
 
170
                fprintf(stderr, 
 
171
                        "%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
 
172
                        argv0, statefile, strerror(errno));
 
173
                exit(1);
 
174
        }
 
175
 
 
176
        if (lock_fd (fileno(state_fp)) < 0) {
 
177
                fprintf(stderr, 
 
178
                        "%s: failed to lock statefile %s: %s\n",
 
179
                        argv0, statefile, strerror(errno));
 
180
                exit(1);
 
181
        }
 
182
}
 
183
 
 
184
#line 3607 "ifupdown.nw"
 
185
        *n_ifaces = 0;
 
186
        *ifaces = NULL;
 
187
 
 
188
        while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
 
189
                
 
190
#line 3740 "ifupdown.nw"
 
191
char *pch;
 
192
 
 
193
pch = buf + strlen(buf) - 1;
 
194
while(pch > buf && isspace(*pch)) pch--;
 
195
*(pch+1) = '\0';
 
196
 
 
197
pch = buf;
 
198
while(isspace(*pch)) pch++;
 
199
 
 
200
#line 3613 "ifupdown.nw"
 
201
                (*n_ifaces)++;
 
202
                *ifaces = realloc (*ifaces, sizeof (**ifaces) * *n_ifaces);
 
203
                (*ifaces)[(*n_ifaces)-1] = strdup (pch);
 
204
        }
 
205
 
 
206
        
 
207
#line 3751 "ifupdown.nw"
 
208
noact:
 
209
if (state_fp != NULL) {
 
210
        fclose(state_fp);
 
211
        state_fp = NULL;
 
212
}
 
213
#line 3619 "ifupdown.nw"
 
214
}
 
215
#line 3629 "ifupdown.nw"
 
216
static void update_state(const char *argv0, const char *iface, const char *state)
 
217
{
 
218
        FILE *tmp_fp;
 
219
 
 
220
        
 
221
#line 3703 "ifupdown.nw"
 
222
FILE *state_fp;
 
223
char buf[80];
 
224
char *p;
 
225
 
 
226
state_fp = fopen(statefile, no_act ? "r" : "a+");
 
227
if (state_fp == NULL) {
 
228
        if (!no_act) {
 
229
                fprintf(stderr, 
 
230
                        "%s: failed to open statefile %s: %s\n",
 
231
                        argv0, statefile, strerror(errno));
 
232
                exit (1);
 
233
        } else {
 
234
                goto noact;
 
235
        }
 
236
}
 
237
 
 
238
if (!no_act) {
 
239
        int flags;
 
240
 
 
241
        if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
 
242
            || fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
 
243
                fprintf(stderr, 
 
244
                        "%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
 
245
                        argv0, statefile, strerror(errno));
 
246
                exit(1);
 
247
        }
 
248
 
 
249
        if (lock_fd (fileno(state_fp)) < 0) {
 
250
                fprintf(stderr, 
 
251
                        "%s: failed to lock statefile %s: %s\n",
 
252
                        argv0, statefile, strerror(errno));
 
253
                exit(1);
 
254
        }
 
255
}
 
256
 
 
257
#line 3635 "ifupdown.nw"
 
258
        if (no_act)
 
259
                goto noact;
 
260
 
 
261
        tmp_fp = fopen(tmpstatefile, "w");
 
262
        if (tmp_fp == NULL) {
 
263
                fprintf(stderr, 
 
264
                        "%s: failed to open temporary statefile %s: %s\n",
 
265
                        argv0, tmpstatefile, strerror(errno));
 
266
                exit (1);
 
267
        }
 
268
 
 
269
        while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
 
270
                
 
271
#line 3740 "ifupdown.nw"
 
272
char *pch;
 
273
 
 
274
pch = buf + strlen(buf) - 1;
 
275
while(pch > buf && isspace(*pch)) pch--;
 
276
*(pch+1) = '\0';
 
277
 
 
278
pch = buf;
 
279
while(isspace(*pch)) pch++;
 
280
 
 
281
#line 3649 "ifupdown.nw"
 
282
                if (strncmp(iface, pch, strlen(iface)) == 0) {
 
283
                        if (pch[strlen(iface)] == '=') {
 
284
                                if (state != NULL) {
 
285
                                        fprintf (tmp_fp, "%s=%s\n",
 
286
                                                 iface, state);
 
287
                                        state = NULL;
 
288
                                }
 
289
 
 
290
                                continue;
 
291
                        }
 
292
                }
 
293
 
 
294
                fprintf (tmp_fp, "%s\n", pch);
 
295
        }
 
296
 
 
297
        if (state != NULL)
 
298
                fprintf (tmp_fp, "%s=%s\n", iface, state);
 
299
 
 
300
        fclose (tmp_fp);
 
301
        if (rename (tmpstatefile, statefile)) {
 
302
                fprintf(stderr, 
 
303
                        "%s: failed to overwrite statefile %s: %s\n",
 
304
                        argv0, statefile, strerror(errno));
 
305
                exit (1);
 
306
        }
 
307
 
 
308
        
 
309
#line 3751 "ifupdown.nw"
 
310
noact:
 
311
if (state_fp != NULL) {
 
312
        fclose(state_fp);
 
313
        state_fp = NULL;
 
314
}
 
315
#line 3676 "ifupdown.nw"
 
316
}
 
317
#line 3686 "ifupdown.nw"
95
318
static int lock_fd (int fd) {
96
319
        struct flock lock;
97
320
 
125
348
        {"force",       no_argument,       NULL,  2 },
126
349
        {0,0,0,0}
127
350
};
128
 
#line 3171 "ifupdown.nw"
 
351
#line 3173 "ifupdown.nw"
129
352
int do_all = 0;
130
353
int run_mappings = 1;
131
354
int force = 0;
132
355
char *allow_class = NULL;
133
356
char *interfaces = "/etc/network/interfaces";
134
 
char *statefile = "/var/run/network/ifstate";
135
357
char *excludeint = NULL ;
136
 
#line 3338 "ifupdown.nw"
 
358
#line 3339 "ifupdown.nw"
137
359
interfaces_file *defn;
138
 
#line 3497 "ifupdown.nw"
 
360
#line 3501 "ifupdown.nw"
139
361
int n_target_ifaces;
140
362
char **target_iface;
141
 
#line 3552 "ifupdown.nw"
142
 
char **state = NULL; /* list of iface=liface */
143
 
int n_state = 0;
144
 
int max_state = 0;
145
 
#line 3626 "ifupdown.nw"
146
 
static FILE *state_fp = NULL;
147
363
 
148
364
#line 2992 "ifupdown.nw"
149
365
        
196
412
}
197
413
#line 2995 "ifupdown.nw"
198
414
        
199
 
#line 3234 "ifupdown.nw"
 
415
#line 3235 "ifupdown.nw"
200
416
for(;;) {
201
417
        int c;
202
418
        c = getopt_long(argc, argv, "e:s:i:hVvna", long_opts, NULL);
204
420
 
205
421
        switch(c) {
206
422
                
207
 
#line 3252 "ifupdown.nw"
 
423
#line 3253 "ifupdown.nw"
208
424
case 'i':
209
425
        interfaces = strdup(optarg);
210
426
        break;
211
 
#line 3257 "ifupdown.nw"
 
427
#line 3258 "ifupdown.nw"
212
428
case 'v':
213
429
        verbose = 1;
214
430
        break;
215
 
#line 3262 "ifupdown.nw"
 
431
#line 3263 "ifupdown.nw"
216
432
case 'a':
217
433
        do_all = 1;
218
434
        break;
219
 
#line 3267 "ifupdown.nw"
 
435
#line 3268 "ifupdown.nw"
220
436
case 3:
221
437
        allow_class = strdup(optarg);
222
438
        break;
223
 
#line 3272 "ifupdown.nw"
 
439
#line 3273 "ifupdown.nw"
224
440
case 'n':
225
441
        no_act = 1;
226
442
        break;
227
 
#line 3277 "ifupdown.nw"
 
443
#line 3278 "ifupdown.nw"
228
444
case 1:
229
445
        run_mappings = 0;
230
446
        break;
231
 
#line 3282 "ifupdown.nw"
 
447
#line 3283 "ifupdown.nw"
232
448
case 2:
233
449
        force = 1;
234
450
        break;
235
 
#line 3287 "ifupdown.nw"
 
451
#line 3288 "ifupdown.nw"
236
452
case 'e':
237
453
        excludeint = strdup(optarg);
238
454
        break;
239
 
#line 3295 "ifupdown.nw"
 
455
#line 3296 "ifupdown.nw"
240
456
case 'h':
241
457
        help(argv[0]);
242
458
        break;
243
 
#line 3300 "ifupdown.nw"
 
459
#line 3301 "ifupdown.nw"
244
460
case 'V':
245
461
        version(argv[0]);
246
462
        break;
247
 
#line 3309 "ifupdown.nw"
 
463
#line 3310 "ifupdown.nw"
248
464
default:
249
465
        usage(argv[0]);
250
466
        break;
251
 
#line 3241 "ifupdown.nw"
 
467
#line 3242 "ifupdown.nw"
252
468
        }
253
469
}
254
470
 
255
 
#line 3321 "ifupdown.nw"
 
471
#line 3322 "ifupdown.nw"
256
472
if (argc - optind > 0 && do_all) {
257
473
        usage(argv[0]);
258
474
}
259
 
#line 3327 "ifupdown.nw"
 
475
#line 3328 "ifupdown.nw"
260
476
if (argc - optind == 0 && !do_all) {
261
477
        usage(argv[0]);
262
478
}
263
479
 
264
480
#line 2997 "ifupdown.nw"
265
481
        
266
 
#line 3342 "ifupdown.nw"
 
482
#line 3343 "ifupdown.nw"
267
483
defn = read_interfaces(interfaces);
268
484
if ( !defn ) {
269
485
        fprintf(stderr, "%s: couldn't read interfaces file \"%s\"\n",
273
489
 
274
490
#line 2999 "ifupdown.nw"
275
491
        
276
 
#line 3630 "ifupdown.nw"
277
 
{
278
 
        state_fp = fopen(statefile, no_act ? "r" : "a+");
279
 
        if (state_fp == NULL && !no_act) {
280
 
                fprintf(stderr, 
281
 
                        "%s: failed to open statefile %s: %s\n",
282
 
                        argv[0], statefile, strerror(errno));
283
 
                exit (1);
284
 
        }
285
 
 
286
 
        if (state_fp != NULL) {
287
 
                char buf[80];
288
 
                char *p;
289
 
 
290
 
                if (!no_act) {
291
 
                        int flags;
292
 
 
293
 
                        if ((flags = fcntl(fileno(state_fp), F_GETFD)) < 0
294
 
                            || fcntl(fileno(state_fp), F_SETFD, flags | FD_CLOEXEC) < 0) {
295
 
                                fprintf(stderr, 
296
 
                                        "%s: failed to set FD_CLOEXEC on statefile %s: %s\n",
297
 
                                        argv[0], statefile, strerror(errno));
298
 
                                exit(1);
299
 
                        }
300
 
 
301
 
                        if (lock_fd (fileno(state_fp)) < 0) {
302
 
                                fprintf(stderr, 
303
 
                                        "%s: failed to lock statefile %s: %s\n",
304
 
                                        argv[0], statefile, strerror(errno));
305
 
                                exit(1);
306
 
                        }
307
 
 
308
 
                }
309
 
 
310
 
                rewind (state_fp);
311
 
                while((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
312
 
                        char *pch;
313
 
 
314
 
                        pch = buf + strlen(buf) - 1;
315
 
                        while(pch > buf && isspace(*pch)) pch--;
316
 
                        *(pch+1) = '\0';
317
 
 
318
 
                        pch = buf;
319
 
                        while(isspace(*pch)) pch++;
320
 
 
321
 
                        add_to_state(&state, &n_state, &max_state, strdup(pch));
322
 
                }
323
 
        }
324
 
}
325
 
#line 3508 "ifupdown.nw"
 
492
#line 3512 "ifupdown.nw"
326
493
if (do_all) {
327
494
        if (
328
495
#line 3087 "ifupdown.nw"
329
496
(cmds == iface_up)
330
 
#line 3509 "ifupdown.nw"
 
497
#line 3513 "ifupdown.nw"
331
498
                                     ) {
332
499
                allowup_defn *autos = find_allowup(defn, "auto");
333
500
                target_iface = autos ? autos->interfaces : NULL;
335
502
        } else if (
336
503
#line 3091 "ifupdown.nw"
337
504
(cmds == iface_down)
338
 
#line 3513 "ifupdown.nw"
 
505
#line 3517 "ifupdown.nw"
339
506
                                            ) {
340
 
                target_iface = state;
341
 
                n_target_ifaces = n_state;
 
507
                read_all_state(argv[0], &target_iface, &n_target_ifaces);
342
508
        } else {
343
509
                assert(0);
344
510
        }       
350
516
{
351
517
        int i;
352
518
        for (
353
 
#line 3502 "ifupdown.nw"
 
519
#line 3506 "ifupdown.nw"
354
520
i = 0; i < n_target_ifaces; i++
355
521
#line 3359 "ifupdown.nw"
356
522
                                      ) {
357
523
                char iface[80], liface[80];
 
524
                const char *current_state;
358
525
 
359
526
                
360
 
#line 3526 "ifupdown.nw"
 
527
#line 3529 "ifupdown.nw"
361
528
strncpy(iface, target_iface[i], sizeof(iface));
362
529
iface[sizeof(iface)-1] = '\0';
363
530
 
372
539
                liface[sizeof(liface)-1] = '\0';
373
540
        }
374
541
}
375
 
#line 3363 "ifupdown.nw"
 
542
#line 3364 "ifupdown.nw"
 
543
                current_state = read_state(argv[0], iface);
376
544
                if (!force) {
377
545
                        
378
 
#line 3713 "ifupdown.nw"
 
546
#line 3763 "ifupdown.nw"
379
547
{
380
 
        int already_up = lookfor_iface(state, n_state, iface);;
381
 
 
382
548
        if (
383
549
#line 3087 "ifupdown.nw"
384
550
(cmds == iface_up)
385
 
#line 3716 "ifupdown.nw"
 
551
#line 3764 "ifupdown.nw"
386
552
                                     ) {
387
 
                if (already_up != -1) {
388
 
                        if (do_all == 0) {
 
553
                if (current_state != NULL) {
 
554
                        if (!do_all) {
389
555
                                fprintf(stderr, 
390
556
                                        "%s: interface %s already configured\n",
391
557
                                        argv[0], iface);
395
561
        } else if (
396
562
#line 3091 "ifupdown.nw"
397
563
(cmds == iface_down)
398
 
#line 3725 "ifupdown.nw"
 
564
#line 3773 "ifupdown.nw"
399
565
                                            ) {
400
 
                if (already_up == -1) {
401
 
                        if (do_all == 0) {
 
566
                if (current_state == NULL) {
 
567
                        if (!do_all) {
402
568
                                fprintf(stderr, "%s: interface %s not configured\n",
403
569
                                        argv[0], iface);
404
570
                        }
405
571
                        continue;
406
572
                }
407
 
                strncpy(liface, strchr(state[already_up], '=') + 1, 80);
 
573
                strncpy(liface, current_state, 80);
408
574
                liface[79] = 0;
409
575
        } else {
410
576
                assert(0);
411
577
        }
412
578
}
413
 
#line 3365 "ifupdown.nw"
 
579
#line 3367 "ifupdown.nw"
414
580
                }
415
581
 
416
582
                if (
417
583
#line 3100 "ifupdown.nw"
418
584
(allow_class != NULL)
419
585
 
420
 
#line 3367 "ifupdown.nw"
 
586
#line 3369 "ifupdown.nw"
421
587
                                                    ) {
422
588
                        
423
589
#line 3103 "ifupdown.nw"
434
600
        if (i >= allowup->n_interfaces)
435
601
                continue;
436
602
}
437
 
#line 3369 "ifupdown.nw"
 
603
#line 3371 "ifupdown.nw"
438
604
                }
439
605
 
440
606
                if (
441
607
#line 3125 "ifupdown.nw"
442
608
(excludeint != NULL && strstr(iface,excludeint) != NULL)
443
 
#line 3371 "ifupdown.nw"
 
609
#line 3373 "ifupdown.nw"
444
610
                                               )  
445
611
                        continue;
446
612
 
447
613
                if (
448
614
#line 3087 "ifupdown.nw"
449
615
(cmds == iface_up)
450
 
#line 3374 "ifupdown.nw"
 
616
#line 3376 "ifupdown.nw"
451
617
                                       && run_mappings) {
452
618
                        
453
619
#line 3392 "ifupdown.nw"
473
639
                }
474
640
        }
475
641
}
476
 
#line 3376 "ifupdown.nw"
 
642
#line 3378 "ifupdown.nw"
477
643
                }
478
644
 
479
645
                
482
648
        interface_defn *currif;
483
649
        int okay = 0;
484
650
        int failed = 0; 
 
651
        
 
652
        
 
653
#line 3793 "ifupdown.nw"
 
654
{
 
655
        if (
 
656
#line 3087 "ifupdown.nw"
 
657
(cmds == iface_up)
 
658
#line 3794 "ifupdown.nw"
 
659
                                     ) {
 
660
                if (current_state == NULL) {
 
661
                        if (failed == 1) {
 
662
                                printf("Failed to bring up %s.\n", liface);
 
663
                                update_state (argv[0], iface, NULL);
 
664
                        } else {
 
665
                                update_state (argv[0], iface, liface);
 
666
                        }
 
667
                } else {
 
668
                        update_state (argv[0], iface, liface);
 
669
                }
 
670
        } else if (
 
671
#line 3091 "ifupdown.nw"
 
672
(cmds == iface_down)
 
673
#line 3805 "ifupdown.nw"
 
674
                                            ) {
 
675
                update_state (argv[0], iface, NULL);
 
676
        } else {
 
677
                assert(0);
 
678
        }
 
679
}
 
680
 
 
681
#line 3438 "ifupdown.nw"
485
682
        for (currif = defn->ifaces; currif; currif = currif->next) {
486
683
                if (strcmp(liface, currif->logical_iface) == 0) {
487
684
                        okay = 1;
488
685
 
489
686
                        
490
 
#line 3457 "ifupdown.nw"
 
687
#line 3461 "ifupdown.nw"
491
688
{
492
689
        currif->real_iface = iface;
493
690
 
519
716
        currif->real_iface = NULL;
520
717
}
521
718
 
522
 
#line 3441 "ifupdown.nw"
 
719
#line 3444 "ifupdown.nw"
523
720
                        if (failed) break;
524
721
                        /* Otherwise keep going: this interface may have
525
722
                         * match with other address families */
529
726
        if (!okay && !force) {
530
727
                fprintf(stderr, "Ignoring unknown interface %s=%s.\n", 
531
728
                        iface, liface);
 
729
                update_state (argv[0], iface, NULL);
532
730
        } else {
533
731
                
534
 
#line 3745 "ifupdown.nw"
 
732
#line 3793 "ifupdown.nw"
535
733
{
536
 
        int already_up = lookfor_iface(state, n_state, iface);
537
 
 
538
734
        if (
539
735
#line 3087 "ifupdown.nw"
540
736
(cmds == iface_up)
541
 
#line 3748 "ifupdown.nw"
 
737
#line 3794 "ifupdown.nw"
542
738
                                     ) {
543
 
                char *newiface = 
544
 
                        malloc(strlen(iface) + 1 + strlen(liface) + 1);
545
 
                sprintf(newiface, "%s=%s", iface, liface);
546
 
 
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);
550
743
                        } else {
551
 
                                add_to_state(&state, &n_state, &max_state, newiface);
 
744
                                update_state (argv[0], iface, liface);
552
745
                        }
553
746
                } else {
554
 
                        free(state[already_up]);
555
 
                        state[already_up] = newiface;
 
747
                        update_state (argv[0], iface, liface);
556
748
                }
557
749
        } else if (
558
750
#line 3091 "ifupdown.nw"
559
751
(cmds == iface_down)
560
 
#line 3763 "ifupdown.nw"
 
752
#line 3805 "ifupdown.nw"
561
753
                                            ) {
562
 
                if (already_up != -1) {
563
 
                        state[already_up] = state[--n_state];
564
 
                }
 
754
                update_state (argv[0], iface, NULL);
565
755
        } else {
566
756
                assert(0);
567
757
        }
568
758
}
569
 
#line 3452 "ifupdown.nw"
570
 
        }
571
 
}
572
 
#line 3379 "ifupdown.nw"
573
 
                
574
 
#line 3689 "ifupdown.nw"
575
 
if (state_fp != NULL && !no_act) {
576
 
        int i;
577
 
 
578
 
        if (ftruncate(fileno(state_fp), 0) < 0)
579
 
        {
580
 
                fprintf(stderr, 
581
 
                        "%s: failed to truncate statefile %s: %s\n",
582
 
                        argv[0], statefile, strerror(errno));
583
 
                exit(1);
584
 
        }
585
 
 
586
 
        rewind(state_fp);
587
 
        for (i = 0; i < n_state; i++) {
588
 
                fprintf(state_fp, "%s\n", state[i]);
589
 
        }
590
 
        fflush(state_fp);
591
 
}
592
 
#line 3380 "ifupdown.nw"
593
 
        }
594
 
}
595
 
#line 3681 "ifupdown.nw"
596
 
if (state_fp != NULL) {
597
 
        fclose(state_fp);
598
 
        state_fp = NULL;
 
759
#line 3456 "ifupdown.nw"
 
760
        }
 
761
}
 
762
#line 3381 "ifupdown.nw"
 
763
        }
599
764
}
600
765
 
601
766
#line 3001 "ifupdown.nw"