netplug / main.c

Bryan O'Sullivan 4a362d0 


Bryan O'Sullivan 62ecaca 
Bryan O'Sullivan e23a659 
Bryan O'Sullivan cf610f9 
Bryan O'Sullivan 4a362d0 













Bryan O'Sullivan 94ff30b 

Bryan O'Sullivan cf610f9 
Bryan O'Sullivan ffe920b 

Bryan O'Sullivan a836f69 
Bryan O'Sullivan 537d0a8 
Bryan O'Sullivan b08cd5e 





Bryan O'Sullivan cf610f9 

Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 7d6af78 

Bryan O'Sullivan 718753e 
Bryan O'Sullivan 691f74d 
Bryan O'Sullivan 57830f5 
Bryan O'Sullivan a836f69 
Bryan O'Sullivan 537d0a8 

Bryan O'Sullivan ffe920b 

Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 


Bryan O'Sullivan 94ff30b 
jsgf 168bdba 
Bryan O'Sullivan 94ff30b 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 09b6082 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 




Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan d085dba 
Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 4a362d0 
jsgf 168bdba 


Bryan O'Sullivan 9b7bc56 
Bryan O'Sullivan cf610f9 
jsgf 168bdba 

Bryan O'Sullivan b08cd5e 
jsgf a3d5d52 
Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan e3f9dd1 
Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan ffe920b 



Bryan O'Sullivan 537d0a8 
Bryan O'Sullivan f38be18 
Bryan O'Sullivan 537d0a8 
Bryan O'Sullivan 57830f5 

Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 62ecaca 

Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 684eb7c 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 684eb7c 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 537d0a8 



Bryan O'Sullivan 684eb7c 
Bryan O'Sullivan 57830f5 
Bryan O'Sullivan 684eb7c 



Bryan O'Sullivan 4a362d0 

Bryan O'Sullivan 684eb7c 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 684eb7c 



Bryan O'Sullivan 57830f5 
















Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan cf610f9 

Bryan O'Sullivan b08cd5e 








Romain Perier 72770e9 
Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan cf610f9 
Bryan O'Sullivan b08cd5e 


Romain Perier 72770e9 








Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan cf610f9 












Bryan O'Sullivan c9881ee 
Bryan O'Sullivan cf610f9 























Bryan O'Sullivan 7d6af78 


Bryan O'Sullivan 684eb7c 


Bryan O'Sullivan 537d0a8 

Bryan O'Sullivan cf610f9 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan cf610f9 


Bryan O'Sullivan 4a362d0 










Bryan O'Sullivan cf610f9 
Bryan O'Sullivan 4a362d0 













Bryan O'Sullivan 537d0a8 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 41c0769 
Bryan O'Sullivan 4a362d0 

Bryan O'Sullivan 41c0769 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 41c0769 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 41c0769 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan 57830f5 



















Bryan O'Sullivan a836f69 
Bryan O'Sullivan 4a362d0 

Bryan O'Sullivan 5bf75ce 
Bryan O'Sullivan 4a362d0 
Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan cf610f9 



Bryan O'Sullivan c9881ee 


Bryan O'Sullivan cf610f9 


Bryan O'Sullivan b08cd5e 
jsgf 7c69e69 
Bryan O'Sullivan b08cd5e 





Bryan O'Sullivan cf610f9 

Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan 85b1629 




Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan cf610f9 

Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan d49000f 
Bryan O'Sullivan 1284c66 









Bryan O'Sullivan 51bc5b8 

Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan cf610f9 

Bryan O'Sullivan b08cd5e 

Bryan O'Sullivan cf610f9 











Bryan O'Sullivan 65fcfaa 
Bryan O'Sullivan cf610f9 
Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan cf610f9 

Bryan O'Sullivan b08cd5e 
Bryan O'Sullivan cf610f9 
jsgf 4130344 
Bryan O'Sullivan cf610f9 





Bryan O'Sullivan 62ecaca 



jsgf 4130344 
Bryan O'Sullivan cf610f9 









Bryan O'Sullivan 57830f5 
Bryan O'Sullivan cf610f9 



Bryan O'Sullivan 57830f5 
Bryan O'Sullivan cf610f9 







Bryan O'Sullivan b08cd5e 


Bryan O'Sullivan 7d6af78 






  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * main.c - daemon startup and link monitoring
 *
 * Copyright 2003 PathScale, Inc.
 * Copyright 2003, 2004, 2005 Bryan O'Sullivan
 * Copyright 2003 Jeremy Fitzhardinge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.  You are
 * forbidden from redistributing or modifying it under the terms of
 * any other license, including other versions of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#define _GNU_SOURCE
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <assert.h>
#include <wait.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>

