2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2008-09-04 00:49:28 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2012-11-13 17:39:20 +08:00
|
|
|
* Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
|
2008-09-04 00:49:28 +08:00
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2009-07-14 23:10:09 +08:00
|
|
|
#include <string.h>
|
2008-09-04 00:49:28 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "common.h"
|
2009-01-13 00:39:01 +08:00
|
|
|
#include "dhcpcd.h"
|
2008-09-04 00:49:28 +08:00
|
|
|
#include "control.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
|
|
|
|
static int fd = -1;
|
|
|
|
static char buffer[1024];
|
|
|
|
static char *argvp[255];
|
|
|
|
|
2012-11-13 17:39:20 +08:00
|
|
|
static struct sockaddr_un sun;
|
|
|
|
struct fd_list *control_fds = NULL;
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
static void
|
|
|
|
cleanup(void)
|
|
|
|
{
|
|
|
|
struct fd_list *f;
|
|
|
|
|
|
|
|
f = control_fds;
|
|
|
|
while (f) {
|
|
|
|
control_fds = f->next;
|
|
|
|
free(f);
|
|
|
|
f = control_fds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-04 03:27:47 +08:00
|
|
|
|
|
|
|
static void
|
2012-11-13 18:17:19 +08:00
|
|
|
control_remove(void *arg)
|
2008-09-04 03:27:47 +08:00
|
|
|
{
|
2012-11-13 17:39:20 +08:00
|
|
|
struct fd_list *l, *n, *last = NULL;
|
2009-01-13 00:39:01 +08:00
|
|
|
|
2012-11-13 17:39:20 +08:00
|
|
|
l = control_fds;
|
|
|
|
while (l) {
|
|
|
|
n = l->next;
|
2009-01-13 00:39:01 +08:00
|
|
|
if (l == arg) {
|
2009-01-15 23:11:13 +08:00
|
|
|
close(l->fd);
|
2012-11-13 19:25:51 +08:00
|
|
|
eloop_event_delete(l->fd);
|
2009-01-13 00:39:01 +08:00
|
|
|
if (last == NULL)
|
2012-11-13 17:39:20 +08:00
|
|
|
control_fds = l->next;
|
2009-01-13 00:39:01 +08:00
|
|
|
else
|
|
|
|
last->next = l->next;
|
|
|
|
free(l);
|
|
|
|
break;
|
2008-09-04 03:27:47 +08:00
|
|
|
}
|
2009-01-13 00:39:01 +08:00
|
|
|
last = l;
|
2012-11-13 17:39:20 +08:00
|
|
|
l = n;
|
2008-09-04 03:27:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
static void
|
2012-11-13 18:17:19 +08:00
|
|
|
control_handle_data(void *arg)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2009-01-13 00:39:01 +08:00
|
|
|
struct fd_list *l = arg;
|
2008-09-04 00:49:28 +08:00
|
|
|
ssize_t bytes;
|
2009-01-13 00:39:01 +08:00
|
|
|
int argc;
|
2008-09-04 00:49:28 +08:00
|
|
|
char *e, *p;
|
|
|
|
char **ap;
|
|
|
|
|
2009-02-06 05:06:43 +08:00
|
|
|
bytes = read(l->fd, buffer, sizeof(buffer) - 1);
|
2009-01-13 00:39:01 +08:00
|
|
|
if (bytes == -1 || bytes == 0) {
|
2012-11-13 18:17:19 +08:00
|
|
|
control_remove(l);
|
2009-01-13 00:39:01 +08:00
|
|
|
return;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2009-02-06 05:06:43 +08:00
|
|
|
buffer[bytes] = '\0';
|
2009-01-13 00:39:01 +08:00
|
|
|
p = buffer;
|
|
|
|
e = buffer + bytes;
|
|
|
|
argc = 0;
|
|
|
|
ap = argvp;
|
|
|
|
while (p < e && (size_t)argc < sizeof(argvp)) {
|
|
|
|
argc++;
|
|
|
|
*ap++ = p;
|
|
|
|
p += strlen(p) + 1;
|
|
|
|
}
|
|
|
|
handle_args(l, argc, argvp);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
2008-11-10 19:15:27 +08:00
|
|
|
/* ARGSUSED */
|
2008-09-04 00:49:28 +08:00
|
|
|
static void
|
2012-11-13 18:17:19 +08:00
|
|
|
control_handle(_unused void *arg)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_un run;
|
|
|
|
socklen_t len;
|
2009-01-13 00:39:01 +08:00
|
|
|
struct fd_list *l;
|
|
|
|
int f;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
|
|
|
len = sizeof(run);
|
2009-01-13 00:39:01 +08:00
|
|
|
if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
|
2008-09-04 00:49:28 +08:00
|
|
|
return;
|
2012-11-13 17:39:20 +08:00
|
|
|
set_cloexec(f);
|
|
|
|
l = malloc(sizeof(*l));
|
|
|
|
if (l) {
|
|
|
|
l->fd = f;
|
|
|
|
l->listener = 0;
|
|
|
|
l->next = control_fds;
|
|
|
|
control_fds = l;
|
2012-11-13 19:25:51 +08:00
|
|
|
eloop_event_add(l->fd, control_handle_data, l);
|
2012-11-13 17:39:20 +08:00
|
|
|
}
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
make_sock(void)
|
|
|
|
{
|
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
|
|
|
return -1;
|
|
|
|
memset(&sun, 0, sizeof(sun));
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sun.sun_path, CONTROLSOCKET, sizeof(sun.sun_path));
|
|
|
|
return sizeof(sun.sun_family) + strlen(sun.sun_path) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-11-13 18:17:19 +08:00
|
|
|
control_start(void)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if ((len = make_sock()) == -1)
|
|
|
|
return -1;
|
|
|
|
unlink(CONTROLSOCKET);
|
|
|
|
if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
|
2009-02-12 01:56:22 +08:00
|
|
|
chmod(CONTROLSOCKET,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 ||
|
2008-09-04 00:49:28 +08:00
|
|
|
set_cloexec(fd) == -1 ||
|
|
|
|
set_nonblock(fd) == -1 ||
|
2012-11-13 17:39:20 +08:00
|
|
|
listen(fd, sizeof(control_fds)) == -1)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-13 19:25:51 +08:00
|
|
|
eloop_event_add(fd, control_handle, NULL);
|
2008-09-04 00:49:28 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-11-13 18:17:19 +08:00
|
|
|
control_stop(void)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
int retval = 0;
|
2012-11-13 17:39:20 +08:00
|
|
|
struct fd_list *l;
|
2009-01-13 00:39:01 +08:00
|
|
|
|
2012-11-13 19:25:51 +08:00
|
|
|
eloop_event_delete(fd);
|
2009-01-15 22:50:19 +08:00
|
|
|
if (shutdown(fd, SHUT_RDWR) == -1)
|
|
|
|
retval = 1;
|
|
|
|
fd = -1;
|
|
|
|
if (unlink(CONTROLSOCKET) == -1)
|
|
|
|
retval = -1;
|
|
|
|
|
2012-11-13 17:39:20 +08:00
|
|
|
l = control_fds;
|
2009-01-13 00:39:01 +08:00
|
|
|
while (l != NULL) {
|
2012-11-13 17:39:20 +08:00
|
|
|
control_fds = l->next;
|
2012-11-13 19:25:51 +08:00
|
|
|
eloop_event_delete(l->fd);
|
2009-01-15 22:50:19 +08:00
|
|
|
shutdown(l->fd, SHUT_RDWR);
|
2009-01-13 00:39:01 +08:00
|
|
|
free(l);
|
2012-11-13 17:39:20 +08:00
|
|
|
l = control_fds;
|
2009-01-13 00:39:01 +08:00
|
|
|
}
|
2009-01-15 22:50:19 +08:00
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-11-13 18:17:19 +08:00
|
|
|
control_open(void)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if ((len = make_sock()) == -1)
|
|
|
|
return -1;
|
2012-11-13 17:39:20 +08:00
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
atexit(cleanup);
|
|
|
|
#endif
|
2008-09-04 00:49:28 +08:00
|
|
|
return connect(fd, (struct sockaddr *)&sun, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-11-13 18:17:19 +08:00
|
|
|
control_send(int argc, char * const *argv)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
|
|
|
char *p = buffer;
|
|
|
|
int i;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if (argc > 255) {
|
|
|
|
errno = ENOBUFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
len = strlen(argv[i]) + 1;
|
|
|
|
if ((p - buffer) + len > sizeof(buffer)) {
|
|
|
|
errno = ENOBUFS;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(p, argv[i], len);
|
|
|
|
p += len;
|
|
|
|
}
|
|
|
|
return write(fd, buffer, p - buffer);
|
|
|
|
}
|