summaryrefslogtreecommitdiffstats
path: root/src/waproamd.c
blob: 98cec2c6a61ecdb12bbde8c1c30af10933ae1ddc (plain)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* $Id$ */

/*
 * This file is part of waproamd.
 *
 * waproamd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * waproamd 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with waproamd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#define _GNU_SOURCE

#include <assert.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <getopt.h>

#include <libdaemon/dpid.h>
#include <libdaemon/dlog.h>
#include <libdaemon/dfork.h>
#include <libdaemon/dsignal.h>

#include "iwapi.h"
#include "interface.h"
#include "exec.h"
#include "nlapi.h"
#include "ifmonitor.h"
#include "assocwatch.h"
#include "waproamd.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

char *interface_name = NULL;
int interface_index = -1;

int disabled = 0,
    associated = 0,
    do_status_check = 0;

struct ap_info associated_ap;

struct ap_info current_ap;
int current_ap_valid = 0;

int use_assocwatch = 1,
    use_ifmonitor = 0,
    daemonize = 1,
    wait_on_fork = 0,
#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE        
    wait_on_kill = 0,
#endif
    use_syslog = 1;

int poll_interval = 5,
    scan_interval = 10;

char log_ident[32], pid_ident[32];

int issue_scan(struct interface *i) {
    static int count = 0;
    
    daemon_log(LOG_ERR, "Scanning... %i", count++);

    /* Due to driver issues, the return codes of these function calls are no longer evaluated */
    iw_set_mode(i, IW_MODE_INFRA);
    iw_set_ap(i, &null_ap);
    iw_set_essid(i, NULL);

    if (iw_scan(i) < 0)
        return -1;

    return 0;
}

int get_script_path(char *path, int l, struct ap_info *ai) {
    assert(path && l);

    if (!ai) {
        snprintf(path, l, "%s/default", SCRIPTDIR);
        return 0;
    }
    
    snprintf(path, l, "%s/%02x:%02x:%02x:%02x:%02x:%02x",
             SCRIPTDIR,
             ai->ap.addr[0], ai->ap.addr[1], ai->ap.addr[2],
             ai->ap.addr[3], ai->ap.addr[4], ai->ap.addr[5]);

    if (!access(path, X_OK))
        return 0;
    
    snprintf(path, l, "%s/%02X:%02X:%02X:%02X:%02X:%02X",
             SCRIPTDIR,
             ai->ap.addr[0], ai->ap.addr[1], ai->ap.addr[2],
             ai->ap.addr[3], ai->ap.addr[4], ai->ap.addr[5]);
    
    if (!access(path, X_OK))
        return 0;

    if (ai->essid[0]) {
        snprintf(path, l, "%s/essid:%s",
                 SCRIPTDIR,
                 escape_essid(ai->essid));
        
        if (!access(path, X_OK))
            return  0;
    }

    return -1;
}

struct ap_info scan_ap;
int scan_ap_has_script;
int scan_ap_valid;

static int scan_result_cb(struct ap_info*ai) {
    int f;
    char path[PATH_MAX];
    
    assert(ai);

    /* Found something suitable earlier */
    if (scan_ap_valid && scan_ap_has_script)
        return 0;

    f = get_script_path(path, sizeof(path), ai);
    if (scan_ap_valid && !f)
        return 0;

    memcpy(&scan_ap, ai, sizeof(struct ap_info));
    scan_ap_valid = 1;
    scan_ap_has_script = !f;
    return 0;
}

int read_scan(struct interface *i, struct ap_info **ap) {
    int r;
    scan_ap_valid = 0;
    scan_ap_has_script = 0;

    if ((r = iw_scan_result(i, scan_result_cb)) < 0)
        return -1;

    if (r == 1)
        return 1;

    *ap = scan_ap_valid ? &scan_ap : NULL;

    return 0;
}
        
int run_script(struct ap_info *ai, const char *arg) {
    char t[32];
    char path[PATH_MAX];

    if (get_script_path(path, sizeof(path), ai) < 0)
        get_script_path(path, sizeof(path), NULL);

    setenv("IFACE", interface_name, 1);
    if (!ai->adhoc) {
        snprint_hw_addr(t, sizeof(t), &ai->ap);
        setenv("AP", t, 1);
    } else
        setenv("AP", "n.a.", 1);
    
    setenv("ESSID", ai->essid, 1);
    setenv("ESSID_ESCAPED", escape_essid(ai->essid), 1);
    setenv("ADHOC", ai->adhoc ? "YES" : "NO", 1);
    
    return log_exec(SCRIPTDIR, path, arg);
};