#include "netplug.h"


int use_syslog;
static char *pid_file;

static int
handle_interface(struct nlmsghdr *hdr, void *arg)
{
    if (hdr->nlmsg_type != RTM_NEWLINK && hdr->nlmsg_type != RTM_DELLINK) {
        return 0;
    }

    struct ifinfomsg *info = NLMSG_DATA(hdr);
    int len = hdr->nlmsg_len - NLMSG_LENGTH(sizeof(*info));

    if (info->ifi_flags & IFF_LOOPBACK) {
        return 0;
    }

    if (len < 0) {
        do_log(LOG_ERR, "Malformed netlink packet length");
        return -1;
    }

    struct rtattr *attrs[IFLA_MAX + 1];

    parse_rtattrs(attrs, IFLA_MAX, IFLA_RTA(info), len);

    if (attrs[IFLA_IFNAME] == NULL) {
        do_log(LOG_ERR, "No interface name");
        return -1;
    }

    char *name = RTA_DATA(attrs[IFLA_IFNAME]);

    if (!if_match(name)) {
        do_log(LOG_INFO, "%s: ignoring event", name);
        return 0;
    }

    struct if_info *i = if_info_get_interface(hdr, attrs);

    if (i == NULL)
        return 0;

    ifsm_flagchange(i, info->ifi_flags);

    if_info_update_interface(hdr, attrs);

    return 0;
}


static void
usage(char *progname, int exitcode)
{
    fprintf(stderr, "Usage: %s [-DFP] [-c config-file] [-i interface] [-p pid-file]\n",
            progname);

    fprintf(stderr, "\t-D\t\t"
            "print extra debugging messages\n");
    fprintf(stderr, "\t-F\t\t"
            "run in foreground (don't become a daemon)\n");
    fprintf(stderr, "\t-P\t\t"
            "do not autoprobe for interfaces (use with care)\n");
    fprintf(stderr, "\t-c config_file\t"
            "read interface patterns from this config file\n");
    fprintf(stderr, "\t-i interface\t"
            "only handle interfaces matching this pattern\n");
    fprintf(stderr, "\t-p pid_file\t"
            "write daemon process ID to pid_file\n");

    exit(exitcode);
}


static void
write_pid(void)
{
    FILE *fp;

    if ((fp = fopen(pid_file, "w")) == NULL) {
        do_log(LOG_ERR, "%s: %m", pid_file);
        return;
    }

    fprintf(fp, "%d\n", getpid());
    fclose(fp);
}

static void
tidy_pid(void)
{
    if (pid_file) {
        unlink(pid_file);
        pid_file = NULL;
    }
}

static void
exit_handler(int sig)
{
    tidy_pid();
    do_log(LOG_ERR, "caught signal %d - exiting", sig);
    exit(1);
}

struct child_exit
{
    pid_t       pid;
    int         status;
};

static int child_handler_pipe[2];

static void
child_handler(int sig, siginfo_t *info, void *v)
{
    struct child_exit ce;
    int ret;
    ssize_t s = 0;

    assert(sig == SIGCHLD);

    ce.pid = info->si_pid;
    ret = waitpid(info->si_pid, &ce.status, 0);
    if (ret == info->si_pid)
    {
        s = write(child_handler_pipe[1], &ce, sizeof(ce));

	if (s == -1)
	{
	    do_log(LOG_ERR, "can't write into pipe");
	    exit(1);
	}
    }
}

/* Poll the existing interface state, so we can catch any state
   changes for which we may not have neen a netlink message. */
static void
poll_interfaces(void)
{
    static int sockfd = -1;

    if (sockfd == -1) {
        sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
        if (sockfd == -1) {
            do_log(LOG_ERR, "can't create interface socket: %m");
            exit(1);
        }
        close_on_exec(sockfd);
    }

    int pollflags(struct if_info *info) {
        struct ifreq ifr;

        if (!if_match(info->name))
            return 0;

        memcpy(ifr.ifr_name, info->name, sizeof(ifr.ifr_name));
        if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0)
            do_log(LOG_ERR, "%s: can't get flags: %m", info->name);
        else {
            ifsm_flagchange(info, ifr.ifr_flags);
            ifsm_flagpoll(info);
        }

        return 0;
    }

    for_each_iface(pollflags);
}

int debug = 0;

