2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2008-09-04 00:49:28 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2014-02-04 22:55:04 +08:00
|
|
|
* Copyright (c) 2006-2014 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>
|
2014-02-08 01:32:08 +08:00
|
|
|
#include <fcntl.h>
|
2008-09-04 00:49:28 +08:00
|
|
|
#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"
|
|
|
|
|
2014-02-06 02:32:13 +08:00
|
|
|
#ifndef SUN_LEN
|
|
|
|
#define SUN_LEN(su) \
|
|
|
|
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2014-02-22 00:10:55 +08:00
|
|
|
struct fd_list *l = arg, *lp, *last;
|
2014-02-05 20:01:09 +08:00
|
|
|
char buffer[1024], *e, *p, *argvp[255], **ap;
|
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
|
|
|
|
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) {
|
2014-02-22 00:10:55 +08:00
|
|
|
/* Control was closed or there was an error.
|
|
|
|
* Remove it from our list. */
|
|
|
|
last = NULL;
|
|
|
|
for (lp = l->ctx->control_fds; lp; lp = lp->next) {
|
|
|
|
if (lp == l) {
|
|
|
|
eloop_event_delete(lp->ctx->eloop, lp->fd);
|
|
|
|
close(lp->fd);
|
|
|
|
if (last == NULL)
|
|
|
|
lp->ctx->control_fds = lp->next;
|
|
|
|
else
|
|
|
|
last->next = lp->next;
|
|
|
|
free(lp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = lp;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_handleargs(l->ctx, l, argc, argvp);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-05 20:01:09 +08:00
|
|
|
control_handle(void *arg)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
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;
|
2014-02-08 01:32:08 +08:00
|
|
|
int fd, flags;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-02-05 20:01:09 +08:00
|
|
|
ctx = arg;
|
2008-09-04 00:49:28 +08:00
|
|
|
len = sizeof(run);
|
2014-02-12 08:39:46 +08:00
|
|
|
if ((fd = accept(ctx->control_fd, (struct sockaddr *)&run, &len)) == -1)
|
2008-09-04 00:49:28 +08:00
|
|
|
return;
|
2014-02-08 01:32:08 +08:00
|
|
|
if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
|
|
|
|
fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-13 17:39:20 +08:00
|
|
|
l = malloc(sizeof(*l));
|
|
|
|
if (l) {
|
2014-02-12 08:39:46 +08:00
|
|
|
l->ctx = ctx;
|
2014-02-08 01:32:08 +08:00
|
|
|
l->fd = fd;
|
2012-11-13 17:39:20 +08:00
|
|
|
l->listener = 0;
|
2014-02-12 08:39:46 +08:00
|
|
|
l->next = ctx->control_fds;
|
|
|
|
ctx->control_fds = l;
|
|
|
|
eloop_event_add(ctx->eloop, l->fd, control_handle_data, l);
|
2014-02-08 01:32:08 +08:00
|
|
|
} else
|
|
|
|
close(fd);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
static socklen_t
|
2014-02-22 00:10:55 +08:00
|
|
|
make_sock(struct dhcpcd_ctx *ctx, struct sockaddr_un *sun, const char *ifname)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-02-05 20:01:09 +08:00
|
|
|
|
2014-02-13 20:58:58 +08:00
|
|
|
#ifdef SOCK_CLOEXEC
|
2014-02-12 08:39:46 +08:00
|
|
|
if ((ctx->control_fd = socket(AF_UNIX,
|
2014-02-08 01:32:08 +08:00
|
|
|
SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1)
|
2014-03-28 06:14:52 +08:00
|
|
|
return 0;
|
2014-02-13 20:58:58 +08:00
|
|
|
#else
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if ((ctx->control_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2014-03-28 06:14:52 +08:00
|
|
|
return 0;
|
2014-02-13 20:58:58 +08:00
|
|
|
if ((flags = fcntl(ctx->control_fd, F_GETFD, 0)) == -1 ||
|
|
|
|
fcntl(ctx->control_fd, F_SETFD, flags | FD_CLOEXEC) == -1)
|
|
|
|
{
|
|
|
|
close(ctx->control_fd);
|
|
|
|
ctx->control_fd = -1;
|
2014-03-28 06:14:52 +08:00
|
|
|
return 0;
|
2014-02-13 20:58:58 +08:00
|
|
|
}
|
|
|
|
if ((flags = fcntl(ctx->control_fd, F_GETFL, 0)) == -1 ||
|
|
|
|
fcntl(ctx->control_fd, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
|
|
{
|
|
|
|
close(ctx->control_fd);
|
|
|
|
ctx->control_fd = -1;
|
2014-03-28 06:14:52 +08:00
|
|
|
return 0;
|
2014-02-13 20:58:58 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-02-05 20:01:09 +08:00
|
|
|
memset(sun, 0, sizeof(*sun));
|
|
|
|
sun->sun_family = AF_UNIX;
|
2014-02-22 00:10:55 +08:00
|
|
|
snprintf(sun->sun_path, sizeof(sun->sun_path), CONTROLSOCKET,
|
|
|
|
ifname ? "-" : "", ifname ? ifname : "");
|
|
|
|
strlcpy(ctx->control_sock, sun->sun_path, sizeof(ctx->control_sock));
|
2014-03-28 07:00:32 +08:00
|
|
|
return (socklen_t)SUN_LEN(sun);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-02-22 00:10:55 +08:00
|
|
|
control_start(struct dhcpcd_ctx *ctx, const char *ifname)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-02-05 20:01:09 +08:00
|
|
|
struct sockaddr_un sun;
|
2014-03-28 06:14:52 +08:00
|
|
|
socklen_t len;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
if ((len = make_sock(ctx, &sun, ifname)) == 0)
|
2008-09-04 00:49:28 +08:00
|
|
|
return -1;
|
2014-02-22 00:10:55 +08:00
|
|
|
unlink(ctx->control_sock);
|
2014-02-12 08:39:46 +08:00
|
|
|
if (bind(ctx->control_fd, (struct sockaddr *)&sun, len) == -1 ||
|
2014-02-22 00:10:55 +08:00
|
|
|
chmod(ctx->control_sock,
|
2009-02-12 01:56:22 +08:00
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 ||
|
2014-02-12 08:39:46 +08:00
|
|
|
listen(ctx->control_fd, sizeof(ctx->control_fds)) == -1)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
close(ctx->control_fd);
|
|
|
|
ctx->control_fd = -1;
|
2014-02-22 00:10:55 +08:00
|
|
|
unlink(ctx->control_sock);
|
2008-09-04 00:49:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_event_add(ctx->eloop, ctx->control_fd, control_handle, ctx);
|
|
|
|
return ctx->control_fd;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-02-12 08:39:46 +08:00
|
|
|
control_stop(struct dhcpcd_ctx *ctx)
|
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
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
if (ctx->control_fd == -1)
|
|
|
|
return 0;
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_event_delete(ctx->eloop, ctx->control_fd);
|
|
|
|
if (shutdown(ctx->control_fd, SHUT_RDWR) == -1)
|
2009-01-15 22:50:19 +08:00
|
|
|
retval = 1;
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->control_fd = -1;
|
2014-02-22 00:10:55 +08:00
|
|
|
if (unlink(ctx->control_sock) == -1)
|
2009-01-15 22:50:19 +08:00
|
|
|
retval = -1;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
l = ctx->control_fds;
|
2009-01-13 00:39:01 +08:00
|
|
|
while (l != NULL) {
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->control_fds = l->next;
|
|
|
|
eloop_event_delete(ctx->eloop, 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);
|
2014-02-12 08:39:46 +08:00
|
|
|
l = ctx->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
|
2014-02-22 00:10:55 +08:00
|
|
|
control_open(struct dhcpcd_ctx *ctx, const char *ifname)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-02-05 20:01:09 +08:00
|
|
|
struct sockaddr_un sun;
|
2014-03-28 06:14:52 +08:00
|
|
|
socklen_t len;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
if ((len = make_sock(ctx, &sun, ifname)) == 0)
|
2008-09-04 00:49:28 +08:00
|
|
|
return -1;
|
2014-02-13 04:51:57 +08:00
|
|
|
if (connect(ctx->control_fd, (struct sockaddr *)&sun, len) == -1) {
|
|
|
|
close(ctx->control_fd);
|
|
|
|
ctx->control_fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
ssize_t
|
2014-02-12 08:39:46 +08:00
|
|
|
control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2014-03-28 06:14:52 +08:00
|
|
|
char buffer[1024];
|
2008-09-04 00:49:28 +08:00
|
|
|
int i;
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t len, l;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
|
|
|
if (argc > 255) {
|
|
|
|
errno = ENOBUFS;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-28 06:14:52 +08:00
|
|
|
len = 0;
|
2008-09-04 00:49:28 +08:00
|
|
|
for (i = 0; i < argc; i++) {
|
2014-03-28 06:14:52 +08:00
|
|
|
l = strlen(argv[i]) + 1;
|
|
|
|
if (len + l > sizeof(buffer)) {
|
2008-09-04 00:49:28 +08:00
|
|
|
errno = ENOBUFS;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-28 06:14:52 +08:00
|
|
|
memcpy(buffer + len, argv[i], l);
|
|
|
|
len += l;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2014-03-28 06:14:52 +08:00
|
|
|
return write(ctx->control_fd, buffer, len);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
control_close(struct dhcpcd_ctx *ctx)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ctx->control_fd != -1) {
|
|
|
|
close(ctx->control_fd);
|
|
|
|
ctx->control_fd = -1;
|
|
|
|
}
|
|
|
|
}
|