int set_current_ap(struct ap_info *a) {
    
    if ((a && current_ap_valid && !iw_ap_info_equal(a, &current_ap)) ||
        (!!a != !!current_ap_valid)) {

        if (current_ap_valid) {
            if (run_script(&current_ap, "stop") < 0)
                return -1;
        }

        if ((current_ap_valid = !!a))
            memcpy(&current_ap, a, sizeof(struct ap_info));
        
        if (current_ap_valid) {

            if (current_ap.adhoc) 
                daemon_log(LOG_INFO, "Selected new ad hoc network with ESSID '%s'", escape_essid(a->essid));
            else {
                char t[20];
                snprint_hw_addr(t, sizeof(t), &current_ap.ap);
                daemon_log(LOG_INFO, "Selected new AP %s with ESSID '%s'", t, escape_essid(a->essid));
            }
            
            if (run_script(&current_ap, "start") < 0)
                return -1;
        }
    }
    
    return 0;
}

int ifmonitor_cb(int b, int index, unsigned short type, const char *name) {
    if (!name)
        return 0;
    
    if (!strcmp(name, interface_name)) {
        interface_index = index;
        disabled = !b;
    }
    return 0;
}

int assocwatch_cb(int index, struct hw_addr *a) {
    char name[IFNAMSIZ+1];

    if (get_ifname(index, name, sizeof(name)) < 0)
        return -1;

    if (strcmp(name, interface_name))
        return 0;

    do_status_check = 1;
    
    return 0;
}