int
main(int argc, char *argv[])
{
    int foreground = 0;
    int cfg_read = 0;
    int probe = 1;
    int c;

    while ((c = getopt(argc, argv, "DFPc:hi:p:")) != EOF) {
        switch (c) {
        case 'D':
            debug = 1;
            break;
        case 'F':
            foreground = 1;
            break;
        case 'P':
            probe = 0;
            break;
        case 'c':
            read_config(optarg);
            cfg_read = 1;
            break;
        case 'h':
            fprintf(stderr, "netplugd version %s\n", NP_VERSION);
            usage(argv[0], 0);
            break;
        case 'i':
            if (save_pattern(optarg) == -1) {
                fprintf(stderr, "Bad pattern for '-i %s'\n", optarg);
                exit(1);
            }
            break;
        case 'p':
            pid_file = optarg;
            break;
        case '?':
            usage(argv[0], 1);
        }
    }

    if (!cfg_read) {
        read_config(NP_ETC_DIR "/netplugd.conf");
    }

    if (getuid() != 0) {
        do_log(LOG_WARNING, "This daemon will not work properly unless "
               "run by root");
    }

    if (probe) {
        probe_interfaces();
    }

    struct sigaction act = {
        .sa_handler = exit_handler,
        .sa_flags = SA_ONESHOT | SA_NOMASK,
    };

    if (sigaction(SIGHUP, &act, NULL) == -1) {
        do_log(LOG_ERR, "can't catch hangup signal: %m");
        exit(1);
    }

    if (sigaction(SIGINT, &act, NULL) == -1) {
        do_log(LOG_ERR, "can't catch interrupt signal: %m");
        exit(1);
    }

    if (sigaction(SIGTERM, &act, NULL) == -1) {
        do_log(LOG_ERR, "can't catch termination signal: %m");
        exit(1);
    }

    if (!foreground) {
        use_syslog = 1;
        openlog("netplugd", LOG_PID, LOG_DAEMON);
    }

    if (pipe(child_handler_pipe) == -1) {
        do_log(LOG_ERR, "can't create pipe: %m");
        exit(1);
    }

    close_on_exec(child_handler_pipe[0]);
    close_on_exec(child_handler_pipe[1]);

    if (fcntl(child_handler_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
        do_log(LOG_ERR, "can't set pipe non-blocking: %m");
        exit(1);
    }

    struct sigaction sa;
    sa.sa_sigaction = child_handler;
    sa.sa_flags = SA_RESTART | SA_SIGINFO;
    sigfillset(&sa.sa_mask);

    if (sigaction(SIGCHLD, &sa, NULL) == -1) {
        do_log(LOG_ERR, "can't set SIGCHLD handler: %m");
        exit(1);
    }

    int fd = netlink_open();

    netlink_request_dump(fd);
    netlink_receive_dump(fd, if_info_save_interface, NULL);

    if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
        do_log(LOG_ERR, "can't set socket non-blocking: %m");
        exit(1);
    }

    if (!foreground) {
	if (daemon(0, 0) == -1) {
	    do_log(LOG_ERR, "daemon: %m");
	    exit(1);
	}

        if (pid_file) {
            atexit(tidy_pid);
            write_pid();
        }
    }

    struct pollfd fds[] = {
        { fd, POLLIN, 0 },
        { child_handler_pipe[0], POLLIN, 0 },
    };

    {
        /* Run over each of the interfaces we know and care about, and
           make sure the state machine has done the appropriate thing
           for their current state. */
        int poll_flags(struct if_info *i) {
            if (if_match(i->name))
                ifsm_flagpoll(i);
            return 0;
        }
        for_each_iface(poll_flags);
    }

    for(;;) {
        int ret;

        /* Make sure we don't miss anything interesting */
        poll_interfaces();

        ret = poll(fds, sizeof(fds)/sizeof(fds[0]), -1);

        if (ret == -1) {
            if (errno == EINTR)
                continue;
            do_log(LOG_ERR, "poll failed: %m");
            exit(1);
        }
        if (ret == 0) {         /* XXX??? */
            sleep(1);           /* don't spin */
            continue;
        }

        if (fds[0].revents & POLLIN) {
            /* interface flag state change */
            if (netlink_listen(fd, handle_interface, NULL) == 0)
                break;          /* done */
        }

        if (fds[1].revents & POLLIN) {
            /* netplug script finished */
            int ret;
            struct child_exit ce;

            do {
                ret = read(child_handler_pipe[0], &ce, sizeof(ce));

                assert(ret == 0 || ret == -1 || ret == sizeof(ce));

                if (ret == sizeof(ce))
                    ifsm_scriptdone(ce.pid, ce.status);
                else if (ret == -1 && errno != EAGAIN) {
                    do_log(LOG_ERR, "pipe read failed: %m");
                    exit(1);
                }
            } while(ret == sizeof(ce));
        }
    }

    return 0;
}


/*
 * Local variables:
 * c-file-style: "stroustrup"
 * End:
 */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.