2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2015-01-03 04:03:28 +08:00
|
|
|
* Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
|
2007-11-15 19:35:53 +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.
|
2006-11-28 04:23:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2009-01-13 00:39:01 +08:00
|
|
|
#include <sys/uio.h>
|
2008-04-13 07:00:23 +08:00
|
|
|
#include <sys/wait.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <netinet/in.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2008-05-13 22:23:01 +08:00
|
|
|
#include <ctype.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <errno.h>
|
2008-02-03 05:56:12 +08:00
|
|
|
#include <signal.h>
|
2012-11-11 00:38:52 +08:00
|
|
|
/* We can't include spawn.h here because it may not exist.
|
|
|
|
* config.h will pull it in, or our compat one. */
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <stdlib.h>
|
2009-07-14 21:59:30 +08:00
|
|
|
#include <string.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-05-10 17:39:26 +08:00
|
|
|
#include "config.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "common.h"
|
2009-03-24 06:02:37 +08:00
|
|
|
#include "dhcp.h"
|
2012-10-12 18:31:51 +08:00
|
|
|
#include "dhcp6.h"
|
2014-04-25 18:42:37 +08:00
|
|
|
#include "if.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "if-options.h"
|
2015-06-13 03:21:32 +08:00
|
|
|
#include "ipv4ll.h"
|
2013-08-20 18:29:43 +08:00
|
|
|
#include "ipv6nd.h"
|
2013-02-02 22:05:55 +08:00
|
|
|
#include "script.h"
|
2008-02-03 01:28:22 +08:00
|
|
|
|
2014-04-23 17:43:30 +08:00
|
|
|
#ifdef HAVE_SPAWN_H
|
|
|
|
#include <spawn.h>
|
|
|
|
#else
|
|
|
|
#include "compat/posix_spawn.h"
|
|
|
|
#endif
|
|
|
|
|
2014-09-09 16:22:10 +08:00
|
|
|
/* Allow the OS to define another script env var name */
|
2014-09-12 16:34:47 +08:00
|
|
|
#ifndef RC_SVCNAME
|
|
|
|
#define RC_SVCNAME "RC_SVCNAME"
|
2014-09-09 16:22:10 +08:00
|
|
|
#endif
|
|
|
|
|
2008-05-13 19:21:19 +08:00
|
|
|
#define DEFAULT_PATH "PATH=/usr/bin:/usr/sbin:/bin:/sbin"
|
|
|
|
|
2014-01-14 22:40:36 +08:00
|
|
|
static const char * const if_params[] = {
|
2012-10-12 18:31:51 +08:00
|
|
|
"interface",
|
|
|
|
"reason",
|
|
|
|
"pid",
|
2014-02-27 18:52:11 +08:00
|
|
|
"ifcarrier",
|
2012-10-12 18:31:51 +08:00
|
|
|
"ifmetric",
|
|
|
|
"ifwireless",
|
|
|
|
"ifflags",
|
|
|
|
"ssid",
|
|
|
|
"profile",
|
|
|
|
"interface_order",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
if_printoptions(void)
|
|
|
|
{
|
2014-01-14 22:40:36 +08:00
|
|
|
const char * const *p;
|
2012-10-12 18:31:51 +08:00
|
|
|
|
|
|
|
for (p = if_params; *p; p++)
|
|
|
|
printf(" - %s\n", *p);
|
|
|
|
}
|
|
|
|
|
2008-08-11 17:28:53 +08:00
|
|
|
static int
|
2015-04-30 18:25:16 +08:00
|
|
|
exec_script(const struct dhcpcd_ctx *ctx, char *const *argv, char *const *env)
|
2008-08-11 17:28:53 +08:00
|
|
|
{
|
|
|
|
pid_t pid;
|
2012-11-11 00:38:52 +08:00
|
|
|
posix_spawnattr_t attr;
|
2015-05-13 07:47:25 +08:00
|
|
|
int r;
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2015-05-13 07:47:25 +08:00
|
|
|
size_t i;
|
2012-11-11 08:04:02 +08:00
|
|
|
short flags;
|
2012-11-11 00:38:52 +08:00
|
|
|
sigset_t defsigs;
|
2015-04-30 18:25:16 +08:00
|
|
|
#else
|
|
|
|
UNUSED(ctx);
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
2012-11-11 00:38:52 +08:00
|
|
|
|
|
|
|
/* posix_spawn is a safe way of executing another image
|
|
|
|
* and changing signals back to how they should be. */
|
|
|
|
if (posix_spawnattr_init(&attr) == -1)
|
|
|
|
return -1;
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2012-11-11 08:04:02 +08:00
|
|
|
flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
|
|
|
|
posix_spawnattr_setflags(&attr, flags);
|
2012-11-11 00:38:52 +08:00
|
|
|
sigemptyset(&defsigs);
|
2015-05-13 07:47:25 +08:00
|
|
|
for (i = 0; i < dhcpcd_signals_len; i++)
|
2015-05-09 18:09:34 +08:00
|
|
|
sigaddset(&defsigs, dhcpcd_signals[i]);
|
2012-11-11 00:38:52 +08:00
|
|
|
posix_spawnattr_setsigdefault(&attr, &defsigs);
|
2014-02-22 00:10:55 +08:00
|
|
|
posix_spawnattr_setsigmask(&attr, &ctx->sigset);
|
|
|
|
#endif
|
2012-11-11 00:38:52 +08:00
|
|
|
errno = 0;
|
2015-05-13 07:47:25 +08:00
|
|
|
r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env);
|
|
|
|
if (r) {
|
|
|
|
errno = r;
|
2012-11-11 00:38:52 +08:00
|
|
|
return -1;
|
2012-11-11 08:25:58 +08:00
|
|
|
}
|
2008-08-11 17:28:53 +08:00
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET
|
2009-01-01 22:23:36 +08:00
|
|
|
static char *
|
2015-03-18 07:46:38 +08:00
|
|
|
make_var(struct dhcpcd_ctx *ctx, const char *prefix, const char *var)
|
2009-01-01 22:23:36 +08:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
len = strlen(prefix) + strlen(var) + 2;
|
2013-02-16 21:21:35 +08:00
|
|
|
v = malloc(len);
|
|
|
|
if (v == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "%s: %m", __func__);
|
2013-02-16 21:21:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-01-01 22:23:36 +08:00
|
|
|
snprintf(v, len, "%s_%s", prefix, var);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-16 21:21:35 +08:00
|
|
|
static int
|
2015-03-18 07:46:38 +08:00
|
|
|
append_config(struct dhcpcd_ctx *ctx, char ***env, size_t *len,
|
2009-02-12 01:56:22 +08:00
|
|
|
const char *prefix, const char *const *config)
|
2009-01-01 22:23:36 +08:00
|
|
|
{
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t i, j, e1;
|
2013-02-16 21:21:35 +08:00
|
|
|
char **ne, *eq, **nep, *p;
|
2014-02-28 05:11:43 +08:00
|
|
|
int ret;
|
2009-01-01 22:23:36 +08:00
|
|
|
|
|
|
|
if (config == NULL)
|
2013-02-16 21:21:35 +08:00
|
|
|
return 0;
|
2009-01-01 22:23:36 +08:00
|
|
|
|
|
|
|
ne = *env;
|
2014-02-28 05:11:43 +08:00
|
|
|
ret = 0;
|
2009-01-01 22:23:36 +08:00
|
|
|
for (i = 0; config[i] != NULL; i++) {
|
|
|
|
eq = strchr(config[i], '=');
|
2014-03-28 06:14:52 +08:00
|
|
|
e1 = (size_t)(eq - config[i] + 1);
|
2009-01-01 22:23:36 +08:00
|
|
|
for (j = 0; j < *len; j++) {
|
2009-02-12 01:56:22 +08:00
|
|
|
if (strncmp(ne[j] + strlen(prefix) + 1,
|
|
|
|
config[i], e1) == 0)
|
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
p = make_var(ctx, prefix, config[i]);
|
2014-02-28 05:11:43 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-01 22:23:36 +08:00
|
|
|
free(ne[j]);
|
2014-02-28 05:11:43 +08:00
|
|
|
ne[j] = p;
|
2009-01-01 22:23:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j == *len) {
|
|
|
|
j++;
|
2015-03-18 07:46:38 +08:00
|
|
|
p = make_var(ctx, prefix, config[i]);
|
2014-02-28 05:11:43 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-16 06:20:18 +08:00
|
|
|
nep = realloc(ne, sizeof(char *) * (j + 1));
|
|
|
|
if (nep == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "%s: %m", __func__);
|
2014-02-28 05:11:43 +08:00
|
|
|
free(p);
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2013-02-16 06:20:18 +08:00
|
|
|
}
|
|
|
|
ne = nep;
|
2013-02-16 21:21:35 +08:00
|
|
|
ne[j - 1] = p;
|
2009-01-01 22:23:36 +08:00
|
|
|
*len = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*env = ne;
|
2014-02-28 05:11:43 +08:00
|
|
|
return ret;
|
2009-01-01 22:23:36 +08:00
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2009-01-01 22:23:36 +08:00
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
static ssize_t
|
2009-01-13 00:39:01 +08:00
|
|
|
arraytostr(const char *const *argv, char **s)
|
|
|
|
{
|
|
|
|
const char *const *ap;
|
|
|
|
char *p;
|
|
|
|
size_t len, l;
|
|
|
|
|
2014-02-27 19:57:45 +08:00
|
|
|
if (*argv == NULL)
|
|
|
|
return 0;
|
2009-01-13 00:39:01 +08:00
|
|
|
len = 0;
|
|
|
|
ap = argv;
|
|
|
|
while (*ap)
|
|
|
|
len += strlen(*ap++) + 1;
|
2013-02-16 21:21:35 +08:00
|
|
|
*s = p = malloc(len);
|
|
|
|
if (p == NULL)
|
|
|
|
return -1;
|
2009-01-13 00:39:01 +08:00
|
|
|
ap = argv;
|
|
|
|
while (*ap) {
|
|
|
|
l = strlen(*ap) + 1;
|
|
|
|
memcpy(p, *ap, l);
|
|
|
|
p += l;
|
|
|
|
ap++;
|
|
|
|
}
|
2014-03-28 06:14:52 +08:00
|
|
|
return (ssize_t)len;
|
2009-01-13 00:39:01 +08:00
|
|
|
}
|
|
|
|
|
2009-01-14 01:04:28 +08:00
|
|
|
static ssize_t
|
2013-02-03 18:35:59 +08:00
|
|
|
make_env(const struct interface *ifp, const char *reason, char ***argv)
|
2007-01-17 01:54:07 +08:00
|
|
|
{
|
2013-02-16 06:20:18 +08:00
|
|
|
char **env, **nenv, *p;
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t e, elen, l;
|
2014-12-22 03:00:19 +08:00
|
|
|
#if defined(INET) || defined(INET6)
|
2014-03-28 06:14:52 +08:00
|
|
|
ssize_t n;
|
2014-12-22 03:00:19 +08:00
|
|
|
#endif
|
2013-02-03 18:35:59 +08:00
|
|
|
const struct if_options *ifo = ifp->options;
|
|
|
|
const struct interface *ifp2;
|
2015-06-10 03:47:53 +08:00
|
|
|
int af;
|
2014-02-28 19:02:32 +08:00
|
|
|
#ifdef INET
|
2015-06-13 03:21:32 +08:00
|
|
|
int dhcp, ipv4ll;
|
2013-02-04 06:55:45 +08:00
|
|
|
const struct dhcp_state *state;
|
2015-06-29 23:31:33 +08:00
|
|
|
const struct ipv4ll_state *istate;
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET6
|
2012-10-12 18:31:51 +08:00
|
|
|
const struct dhcp6_state *d6_state;
|
2014-02-28 19:02:32 +08:00
|
|
|
int dhcp6, ra;
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2011-12-15 10:35:47 +08:00
|
|
|
|
2014-02-28 19:02:32 +08:00
|
|
|
#ifdef INET
|
2015-06-13 03:21:32 +08:00
|
|
|
dhcp = ipv4ll = 0;
|
2013-02-04 06:55:45 +08:00
|
|
|
state = D_STATE(ifp);
|
2015-06-29 23:31:33 +08:00
|
|
|
istate = IPV4LL_CSTATE(ifp);
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET6
|
2014-02-28 19:02:32 +08:00
|
|
|
dhcp6 = ra = 0;
|
2013-02-04 06:55:45 +08:00
|
|
|
d6_state = D6_CSTATE(ifp);
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2012-06-28 17:11:24 +08:00
|
|
|
if (strcmp(reason, "TEST") == 0) {
|
2014-02-28 19:02:32 +08:00
|
|
|
if (1 == 2) {}
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET6
|
2014-02-28 19:02:32 +08:00
|
|
|
else if (d6_state && d6_state->new)
|
2012-10-12 18:31:51 +08:00
|
|
|
dhcp6 = 1;
|
2014-05-19 08:49:14 +08:00
|
|
|
else if (ipv6nd_hasra(ifp))
|
2012-06-28 17:11:24 +08:00
|
|
|
ra = 1;
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2014-02-28 19:02:32 +08:00
|
|
|
#ifdef INET
|
2015-07-02 21:04:15 +08:00
|
|
|
else if (state->added)
|
2012-06-28 17:11:24 +08:00
|
|
|
dhcp = 1;
|
2015-07-02 21:04:15 +08:00
|
|
|
else
|
|
|
|
ipv4ll = 1;
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#ifdef INET6
|
|
|
|
else if (reason[strlen(reason) - 1] == '6')
|
2012-10-12 18:31:51 +08:00
|
|
|
dhcp6 = 1;
|
|
|
|
else if (strcmp(reason, "ROUTERADVERT") == 0)
|
2011-12-15 10:35:47 +08:00
|
|
|
ra = 1;
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
2014-05-07 02:15:52 +08:00
|
|
|
else if (strcmp(reason, "PREINIT") == 0 ||
|
|
|
|
strcmp(reason, "CARRIER") == 0 ||
|
|
|
|
strcmp(reason, "NOCARRIER") == 0 ||
|
|
|
|
strcmp(reason, "UNKNOWN") == 0 ||
|
|
|
|
strcmp(reason, "DEPARTED") == 0 ||
|
|
|
|
strcmp(reason, "STOPPED") == 0)
|
2014-12-22 03:00:19 +08:00
|
|
|
{
|
|
|
|
/* This space left intentionally blank */
|
|
|
|
}
|
2014-02-28 19:02:32 +08:00
|
|
|
#ifdef INET
|
2015-06-13 04:24:12 +08:00
|
|
|
else if (strcmp(reason, "IPV4LL") == 0)
|
|
|
|
ipv4ll = 1;
|
2011-12-15 10:35:47 +08:00
|
|
|
else
|
|
|
|
dhcp = 1;
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2011-03-11 20:25:01 +08:00
|
|
|
/* When dumping the lease, we only want to report interface and
|
|
|
|
reason - the other interface variables are meaningless */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ifp->ctx->options & DHCPCD_DUMPLEASE)
|
2011-03-11 20:25:01 +08:00
|
|
|
elen = 2;
|
|
|
|
else
|
2015-06-10 03:47:53 +08:00
|
|
|
elen = 11;
|
2011-03-11 20:25:01 +08:00
|
|
|
|
2014-03-28 06:14:52 +08:00
|
|
|
#define EMALLOC(i, l) if ((env[(i)] = malloc((l))) == NULL) goto eexit;
|
2014-11-26 23:34:14 +08:00
|
|
|
/* Make our env + space for profile, wireless and debug */
|
2015-06-10 03:47:53 +08:00
|
|
|
env = calloc(1, sizeof(char *) * (elen + 4 + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (env == NULL)
|
|
|
|
goto eexit;
|
2013-02-03 18:35:59 +08:00
|
|
|
e = strlen("interface") + strlen(ifp->name) + 2;
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(0, e);
|
2013-02-03 18:35:59 +08:00
|
|
|
snprintf(env[0], e, "interface=%s", ifp->name);
|
2012-02-03 00:22:40 +08:00
|
|
|
e = strlen("reason") + strlen(reason) + 2;
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(1, e);
|
2012-02-03 00:22:40 +08:00
|
|
|
snprintf(env[1], e, "reason=%s", reason);
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ifp->ctx->options & DHCPCD_DUMPLEASE)
|
2011-03-11 20:25:01 +08:00
|
|
|
goto dumplease;
|
2013-04-02 15:01:11 +08:00
|
|
|
e = 20;
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(2, e);
|
2009-01-14 01:04:28 +08:00
|
|
|
snprintf(env[2], e, "pid=%d", getpid());
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(3, e);
|
2014-02-27 18:52:11 +08:00
|
|
|
snprintf(env[3], e, "ifcarrier=%s",
|
|
|
|
ifp->carrier == LINK_UNKNOWN ? "unknown" :
|
|
|
|
ifp->carrier == LINK_UP ? "up" : "down");
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(4, e);
|
2014-02-27 18:52:11 +08:00
|
|
|
snprintf(env[4], e, "ifmetric=%d", ifp->metric);
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(5, e);
|
2014-02-27 18:52:11 +08:00
|
|
|
snprintf(env[5], e, "ifwireless=%d", ifp->wireless);
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(6, e);
|
2014-02-27 18:52:11 +08:00
|
|
|
snprintf(env[6], e, "ifflags=%u", ifp->flags);
|
|
|
|
EMALLOC(7, e);
|
2015-08-06 21:07:15 +08:00
|
|
|
snprintf(env[7], e, "ifmtu=%d", if_getmtu(ifp));
|
2009-04-20 18:02:17 +08:00
|
|
|
l = e = strlen("interface_order=");
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
|
2015-06-06 06:06:05 +08:00
|
|
|
e += strlen(ifp2->name) + 1;
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2014-02-27 18:52:11 +08:00
|
|
|
EMALLOC(8, e);
|
|
|
|
p = env[8];
|
2009-04-20 18:02:17 +08:00
|
|
|
strlcpy(p, "interface_order=", e);
|
2008-09-10 01:07:48 +08:00
|
|
|
e -= l;
|
|
|
|
p += l;
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
|
2015-06-06 06:06:05 +08:00
|
|
|
l = strlcpy(p, ifp2->name, e);
|
|
|
|
p += l;
|
|
|
|
e -= l;
|
|
|
|
*p++ = ' ';
|
|
|
|
e--;
|
2008-09-10 01:07:48 +08:00
|
|
|
}
|
|
|
|
*--p = '\0';
|
2014-11-28 23:18:09 +08:00
|
|
|
if (strcmp(reason, "STOPPED") == 0) {
|
|
|
|
env[9] = strdup("if_up=false");
|
|
|
|
if (ifo->options & DHCPCD_RELEASE)
|
2014-11-28 23:20:01 +08:00
|
|
|
env[10] = strdup("if_down=true");
|
2014-11-28 23:18:09 +08:00
|
|
|
else
|
2014-11-28 23:20:01 +08:00
|
|
|
env[10] = strdup("if_down=false");
|
2014-11-28 23:18:09 +08:00
|
|
|
} else if (strcmp(reason, "TEST") == 0 ||
|
2014-10-22 02:16:28 +08:00
|
|
|
strcmp(reason, "PREINIT") == 0 ||
|
|
|
|
strcmp(reason, "CARRIER") == 0 ||
|
|
|
|
strcmp(reason, "UNKNOWN") == 0)
|
|
|
|
{
|
2014-02-27 18:52:11 +08:00
|
|
|
env[9] = strdup("if_up=false");
|
|
|
|
env[10] = strdup("if_down=false");
|
2014-02-28 19:02:32 +08:00
|
|
|
} else if (1 == 2 /* appease ifdefs */
|
|
|
|
#ifdef INET
|
|
|
|
|| (dhcp && state && state->new)
|
2015-06-29 21:09:57 +08:00
|
|
|
|| (ipv4ll && IPV4LL_STATE_RUNNING(ifp))
|
2014-02-28 19:02:32 +08:00
|
|
|
#endif
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET6
|
|
|
|
|| (dhcp6 && d6_state && d6_state->new)
|
2014-05-19 08:49:14 +08:00
|
|
|
|| (ra && ipv6nd_hasra(ifp))
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
|
|
|
)
|
2012-10-12 18:31:51 +08:00
|
|
|
{
|
2014-02-27 18:52:11 +08:00
|
|
|
env[9] = strdup("if_up=true");
|
|
|
|
env[10] = strdup("if_down=false");
|
2012-02-03 00:22:40 +08:00
|
|
|
} else {
|
2014-02-27 18:52:11 +08:00
|
|
|
env[9] = strdup("if_up=false");
|
|
|
|
env[10] = strdup("if_down=true");
|
2012-02-03 00:22:40 +08:00
|
|
|
}
|
2014-02-27 18:52:11 +08:00
|
|
|
if (env[9] == NULL || env[10] == NULL)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2015-06-10 03:47:53 +08:00
|
|
|
if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
|
|
|
|
e = 20;
|
|
|
|
EMALLOC(elen, e);
|
|
|
|
snprintf(env[elen++], e, "if_afwaiting=%d", af);
|
|
|
|
}
|
|
|
|
if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
|
2015-06-10 04:00:39 +08:00
|
|
|
TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
|
|
|
|
if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (af != AF_MAX) {
|
2015-06-10 03:47:53 +08:00
|
|
|
e = 20;
|
|
|
|
EMALLOC(elen, e);
|
|
|
|
snprintf(env[elen++], e, "af_waiting=%d", af);
|
|
|
|
}
|
2014-11-26 22:54:14 +08:00
|
|
|
if (ifo->options & DHCPCD_DEBUG) {
|
|
|
|
e = strlen("syslog_debug=true") + 1;
|
|
|
|
EMALLOC(elen, e);
|
|
|
|
snprintf(env[elen++], e, "syslog_debug=true");
|
|
|
|
}
|
2013-02-04 06:55:45 +08:00
|
|
|
if (*ifp->profile) {
|
2014-11-26 23:34:14 +08:00
|
|
|
e = strlen("profile=") + strlen(ifp->profile) + 1;
|
2013-02-16 21:21:35 +08:00
|
|
|
EMALLOC(elen, e);
|
2013-02-04 06:55:45 +08:00
|
|
|
snprintf(env[elen++], e, "profile=%s", ifp->profile);
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->wireless) {
|
2014-10-02 02:32:03 +08:00
|
|
|
static const char *pfx = "ifssid=";
|
2014-09-29 17:03:12 +08:00
|
|
|
size_t pfx_len;
|
|
|
|
ssize_t psl;
|
2014-09-10 09:46:58 +08:00
|
|
|
|
2014-09-29 17:03:12 +08:00
|
|
|
pfx_len = strlen(pfx);
|
2014-10-02 02:32:03 +08:00
|
|
|
psl = print_string(NULL, 0, ESCSTRING,
|
2014-09-29 17:03:12 +08:00
|
|
|
(const uint8_t *)ifp->ssid, ifp->ssid_len);
|
|
|
|
if (psl != -1) {
|
|
|
|
EMALLOC(elen, pfx_len + (size_t)psl + 1);
|
|
|
|
memcpy(env[elen], pfx, pfx_len);
|
2014-10-02 21:21:29 +08:00
|
|
|
print_string(env[elen] + pfx_len, (size_t)psl + 1,
|
2014-10-02 02:32:03 +08:00
|
|
|
ESCSTRING,
|
2014-09-10 09:46:58 +08:00
|
|
|
(const uint8_t *)ifp->ssid, ifp->ssid_len);
|
2014-09-29 17:03:12 +08:00
|
|
|
elen++;
|
2009-01-23 07:52:20 +08:00
|
|
|
}
|
2009-01-23 06:52:41 +08:00
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET
|
2013-02-04 06:55:45 +08:00
|
|
|
if (dhcp && state && state->old) {
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp_env(NULL, NULL, state->old, ifp);
|
|
|
|
if (n == -1)
|
|
|
|
goto eexit;
|
|
|
|
if (n > 0) {
|
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp_env(env + elen, "old", state->old, ifp);
|
|
|
|
if (n == -1)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen += (size_t)n;
|
2008-05-13 19:21:19 +08:00
|
|
|
}
|
2015-03-18 07:46:38 +08:00
|
|
|
if (append_config(ifp->ctx, &env, &elen, "old",
|
2013-02-16 21:21:35 +08:00
|
|
|
(const char *const *)ifo->config) == -1)
|
|
|
|
goto eexit;
|
2008-05-13 19:21:19 +08:00
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
2013-02-04 06:55:45 +08:00
|
|
|
if (dhcp6 && d6_state && d6_state->old) {
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp6_env(NULL, NULL, ifp,
|
2012-10-12 18:31:51 +08:00
|
|
|
d6_state->old, d6_state->old_len);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (n > 0) {
|
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp6_env(env + elen, "old", ifp,
|
2012-10-12 18:31:51 +08:00
|
|
|
d6_state->old, d6_state->old_len);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (n == -1)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen += (size_t)n;
|
2012-10-12 18:31:51 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2011-03-11 20:25:01 +08:00
|
|
|
|
|
|
|
dumplease:
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET
|
2015-06-13 03:21:32 +08:00
|
|
|
if (ipv4ll) {
|
|
|
|
n = ipv4ll_env(NULL, NULL, ifp);
|
|
|
|
if (n > 0) {
|
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2015-06-29 23:31:33 +08:00
|
|
|
if ((n = ipv4ll_env(env + elen,
|
|
|
|
istate->down ? "old" : "new", ifp)) == -1)
|
2015-06-13 03:21:32 +08:00
|
|
|
goto eexit;
|
|
|
|
elen += (size_t)n;
|
|
|
|
}
|
|
|
|
}
|
2013-02-04 06:55:45 +08:00
|
|
|
if (dhcp && state && state->new) {
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp_env(NULL, NULL, state->new, ifp);
|
2014-05-06 04:18:14 +08:00
|
|
|
if (n > 0) {
|
2014-03-28 06:14:52 +08:00
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp_env(env + elen, "new",
|
2013-02-04 06:55:45 +08:00
|
|
|
state->new, ifp);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (n == -1)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen += (size_t)n;
|
2008-05-13 19:21:19 +08:00
|
|
|
}
|
2015-03-18 07:46:38 +08:00
|
|
|
if (append_config(ifp->ctx, &env, &elen, "new",
|
2013-02-16 21:21:35 +08:00
|
|
|
(const char *const *)ifo->config) == -1)
|
|
|
|
goto eexit;
|
2008-05-13 19:21:19 +08:00
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
2014-10-04 01:54:06 +08:00
|
|
|
if (dhcp6 && D6_STATE_RUNNING(ifp)) {
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp6_env(NULL, NULL, ifp,
|
2012-10-12 18:31:51 +08:00
|
|
|
d6_state->new, d6_state->new_len);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (n > 0) {
|
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
n = dhcp6_env(env + elen, "new", ifp,
|
2012-10-12 18:31:51 +08:00
|
|
|
d6_state->new, d6_state->new_len);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (n == -1)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen += (size_t)n;
|
2012-10-12 18:31:51 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 10:35:47 +08:00
|
|
|
if (ra) {
|
2014-03-28 06:14:52 +08:00
|
|
|
n = ipv6nd_env(NULL, NULL, ifp);
|
|
|
|
if (n > 0) {
|
|
|
|
nenv = realloc(env, sizeof(char *) *
|
|
|
|
(elen + (size_t)n + 1));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
n = ipv6nd_env(env + elen, NULL, ifp);
|
|
|
|
if (n == -1)
|
2013-02-16 21:21:35 +08:00
|
|
|
goto eexit;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen += (size_t)n;
|
2011-12-15 10:35:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2009-01-01 22:23:36 +08:00
|
|
|
|
2008-05-21 00:05:15 +08:00
|
|
|
/* Add our base environment */
|
2008-09-02 21:28:11 +08:00
|
|
|
if (ifo->environ) {
|
2008-05-21 00:05:15 +08:00
|
|
|
e = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
while (ifo->environ[e++])
|
2008-05-21 00:05:15 +08:00
|
|
|
;
|
2013-02-16 06:20:18 +08:00
|
|
|
nenv = realloc(env, sizeof(char *) * (elen + e + 1));
|
|
|
|
if (nenv == NULL)
|
|
|
|
goto eexit;
|
|
|
|
env = nenv;
|
2008-05-21 00:05:15 +08:00
|
|
|
e = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
while (ifo->environ[e]) {
|
2013-02-16 06:46:47 +08:00
|
|
|
env[elen + e] = strdup(ifo->environ[e]);
|
|
|
|
if (env[elen + e] == NULL)
|
|
|
|
goto eexit;
|
2008-05-21 00:05:15 +08:00
|
|
|
e++;
|
|
|
|
}
|
|
|
|
elen += e;
|
|
|
|
}
|
2013-11-19 17:43:54 +08:00
|
|
|
env[elen] = NULL;
|
2008-05-13 19:21:19 +08:00
|
|
|
|
2009-01-14 01:04:28 +08:00
|
|
|
*argv = env;
|
2014-03-28 06:14:52 +08:00
|
|
|
return (ssize_t)elen;
|
2013-02-16 06:20:18 +08:00
|
|
|
|
|
|
|
eexit:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
|
2014-02-27 19:57:45 +08:00
|
|
|
if (env) {
|
|
|
|
nenv = env;
|
|
|
|
while (*nenv)
|
|
|
|
free(*nenv++);
|
|
|
|
free(env);
|
|
|
|
}
|
2013-02-16 06:20:18 +08:00
|
|
|
return -1;
|
2009-01-14 01:04:28 +08:00
|
|
|
}
|
|
|
|
|
2014-09-05 20:28:05 +08:00
|
|
|
static int
|
|
|
|
send_interface1(struct fd_list *fd, const struct interface *iface,
|
|
|
|
const char *reason)
|
2009-01-14 01:04:28 +08:00
|
|
|
{
|
|
|
|
char **env, **ep, *s;
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t elen;
|
2014-09-05 20:28:05 +08:00
|
|
|
int retval;
|
2009-01-14 01:04:28 +08:00
|
|
|
|
2013-02-16 21:21:35 +08:00
|
|
|
if (make_env(iface, reason, &env) == -1)
|
|
|
|
return -1;
|
2014-09-15 03:12:57 +08:00
|
|
|
s = NULL;
|
2014-03-28 06:14:52 +08:00
|
|
|
elen = (size_t)arraytostr((const char *const *)env, &s);
|
2014-09-15 03:12:57 +08:00
|
|
|
if ((ssize_t)elen == -1) {
|
|
|
|
free(s);
|
2013-02-16 21:21:35 +08:00
|
|
|
return -1;
|
2014-09-15 03:12:57 +08:00
|
|
|
}
|
2014-09-05 20:28:05 +08:00
|
|
|
retval = control_queue(fd, s, elen, 1);
|
2009-01-14 01:04:28 +08:00
|
|
|
ep = env;
|
|
|
|
while (*ep)
|
|
|
|
free(*ep++);
|
|
|
|
free(env);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-09-05 20:28:05 +08:00
|
|
|
send_interface(struct fd_list *fd, const struct interface *ifp)
|
2012-02-03 00:22:40 +08:00
|
|
|
{
|
2013-02-17 01:53:13 +08:00
|
|
|
const char *reason;
|
2012-02-03 00:22:40 +08:00
|
|
|
int retval = 0;
|
2013-02-04 21:30:35 +08:00
|
|
|
#ifdef INET
|
2014-09-03 07:41:52 +08:00
|
|
|
const struct dhcp_state *d;
|
2014-05-06 05:21:31 +08:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
2014-09-03 07:41:52 +08:00
|
|
|
const struct dhcp6_state *d6;
|
2014-05-06 05:21:31 +08:00
|
|
|
#endif
|
2013-02-04 06:55:45 +08:00
|
|
|
|
2014-05-06 22:42:24 +08:00
|
|
|
switch (ifp->carrier) {
|
|
|
|
case LINK_UP:
|
|
|
|
reason = "CARRIER";
|
|
|
|
break;
|
|
|
|
case LINK_DOWN:
|
|
|
|
reason = "NOCARRIER";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
reason = "UNKNOWN";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (send_interface1(fd, ifp, reason) == -1)
|
2014-09-05 20:28:05 +08:00
|
|
|
retval = -1;
|
2014-05-06 05:21:31 +08:00
|
|
|
#ifdef INET
|
2014-09-03 07:41:52 +08:00
|
|
|
if (D_STATE_RUNNING(ifp)) {
|
|
|
|
d = D_CSTATE(ifp);
|
2014-05-06 05:21:31 +08:00
|
|
|
if (send_interface1(fd, ifp, d->reason) == -1)
|
2013-02-17 01:53:13 +08:00
|
|
|
retval = -1;
|
2014-09-03 07:41:52 +08:00
|
|
|
}
|
2015-06-29 23:31:33 +08:00
|
|
|
if (IPV4LL_STATE_RUNNING(ifp)) {
|
|
|
|
if (send_interface1(fd, ifp, "IPV4LL") == -1)
|
|
|
|
retval = -1;
|
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INET6
|
2014-09-05 03:55:37 +08:00
|
|
|
if (RS_STATE_RUNNING(ifp)) {
|
2014-05-06 05:21:31 +08:00
|
|
|
if (send_interface1(fd, ifp, "ROUTERADVERT") == -1)
|
2012-02-03 00:22:40 +08:00
|
|
|
retval = -1;
|
|
|
|
}
|
2014-09-03 07:41:52 +08:00
|
|
|
if (D6_STATE_RUNNING(ifp)) {
|
|
|
|
d6 = D6_CSTATE(ifp);
|
2014-05-06 05:21:31 +08:00
|
|
|
if (send_interface1(fd, ifp, d6->reason) == -1)
|
2012-10-13 03:10:04 +08:00
|
|
|
retval = -1;
|
|
|
|
}
|
2013-02-04 21:30:35 +08:00
|
|
|
#endif
|
2013-02-17 01:53:13 +08:00
|
|
|
|
2012-02-03 00:22:40 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-02-03 18:35:59 +08:00
|
|
|
script_runreason(const struct interface *ifp, const char *reason)
|
2009-01-14 01:04:28 +08:00
|
|
|
{
|
2014-02-12 19:16:22 +08:00
|
|
|
char *argv[2];
|
2009-01-14 01:04:28 +08:00
|
|
|
char **env = NULL, **ep;
|
2014-09-09 16:22:10 +08:00
|
|
|
char *svcname, *path, *bigenv;
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t e, elen = 0;
|
2009-01-14 01:04:28 +08:00
|
|
|
pid_t pid;
|
|
|
|
int status = 0;
|
2014-09-05 20:28:05 +08:00
|
|
|
struct fd_list *fd;
|
2009-01-14 01:04:28 +08:00
|
|
|
|
2014-02-12 19:16:22 +08:00
|
|
|
if (ifp->options->script &&
|
|
|
|
(ifp->options->script[0] == '\0' ||
|
2015-06-03 18:13:37 +08:00
|
|
|
strcmp(ifp->options->script, "/dev/null") == 0) &&
|
|
|
|
TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
|
2012-01-31 00:59:48 +08:00
|
|
|
return 0;
|
2013-04-02 15:01:11 +08:00
|
|
|
|
2009-01-14 01:04:28 +08:00
|
|
|
/* Make our env */
|
2014-03-28 06:14:52 +08:00
|
|
|
elen = (size_t)make_env(ifp, reason, &env);
|
|
|
|
if (elen == (size_t)-1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: make_env: %m", ifp->name);
|
2014-03-28 06:14:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-06-03 18:13:37 +08:00
|
|
|
|
|
|
|
if (ifp->options->script &&
|
|
|
|
(ifp->options->script[0] == '\0' ||
|
|
|
|
strcmp(ifp->options->script, "/dev/null") == 0))
|
|
|
|
goto send_listeners;
|
|
|
|
|
|
|
|
argv[0] = ifp->options->script ? ifp->options->script : UNCONST(SCRIPT);
|
|
|
|
argv[1] = NULL;
|
|
|
|
logger(ifp->ctx, LOG_DEBUG, "%s: executing `%s' %s",
|
|
|
|
ifp->name, argv[0], reason);
|
|
|
|
|
2014-09-12 16:34:47 +08:00
|
|
|
/* Resize for PATH and RC_SVCNAME */
|
|
|
|
svcname = getenv(RC_SVCNAME);
|
2014-09-09 16:22:10 +08:00
|
|
|
ep = realloc(env, sizeof(char *) * (elen + 2 + (svcname ? 1 : 0)));
|
2013-02-16 06:20:18 +08:00
|
|
|
if (ep == NULL) {
|
2014-03-28 06:14:52 +08:00
|
|
|
elen = 0;
|
2013-02-16 06:20:18 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
env = ep;
|
2009-01-14 01:04:28 +08:00
|
|
|
/* Add path to it */
|
|
|
|
path = getenv("PATH");
|
|
|
|
if (path) {
|
|
|
|
e = strlen("PATH") + strlen(path) + 2;
|
2013-02-16 21:21:35 +08:00
|
|
|
env[elen] = malloc(e);
|
|
|
|
if (env[elen] == NULL) {
|
2014-03-28 06:14:52 +08:00
|
|
|
elen = 0;
|
2013-02-16 21:21:35 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2009-01-14 01:04:28 +08:00
|
|
|
snprintf(env[elen], e, "PATH=%s", path);
|
2013-02-16 06:46:47 +08:00
|
|
|
} else {
|
|
|
|
env[elen] = strdup(DEFAULT_PATH);
|
|
|
|
if (env[elen] == NULL) {
|
2014-03-28 06:14:52 +08:00
|
|
|
elen = 0;
|
2013-02-16 06:46:47 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 16:22:10 +08:00
|
|
|
if (svcname) {
|
2014-09-12 16:34:47 +08:00
|
|
|
e = strlen(RC_SVCNAME) + strlen(svcname) + 2;
|
2014-09-09 16:22:10 +08:00
|
|
|
env[++elen] = malloc(e);
|
|
|
|
if (env[elen] == NULL) {
|
|
|
|
elen = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
2014-09-12 16:34:47 +08:00
|
|
|
snprintf(env[elen], e, "%s=%s", RC_SVCNAME, svcname);
|
2014-09-09 16:22:10 +08:00
|
|
|
}
|
2013-11-19 17:43:54 +08:00
|
|
|
env[++elen] = NULL;
|
2009-01-14 01:04:28 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
pid = exec_script(ifp->ctx, argv, env);
|
2012-11-11 08:25:58 +08:00
|
|
|
if (pid == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: %s: %m", __func__, argv[0]);
|
2012-11-11 08:25:58 +08:00
|
|
|
else if (pid != 0) {
|
2008-08-11 17:28:53 +08:00
|
|
|
/* Wait for the script to finish */
|
|
|
|
while (waitpid(pid, &status, 0) == -1) {
|
|
|
|
if (errno != EINTR) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "waitpid: %m");
|
2012-11-11 08:04:02 +08:00
|
|
|
status = 0;
|
2008-08-11 17:28:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-05-07 17:39:17 +08:00
|
|
|
}
|
2012-11-11 20:19:19 +08:00
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status))
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
2013-02-02 22:05:55 +08:00
|
|
|
"%s: %s: WEXITSTATUS %d",
|
|
|
|
__func__, argv[0], WEXITSTATUS(status));
|
2012-11-11 20:19:19 +08:00
|
|
|
} else if (WIFSIGNALED(status))
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: %s: %s",
|
2013-02-02 22:05:55 +08:00
|
|
|
__func__, argv[0], strsignal(WTERMSIG(status)));
|
2008-05-07 17:39:17 +08:00
|
|
|
}
|
2008-04-13 07:00:23 +08:00
|
|
|
|
2015-06-03 18:13:37 +08:00
|
|
|
send_listeners:
|
2009-01-13 00:39:01 +08:00
|
|
|
/* Send to our listeners */
|
|
|
|
bigenv = NULL;
|
2014-09-11 19:37:10 +08:00
|
|
|
status = 0;
|
2014-09-05 20:28:05 +08:00
|
|
|
TAILQ_FOREACH(fd, &ifp->ctx->control_fds, next) {
|
2014-09-15 03:12:57 +08:00
|
|
|
if (!(fd->flags & FD_LISTEN))
|
2014-09-05 20:28:05 +08:00
|
|
|
continue;
|
|
|
|
if (bigenv == NULL) {
|
|
|
|
elen = (size_t)arraytostr((const char *const *)env,
|
|
|
|
&bigenv);
|
|
|
|
if ((ssize_t)elen == -1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: arraytostr: %m",
|
2014-09-05 20:28:05 +08:00
|
|
|
ifp->name);
|
|
|
|
break;
|
2009-01-13 00:39:01 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-05 20:28:05 +08:00
|
|
|
if (control_queue(fd, bigenv, elen, 1) == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
|
|
|
"%s: control_queue: %m", __func__);
|
2014-09-11 19:37:10 +08:00
|
|
|
else
|
|
|
|
status = 1;
|
2009-01-13 00:39:01 +08:00
|
|
|
}
|
2014-09-11 19:37:10 +08:00
|
|
|
if (!status)
|
|
|
|
free(bigenv);
|
2009-01-13 00:39:01 +08:00
|
|
|
|
2013-02-16 06:20:18 +08:00
|
|
|
out:
|
2008-05-13 19:21:19 +08:00
|
|
|
/* Cleanup */
|
|
|
|
ep = env;
|
|
|
|
while (*ep)
|
|
|
|
free(*ep++);
|
|
|
|
free(env);
|
2014-03-28 06:14:52 +08:00
|
|
|
if (elen == 0)
|
2013-02-16 06:20:18 +08:00
|
|
|
return -1;
|
2012-11-11 08:25:58 +08:00
|
|
|
return WEXITSTATUS(status);
|
2007-01-17 01:54:07 +08:00
|
|
|
}
|