int go(struct interface *i) {
    time_t next_scan;
    int scanning = 0;
    int send_retval = 1;
    int r = -1, sigfd;
    int paused = 0;
    fd_set fds;

    daemon_log(LOG_INFO, "waproamd "VERSION" initializing%s%s.", use_ifmonitor ? ", using NETLINK device monitoring" : "", use_assocwatch ? ", using wireless event notifications" : "");
    
    if (daemon_pid_file_create() < 0) {
        daemon_log(LOG_ERR, "Could not create PID file %s.", daemon_pid_file_proc());
        goto finish;
    }

    if (daemon_signal_init(SIGINT, SIGTERM, SIGQUIT, SIGHUP, SIGCHLD, SIGUSR1, SIGUSR2, -1) < 0) {
        daemon_log(LOG_ERR, "Could not register signal handler: %s", strerror(errno));
        goto finish;
    }

    if (nlapi_open(RTMGRP_LINK) < 0)
        goto finish;

    if (use_ifmonitor) {
        int b;
        
        if ((b = is_iface_available(interface_name)) < 0)
            goto finish;

        disabled = !b;
    } else
        disabled = 0;

    memset(&current_ap, 0, sizeof(current_ap));
    current_ap_valid = 0;

    if ((associated = iw_assoc(i, &associated_ap)) < 0) {
        if (!use_ifmonitor)
            goto finish;

        associated = 0;
    }
    
    daemon_log(LOG_INFO, "Currently %sassociated, interface %s.", associated ? "" : "not ", disabled ? "disabled" : "enabled");

    if (use_assocwatch)
        if (assocwatch_init(assocwatch_cb) < 0)
            goto finish;

    if (use_ifmonitor)
        if (ifmonitor_init(ifmonitor_cb) < 0)
            goto finish;
    
    daemon_log(LOG_INFO, "Initialization complete.");

    set_current_ap(associated ? &associated_ap : NULL);
    next_scan = associated || disabled ? (time_t) -1 : 0;

    if (daemonize && wait_on_fork) {
        daemon_retval_send(0);
        send_retval = 0;
    }

    FD_ZERO(&fds);
    FD_SET(sigfd = daemon_signal_fd(), &fds);
    FD_SET(nlapi_fd, &fds);
    
    for (;;) {
        fd_set qfds;
        struct timeval tv, *ptv = NULL;
        time_t now = time(NULL);
        int a, d;

        if (next_scan != (time_t) -1 && next_scan <= now) {
            if (issue_scan(i) < 0) {
                if (!use_ifmonitor)
                    goto finish;
            } else 
                scanning = 1;
            
            next_scan = (time_t) -1;
        }

        if (do_status_check) {
            tv.tv_sec = 0;
            tv.tv_usec = 0;
            ptv = &tv;
        } if (scanning) {
            tv.tv_sec = 0;
            tv.tv_usec = 100000;
            ptv = &tv;
        } else if (!use_assocwatch) {
            tv.tv_sec = poll_interval;
            tv.tv_usec = 0;
            ptv = &tv;
        }
        
        if (next_scan != (time_t) -1) {
            struct timeval tv2; 
            now = time(NULL);
            tv2.tv_sec = next_scan > now ? next_scan - now : 0;
            tv2.tv_usec = 0;
            
            if (!ptv || tv2.tv_sec < tv.tv_sec) {
                tv.tv_sec = tv2.tv_sec;
                tv.tv_usec = tv2.tv_usec;
                ptv = &tv;
            }
        }

        qfds = fds;
        
        if (select(FD_SETSIZE, &qfds, NULL, NULL, ptv) < 0) {
            if (errno == EINTR)
                continue;

            daemon_log(LOG_ERR, "select() failed: %s", strerror(errno));
            goto finish;
        }

        a = associated;
        d = disabled;
        
        if (FD_ISSET(nlapi_fd, &qfds))
            if (nlapi_work(0) < 0)
                goto finish;

        if (do_status_check || (!use_assocwatch && !disabled && !paused)) {

            if ((associated = iw_assoc(i, &associated_ap)) < 0) {
                if (!use_ifmonitor)
                    goto finish;

                associated = 0;
            }

        }

        do_status_check = 0;

        if (paused) {

            /* If paused ignore new data */
            associated = a;
            disabled = d;
            
        } else {
            
            /* Changed: enabled -> disabled */
            if (!d && disabled) {
                daemon_log(LOG_INFO, "Interface disabled");
                
                if (associated)
                    if (set_current_ap(NULL) < 0)
                        goto finish;
                
                associated = scanning = 0;
            }
            
            /* Changed: disabled -> enabled */
            if (d && !disabled) {
                daemon_log(LOG_INFO, "Interface enabled");
                associated = scanning = 0;
            }

            if (!disabled) {
                /* Changed: associated -> not associated */
                if (a && !associated) {
                    daemon_log(LOG_INFO, "No longer associated.");
                    scanning = 0;
                }
                
                /* Changed: not associated -> associated */
                if (!a && associated) {
                    if (set_current_ap(&associated_ap) < 0)
                        goto finish;
                    
                    daemon_log(LOG_INFO, "Associated.");
                    scanning = 0;
                }                
        
                if (scanning) {
                    int r;
                    struct ap_info *ai = NULL;
                    
                    if ((r = read_scan(i, &ai)) < 0) {
                        if (!use_ifmonitor)
                            goto finish;
                        
                        scanning = 0;
                        
                    } else if (!r) {
                    
                        scanning = 0;
                    
                        if (!associated && ai) {

                            if (iw_tune(i, ai) < 0)
                                goto finish;
                            
                            if (set_current_ap(ai) < 0)
                                goto finish;
                        }
                    }
                }
            }
        }

        if (FD_ISSET(sigfd, &qfds)) {
            int sig;

            if ((sig = daemon_signal_next()) < 0) {
                daemon_log(LOG_ERR, "daemon_signal_next(): %s", strerror(errno));
                goto finish;
            }


            switch (sig) {

                case SIGINT:
                case SIGTERM:
                case SIGQUIT:
                    r = 0;
                    goto finish;

                case SIGHUP:
                    if (!disabled && !associated && !paused) {
                        next_scan = 0;
                        daemon_log(LOG_INFO, "SIGHUP: Immediate scan requested");
                    } else
                        daemon_log(LOG_INFO, "SIGHUP: Immediate scan request ignored");
                    
                    break;

                case SIGCHLD:
                        break;

                case SIGUSR1:
                    daemon_log(LOG_INFO, "SIGUSR1: Daemon suspended (#%i)", ++paused);
                    break;

                case SIGUSR2:
                    if (paused > 0) {
                        daemon_log(LOG_INFO, "SIGUSR2: Daemon resumed (#%i)", paused--);
                        if (!paused)
                            do_status_check = 1;
                    }

                    break;
                    
                default:
                    daemon_log(LOG_INFO, "Ignoring unknown signal %s", strsignal(sig));
                    break;

            }
        }
        
        if (disabled || associated || paused) {
            scanning = 0;
            next_scan = (time_t) -1;
        } else if (next_scan == (time_t) -1 && !scanning)
            next_scan = time(NULL) + scan_interval;
    }

    r = 0;
    
finish:

    set_current_ap(NULL);
    
    if (send_retval && daemonize && wait_on_fork)
        daemon_retval_send(1);

    nlapi_close();
    
    daemon_pid_file_remove();
    daemon_signal_done();
    
    daemon_log(LOG_INFO, "Exiting.");
    
    return r;
}

