mirror of
https://github.com/rsmarples/dhcpcd.git
synced 2024-11-24 02:24:35 +08:00
Move control globals into a context.
This commit is contained in:
parent
cff708ad3b
commit
9c8b417d8a
68
control.c
68
control.c
@ -41,11 +41,6 @@
|
||||
#include "control.h"
|
||||
#include "eloop.h"
|
||||
|
||||
static int fd = -1;
|
||||
static char buffer[1024];
|
||||
static char *argvp[255];
|
||||
|
||||
static struct sockaddr_un sun;
|
||||
struct fd_list *control_fds = NULL;
|
||||
|
||||
static void
|
||||
@ -75,10 +70,9 @@ static void
|
||||
control_handle_data(void *arg)
|
||||
{
|
||||
struct fd_list *l = arg;
|
||||
char buffer[1024], *e, *p, *argvp[255], **ap;
|
||||
ssize_t bytes;
|
||||
int argc;
|
||||
char *e, *p;
|
||||
char **ap;
|
||||
|
||||
bytes = read(l->fd, buffer, sizeof(buffer) - 1);
|
||||
if (bytes == -1 || bytes == 0) {
|
||||
@ -100,15 +94,17 @@ control_handle_data(void *arg)
|
||||
|
||||
/* ARGSUSED */
|
||||
static void
|
||||
control_handle(__unused void *arg)
|
||||
control_handle(void *arg)
|
||||
{
|
||||
struct control_ctx *ctx;
|
||||
struct sockaddr_un run;
|
||||
socklen_t len;
|
||||
struct fd_list *l;
|
||||
int f;
|
||||
|
||||
ctx = arg;
|
||||
len = sizeof(run);
|
||||
if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
|
||||
if ((f = accept(ctx->fd, (struct sockaddr *)&run, &len)) == -1)
|
||||
return;
|
||||
set_cloexec(f);
|
||||
l = malloc(sizeof(*l));
|
||||
@ -122,48 +118,50 @@ control_handle(__unused void *arg)
|
||||
}
|
||||
|
||||
static int
|
||||
make_sock(void)
|
||||
make_sock(struct control_ctx *ctx, struct sockaddr_un *sun)
|
||||
{
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
||||
|
||||
if ((ctx->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;
|
||||
memset(sun, 0, sizeof(*sun));
|
||||
sun->sun_family = AF_UNIX;
|
||||
strlcpy(sun->sun_path, CONTROLSOCKET, sizeof(sun->sun_path));
|
||||
return sizeof(sun->sun_family) + sizeof(sun->sun_path) + 1;
|
||||
}
|
||||
|
||||
int
|
||||
control_start(void)
|
||||
control_start(struct control_ctx *ctx)
|
||||
{
|
||||
struct sockaddr_un sun;
|
||||
int len;
|
||||
|
||||
if ((len = make_sock()) == -1)
|
||||
if ((len = make_sock(ctx, &sun)) == -1)
|
||||
return -1;
|
||||
unlink(CONTROLSOCKET);
|
||||
if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
|
||||
if (bind(ctx->fd, (struct sockaddr *)&sun, len) == -1 ||
|
||||
chmod(CONTROLSOCKET,
|
||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) == -1 ||
|
||||
set_cloexec(fd) == -1 ||
|
||||
set_nonblock(fd) == -1 ||
|
||||
listen(fd, sizeof(control_fds)) == -1)
|
||||
set_cloexec(ctx->fd) == -1 ||
|
||||
set_nonblock(ctx->fd) == -1 ||
|
||||
listen(ctx->fd, sizeof(control_fds)) == -1)
|
||||
{
|
||||
close(fd);
|
||||
close(ctx->fd);
|
||||
return -1;
|
||||
}
|
||||
eloop_event_add(fd, control_handle, NULL);
|
||||
return fd;
|
||||
eloop_event_add(ctx->fd, control_handle, ctx);
|
||||
return ctx->fd;
|
||||
}
|
||||
|
||||
int
|
||||
control_stop(void)
|
||||
control_stop(struct control_ctx *ctx)
|
||||
{
|
||||
int retval = 0;
|
||||
struct fd_list *l;
|
||||
|
||||
eloop_event_delete(fd);
|
||||
if (shutdown(fd, SHUT_RDWR) == -1)
|
||||
eloop_event_delete(ctx->fd);
|
||||
if (shutdown(ctx->fd, SHUT_RDWR) == -1)
|
||||
retval = 1;
|
||||
fd = -1;
|
||||
ctx->fd = -1;
|
||||
if (unlink(CONTROLSOCKET) == -1)
|
||||
retval = -1;
|
||||
|
||||
@ -180,19 +178,20 @@ control_stop(void)
|
||||
}
|
||||
|
||||
int
|
||||
control_open(void)
|
||||
control_open(struct control_ctx *ctx)
|
||||
{
|
||||
struct sockaddr_un sun;
|
||||
int len;
|
||||
|
||||
if ((len = make_sock()) == -1)
|
||||
if ((len = make_sock(ctx, &sun)) == -1)
|
||||
return -1;
|
||||
return connect(fd, (struct sockaddr *)&sun, len);
|
||||
return connect(ctx->fd, (struct sockaddr *)&sun, len);
|
||||
}
|
||||
|
||||
int
|
||||
control_send(int argc, char * const *argv)
|
||||
control_send(struct control_ctx *ctx, int argc, char * const *argv)
|
||||
{
|
||||
char *p = buffer;
|
||||
char buffer[1024], *p;
|
||||
int i;
|
||||
size_t len;
|
||||
|
||||
@ -200,6 +199,7 @@ control_send(int argc, char * const *argv)
|
||||
errno = ENOBUFS;
|
||||
return -1;
|
||||
}
|
||||
p = buffer;
|
||||
for (i = 0; i < argc; i++) {
|
||||
len = strlen(argv[i]) + 1;
|
||||
if ((p - buffer) + len > sizeof(buffer)) {
|
||||
@ -209,5 +209,5 @@ control_send(int argc, char * const *argv)
|
||||
memcpy(p, argv[i], len);
|
||||
p += len;
|
||||
}
|
||||
return write(fd, buffer, p - buffer);
|
||||
return write(ctx->fd, buffer, p - buffer);
|
||||
}
|
||||
|
16
control.h
16
control.h
@ -1,6 +1,6 @@
|
||||
/*
|
||||
* dhcpcd - DHCP client daemon
|
||||
* Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
|
||||
* Copyright (c) 2006-2014 Roy Marples <roy@marples.name>
|
||||
* All rights reserved
|
||||
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -28,8 +28,6 @@
|
||||
#ifndef CONTROL_H
|
||||
#define CONTROL_H
|
||||
|
||||
#include "dhcpcd.h"
|
||||
|
||||
struct fd_list {
|
||||
int fd;
|
||||
int listener;
|
||||
@ -37,9 +35,13 @@ struct fd_list {
|
||||
};
|
||||
extern struct fd_list *control_fds;
|
||||
|
||||
int control_start(void);
|
||||
int control_stop(void);
|
||||
int control_open(void);
|
||||
int control_send(int, char * const *);
|
||||
struct control_ctx {
|
||||
int fd;
|
||||
};
|
||||
|
||||
int control_start(struct control_ctx *);
|
||||
int control_stop(struct control_ctx *);
|
||||
int control_open(struct control_ctx *);
|
||||
int control_send(struct control_ctx *, int, char * const *);
|
||||
|
||||
#endif
|
||||
|
9
dhcpcd.c
9
dhcpcd.c
@ -1062,6 +1062,7 @@ main(int argc, char **argv)
|
||||
struct timespec ts;
|
||||
struct utsname utn;
|
||||
const char *platform;
|
||||
struct control_ctx control_ctx;
|
||||
|
||||
pidfile = NULL;
|
||||
closefrom(3);
|
||||
@ -1212,10 +1213,10 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) {
|
||||
if ((i = control_open()) != -1) {
|
||||
if ((i = control_open(&control_ctx)) != -1) {
|
||||
syslog(LOG_INFO,
|
||||
"sending commands to master dhcpcd process");
|
||||
len = control_send(argc, argv);
|
||||
len = control_send(&control_ctx, argc, argv);
|
||||
close(i);
|
||||
if (len > 0) {
|
||||
syslog(LOG_DEBUG, "send OK");
|
||||
@ -1308,7 +1309,7 @@ main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (options & DHCPCD_MASTER) {
|
||||
if (control_start() == -1)
|
||||
if (control_start(&control_ctx) == -1)
|
||||
syslog(LOG_ERR, "control_start: %m");
|
||||
}
|
||||
|
||||
@ -1458,7 +1459,7 @@ exit1:
|
||||
}
|
||||
if (pidfd > -1) {
|
||||
if (options & DHCPCD_MASTER) {
|
||||
if (control_stop() == -1)
|
||||
if (control_stop(&control_ctx) == -1)
|
||||
syslog(LOG_ERR, "control_stop: %m");
|
||||
}
|
||||
close(pidfd);
|
||||
|
Loading…
Reference in New Issue
Block a user