source: rtems-libbsd/dhcpcd/dev.c @ 67cbb9d

55-freebsd-126-freebsd-12
Last change on this file since 67cbb9d was f2ed769, checked in by Sebastian Huber <sebastian.huber@…>, on 01/30/14 at 12:29:46

DHCPCD(8): Import

Import DHCPCD(8) from:

http://roy.marples.name/projects/dhcpcd/

The upstream sources can be obtained via:

fossil clone http://roy.marples.name/projects/dhcpcd

The imported version is 2014-01-29 19:46:44 [6b209507bb].

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dirent.h>
28#include <dlfcn.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <syslog.h>
33
34#include "common.h"
35#include "dev.h"
36#include "eloop.h"
37#include "dhcpcd.h"
38
39static struct dev *dev;
40static void *handle;
41static int fd = -1;
42
43static struct dev_dhcpcd dev_dhcpcd = {
44        .handle_interface = &handle_interface
45};
46
47int
48dev_initialized(const char *ifname)
49{
50
51        if (dev == NULL)
52                return 1;
53        return dev->initialized(ifname);
54}
55
56int
57dev_listening(void)
58{
59
60        if (dev == NULL)
61                return 0;
62        return dev->listening();
63}
64
65void
66dev_stop(void)
67{
68
69        if (dev) {
70                syslog(LOG_DEBUG, "dev: unloaded %s", dev->name);
71                dev->stop();
72                free(dev);
73                dev = NULL;
74        }
75        if (handle) {
76                dlclose(handle);
77                handle = NULL;
78        }
79}
80
81static int
82dev_start2(const char *name)
83{
84        char file[PATH_MAX];
85        void *h;
86        void (*fptr)(struct dev *, const struct dev_dhcpcd *);
87        int r;
88
89        snprintf(file, sizeof(file), DEVDIR "/%s", name);
90        h = dlopen(file, RTLD_LAZY);
91        if (h == NULL) {
92                syslog(LOG_ERR, "dlopen: %s", dlerror());
93                return -1;
94        }
95        fptr = (void (*)(struct dev *, const struct dev_dhcpcd *))
96            dlsym(h, "dev_init");
97        if (fptr == NULL) {
98                syslog(LOG_ERR, "dlsym: %s", dlerror());
99                dlclose(h);
100                return -1;
101        }
102        dev = calloc(1, sizeof(*dev));
103        fptr(dev, &dev_dhcpcd);
104        if (dev->start  == NULL || (r = dev->start()) == -1) {
105                free(dev);
106                dev = NULL;
107                dlclose(h);
108                return -1;
109        }
110        syslog(LOG_INFO, "dev: loaded %s", dev->name);
111        handle = h;
112        return r;
113}
114
115static int
116dev_start1(const char *plugin)
117{
118        DIR *dp;
119        struct dirent *d;
120        int r;
121
122        if (dev) {
123                syslog(LOG_ERR, "dev: already started %s", dev->name);
124                return -1;
125        }
126
127        if (plugin)
128                return dev_start2(plugin);
129
130        dp = opendir(DEVDIR);
131        if (dp == NULL) {
132                syslog(LOG_DEBUG, "dev: %s: %m", DEVDIR);
133                return 0;
134        }
135
136        r = 0;
137        while ((d = readdir(dp))) {
138                if (d->d_name[0] == '.')
139                        continue;
140
141                r = dev_start2(d->d_name);
142                if (r != -1)
143                        break;
144        }
145        closedir(dp);
146        return r;
147}
148
149static void
150dev_handle_data(__unused void *arg)
151{
152
153        if (dev->handle_device() == -1) {
154                /* XXX: an error occured. should we restart dev? */
155        }
156}
157
158int
159dev_start(const char *plugin)
160{
161
162        if (fd != -1) {
163                syslog(LOG_ERR, "%s: already started on fd %d", __func__, fd);
164                return fd;
165        }
166
167        fd = dev_start1(plugin);
168        if (fd != -1) {
169                if (eloop_event_add(fd, dev_handle_data, NULL) == -1) {
170                        syslog(LOG_ERR, "%s: eloop_event_add: %m", __func__);
171                        dev_stop();
172                        return -1;
173                }
174        }
175
176        return fd;
177}
Note: See TracBrowser for help on using the repository browser.