void usage(char *p) {
    if (strrchr(p, '/'))
        p = strchr(p, '/')+1;

    printf("%s -- Wireless Access Point Roaming Daemon for WLAN IEEE 802.11\n\n"
           "Usage: %s [options]\n\n"
           "Options:\n"
           "   -n --no-daemon            Do not daemonize (for debugging) (%s)\n"
           "   -s --no-syslog            Do not use syslog, use stderr instead (for debugging) (%s)\n"
           "   -i --iface=IFACE          Specify network interface (%s)\n"
           "   -w --wait-on-fork         Wait until daemon fork finished (%s)\n"
#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE        
           "   -W --wait-on-kill         When run with -k, wait until the daemon died (%s)\n"
#endif		   
           "   -M --monitor              Use interface monitoring (%s)\n"
           "   -e --no-event             Don't use wireless event API (%s)\n"
           "   -t --scan-interval=SECS   Specify scan interval (%i)\n"
           "   -p --poll-interval=SECS   Specify association poll interval, unless using -e (%i)\n"
           "   -h --help                 Show this help\n"
           "   -k --kill                 Kill a running daemon\n"
	   "   -c --check-running        Check if a daemon is currently running\n"
           "   -v --version              Show version\n"
           "   -S --suspend              Suspend running daemon\n"
           "   -R --resume               Resume running daemon\n"
           "   -r --issue-scan           Tell running daemon to issue a new scan immediately\n",
           p, p,
           !daemonize ? "on" : "off",
           !use_syslog ? "on" : "off",
           interface_name,
           wait_on_fork ? "on" : "off",
#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE        
		   wait_on_kill ? "on" : "off",
#endif
           use_ifmonitor ? "on" : "off",
           use_assocwatch ? "off" : "on",
           scan_interval,
           poll_interval);
}

void parse_args(int argc, char *argv[]) {
    static struct option long_options[] = {
        {"no-daemon",            no_argument, 0, 'n'},
        {"no-syslog",            no_argument, 0, 's'},
        {"iface",                required_argument, 0, 'i'},
        {"wait-on-fork",         no_argument, 0, 'w'},
#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE
        {"wait-on-kill",         no_argument, 0, 'W'},
#endif
        {"monitor",              no_argument, 0, 'M'},
        {"no-event",             no_argument, 0, 'e'},
        {"scan-interval",        required_argument, 0, 't'},
        {"poll-interval",        required_argument, 0, 'p'},
        {"help",                 no_argument, 0, 'h'},
        {"kill",                 no_argument, 0, 'k'},
        {"check-running",        no_argument, 0, 'c'},
        {"version",              no_argument, 0, 'v'},
        {"suspend",              no_argument, 0, 'S'},
        {"resume",               no_argument, 0, 'R'},
        {"issue-scan",           no_argument, 0, 'r'},
        {0, 0, 0, 0}
    };
    int option_index = 0;
    int _help = 0, _kill = 0, _check = 0, _version = 0, _suspend = 0, _resume = 0, _issuescan = 0;
    
    for (;;) {
        int c;
        
        if ((c = getopt_long(argc, argv, "nsi:whkcvMet:p:SRrWr", long_options, &option_index)) < 0)
            break;

        switch (c) {
            case 'n' :
                daemonize = !daemonize;
                break;

            case 's' :
                use_syslog = !use_syslog;
                break;

            case 'i' :
                if (interface_name)
                    free(interface_name);
                interface_name = strdup(optarg);
                break;

            case 'w':
                wait_on_fork = !wait_on_fork;
                break;

#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE
            case 'W':
                wait_on_kill = !wait_on_kill;
                break;
#endif
            case 'S':
                _suspend = 1;
                break;
                
            case 'R':
                _resume = 1;
                break;
                
            case 'r':
                _issuescan = 1;
                break;
                
            case 'M':
                use_ifmonitor = !use_ifmonitor;
                break;

            case 'e':
                use_assocwatch = !use_assocwatch;
                break;

            case 't':
                if ((scan_interval = atoi(optarg)) < 0) {
                    daemon_log(LOG_ERR, "Scan interval must be a positive, nonzero integer.");
                    exit(1);
                }

            case 'p':
                if ((poll_interval = atoi(optarg)) < 0) {
                    daemon_log(LOG_ERR, "Poll interval must be a positive, nonzero integer.");
                    exit(1);
                }
                
            case 'h':
                _help = 1;
                break;

            case 'k':
                _kill = 1;
                break;

            case 'c':
                _check = 1;
                break;

            case 'v':
                _version = 1;
                break;
                
            default:
                daemon_log(LOG_ERR, "Unknown parameter.");
                exit(1);
        }
    }

    if (!interface_name)
        interface_name = strdup("wlan0");

    snprintf(pid_ident, sizeof(pid_ident), "waproamd.%s", interface_name);
    daemon_pid_file_ident =  pid_ident;
    snprintf(log_ident, sizeof(log_ident), "waproamd(%s)", interface_name);
    daemon_log_ident = log_ident;


    if (_help) {
        usage(argv[0]);
        exit(0);
    }

    if (_kill || _resume || _suspend || _issuescan) {
        int rv;

#ifdef DAEMON_PID_FILE_KILL_WAIT_AVAILABLE
        if (_kill && wait_on_kill)
            rv = daemon_pid_file_kill_wait(SIGINT, 5);
        else
#endif
            rv = daemon_pid_file_kill(_kill ? SIGINT : (_resume ? SIGUSR2 : (_issuescan ? SIGHUP : SIGUSR1)));
        
        if (rv < 0) {
            daemon_log(LOG_ERR, "Failed to kill daemon. (%s)", strerror(errno));
            exit(6);
        }

        exit(0);
    }

    if (_version) {
        printf("waproamd "VERSION"\n");
        exit(0);
    }

    if (_check) {
        pid_t pid = daemon_pid_file_is_running();

        if (pid == (pid_t) -1 || pid == 0) {
            printf("waproamd not running.\n");
            exit(255);
        } else {
            printf("waproamd process for device %s running as pid %u.\n", interface_name, pid);
            exit(0);
        }
    }
    
    if (!use_syslog) 
        daemon_log_use = DAEMON_LOG_STDERR;

}

int main(int argc, char *argv[]) {
    struct interface *i = NULL;
    int r = 1;
    pid_t pid;

    daemon_pid_file_ident = daemon_log_ident = daemon_ident_from_argv0(argv[0]);

    parse_args(argc, argv);

    if (geteuid() != 0) {
        daemon_log(LOG_ERR, "Sorry, you need to be root to run this binary.");
        goto finish;
    }

    if ((pid = daemon_pid_file_is_running()) >= 0) {
        daemon_log(LOG_ERR, "Daemon already running on PID file %u", pid);
        goto finish;
        
    }
    
    if (daemonize) {
        pid_t pid;

        if (wait_on_fork)
            if (daemon_retval_init() < 0) {
                daemon_log(LOG_ERR, "Sorry, could not create pipe: %s", strerror(errno));
                goto finish;
            }
        
        if ((pid = daemon_fork()) < 0)
            goto finish;

        if (pid) {
            int c = 0;
            
            // Parent process

            if (wait_on_fork)
                if ((c = daemon_retval_wait(60)) < 0) {
                    daemon_log(LOG_WARNING, "Killing background process.");
                    kill(pid, SIGTERM);
                }

            r = c;
            goto finish;
        }
    }

    if (!(i = interface_open(interface_name)) < 0)
        goto finish;

    if (go(i) < 0)
        goto finish;

    r = 0;

finish:

    if (i)
        interface_close(i);

    if (interface_name)
        free(interface_name);

    return r;
}