if (prefix)
len += strlen(prefix) + 1;
- **e = xmalloc(len);
+ **e = malloc(len);
+ if (**e == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
if (prefix)
snprintf(**e, len, "%s_%s=%s", prefix, var, value);
else
return -1;
return 0;
}
-
-#ifndef xmalloc
-void *
-xmalloc(size_t s)
-{
- void *value = malloc(s);
-
- if (value != NULL)
- return value;
- syslog(LOG_ERR, "memory exhausted (xalloc %zu bytes)", s);
- exit (EXIT_FAILURE);
- /* NOTREACHED */
-}
-#endif
ssize_t setvard(char ***, const char *, const char *, int);
time_t uptime(void);
int writepid(int, pid_t);
-void *xmalloc(size_t);
#endif
l = decode_rfc3397(NULL, 0, dl, data);
if (l < 1)
return l;
- tmp = xmalloc(l);
+ tmp = malloc(l);
+ if (tmp == NULL)
+ return -1;
decode_rfc3397(tmp, l, dl, data);
l = print_string(s, len, l - 1, (uint8_t *)tmp);
free(tmp);
if (o == opt) {
if (op) {
if (!opt_buffer) {
- opt_buffer = xmalloc(sizeof(*dhcp));
+ opt_buffer = malloc(sizeof(*dhcp));
+ if (opt_buffer == NULL)
+ return NULL;
#ifdef DEBUG_MEMORY
atexit(free_option_buffer);
#endif
switch (enc) {
case 0:
if ((l = decode_rfc3397(NULL, 0, dl, data)) > 0) {
- sip = xmalloc(l);
+ sip = malloc(l);
+ if (sip == NULL)
+ return 0;
decode_rfc3397(sip, l, dl, data);
}
break;
}
addr.s_addr = INADDR_BROADCAST;
l = ((dl / sizeof(addr.s_addr)) * ((4 * 4) + 1)) + 1;
- sip = p = xmalloc(l);
+ sip = p = malloc(l);
+ if (sip == NULL)
+ return 0;
while (dl != 0) {
memcpy(&addr.s_addr, data, sizeof(addr.s_addr));
data += sizeof(addr.s_addr);
errno = EINVAL;
return NULL;
}
- s = xmalloc(sizeof(char) * type);
- decode_rfc3397(s, type, len, p);
+ s = malloc(sizeof(char) * type);
+ if (s)
+ decode_rfc3397(s, type, len, p);
return s;
}
if (type & RFC3361)
return decode_rfc3361(len, p);
- s = xmalloc(sizeof(char) * (len + 1));
- memcpy(s, p, len);
- s[len] = '\0';
+ s = malloc(sizeof(char) * (len + 1));
+ if (s) {
+ memcpy(s, p, len);
+ s[len] = '\0';
+ }
return s;
}
}
syslog(LOG_DEBUG, "%s: reading lease `%s'",
ifp->name, state->leasefile);
- dhcp = xmalloc(sizeof(*dhcp));
- memset(dhcp, 0, sizeof(*dhcp));
+ dhcp = calloc(1, sizeof(*dhcp));
+ if (dhcp == NULL) {
+ close(fd);
+ return NULL;
+ }
bytes = read(fd, dhcp, sizeof(*dhcp));
close(fd);
if (bytes < 0) {
if (len < 0)
return -1;
e = strlen(prefix) + strlen(opt->var) + len + 4;
- v = val = *ep++ = xmalloc(e);
+ v = val = *ep++ = malloc(e);
+ if (v == NULL)
+ return -1;
v += snprintf(val, e, "%s_%s=", prefix, opt->var);
if (len != 0)
print_option(v, len, opt->type, pl, p, ifp->name);
/* We loop through until our buffer is empty.
* The benefit is that if we get >1 DHCP packet in our buffer and
* the first one fails for any reason, we can use the next. */
- if (packet == NULL)
- packet = xmalloc(udp_dhcp_len);
+ if (packet == NULL) {
+ packet = malloc(udp_dhcp_len);
+ if (packet == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return;
+ }
+ }
+
for(;;) {
bytes = ipv4_getrawpacket(iface, ETHERTYPE_IP,
packet, udp_dhcp_len, &partialcsum);
state->clientid = NULL;
if (*ifo->clientid) {
- state->clientid = xmalloc(ifo->clientid[0] + 1);
+ state->clientid = malloc(ifo->clientid[0] + 1);
+ if (state->clientid == NULL)
+ goto eexit;
memcpy(state->clientid, ifo->clientid, ifo->clientid[0] + 1);
} else if (ifo->options & DHCPCD_CLIENTID) {
len = 0;
if (ifo->options & DHCPCD_DUID) {
- duid = xmalloc(DUID_LEN);
+ duid = malloc(DUID_LEN);
+ if (duid == NULL)
+ goto eexit;
if ((len = get_duid(duid, ifp)) == 0)
syslog(LOG_ERR, "get_duid: %m");
} else
duid = NULL;
if (len > 0) {
- state->clientid = xmalloc(len + 6);
+ state->clientid = malloc(len + 6);
+ if (state->clientid == NULL)
+ goto eexit;
state->clientid[0] = len + 5;
state->clientid[1] = 255; /* RFC 4361 */
ifl = strlen(ifp->name);
memcpy(state->clientid + 6, duid, len);
} else if (len == 0) {
len = ifp->hwlen + 1;
- state->clientid = xmalloc(len + 1);
+ state->clientid = malloc(len + 1);
+ if (state->clientid == NULL)
+ goto eexit;
state->clientid[0] = len;
state->clientid[1] = ifp->family;
memcpy(state->clientid + 2, ifp->hwaddr,
else if (ifp->hwlen)
syslog(LOG_DEBUG, "%s: using hwaddr %s", ifp->name,
hwaddr_ntoa(ifp->hwaddr, ifp->hwlen));
-
return 0;
+
+eexit:
+ syslog(LOG_ERR, "%s: Error making ClientID: %m", __func__);
+ return -1;
}
void
if (len < 0)
return -1;
e = strlen(prefix) + 6 + strlen(opt->var) + len + 4;
- v = val = *ep++ = xmalloc(e);
+ v = val = *ep++ = malloc(e);
+ if (v == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
v += snprintf(val, e, "%s_dhcp6_%s=", prefix, opt->var);
if (len != 0)
print_option(v, len, opt->type, ol, od, ifp->name);
TAILQ_FOREACH(ap, &state->addrs, next) {
e += strlen(ap->saddr) + 1;
}
- v = val = *ep++ = xmalloc(e);
+ v = val = *ep++ = malloc(e);
+ if (v == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
v += snprintf(val, e, "%s_dhcp6_ip_address=", prefix);
TAILQ_FOREACH(ap, &state->addrs, next) {
strcpy(v, ap->saddr);
len = 0;
for (opt = 0; opt < argc; opt++)
len += strlen(argv[opt]) + 1;
- tmp = p = xmalloc(len + 1);
+ tmp = p = malloc(len + 1);
+ if (tmp == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
for (opt = 0; opt < argc; opt++) {
l = strlen(argv[opt]);
strlcpy(p, argv[opt], l + 1);
/* We need at least one interface */
if (optind == argc) {
- syslog(LOG_ERR, "handle_args: no interface");
+ syslog(LOG_ERR, "%s: no interface", __func__);
return -1;
}
/* If we have any other args, we should run as a single dhcpcd
* instance for that interface. */
len = strlen(PIDFILE) + IF_NAMESIZE + 2;
- pidfile = xmalloc(len);
+ pidfile = malloc(len);
+ if (pidfile == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ exit(EXIT_FAILURE);
+ }
if (optind == argc - 1)
snprintf(pidfile, len, PIDFILE, "-", argv[optind]);
else {
if (free_events) {
e = free_events;
free_events = e->next;
- } else
- e = xmalloc(sizeof(*e));
+ } else {
+ e = malloc(sizeof(*e));
+ if (e == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return;
+ }
+ }
+
e->fd = fd;
e->callback = callback;
e->arg = arg;
if (free_timeouts) {
t = free_timeouts;
free_timeouts = t->next;
- } else
- t = xmalloc(sizeof(*t));
+ } else {
+ t = malloc(sizeof(*t));
+ if (t == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return;
+ }
+ }
}
t->when.tv_sec = w.tv_sec;
while ((p = strchr(arg, ',')))
*p = ' ';
s = strlen("skip_hooks=") + strlen(arg) + 1;
- p = xmalloc(sizeof(char) * s);
+ p = malloc(sizeof(char) * s);
+ if (p == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
snprintf(p, s, "skip_hooks=%s", arg);
add_environ(ifo, p, 0);
free(p);
rt = ifo->routes;
while (rt->next)
rt = rt->next;
- rt->next = xmalloc(sizeof(*rt));
+ rt->next = malloc(sizeof(*rt));
+ if (rt->next == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
rt = rt->next;
}
rt->next = NULL;
rt = ifo->routes;
while (rt->next)
rt = rt->next;
- rt->next = xmalloc(sizeof(*rt));
+ rt->next = malloc(sizeof(*rt));
+ if (rt->next == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
rt = rt->next;
}
rt->dest.s_addr = INADDR_ANY;
iface->options->req_addr.s_addr == INADDR_ANY))
return rt;
- r = xmalloc(sizeof(*r));
+ r = malloc(sizeof(*r));
+ if (r == NULL)
+ return NULL;
r->dest.s_addr = s->addr.s_addr & s->net.s_addr;
r->net.s_addr = s->net.s_addr;
r->gate.s_addr = 0;
if (rt->gate.s_addr == 0)
break;
if (r == NULL)
- r = nrt = xmalloc(sizeof(*r));
+ r = nrt = malloc(sizeof(*r));
else {
- r->next = xmalloc(sizeof(*r));
+ r->next = malloc(sizeof(*r));
r = r->next;
}
+ if (r == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ ipv4_freeroutes(nrt);
+ return NULL;
+ }
memcpy(r, rt, sizeof(*r));
r->next = NULL;
}
if (!(iface->flags & IFF_POINTOPOINT) ||
!has_option_mask(iface->options->dstmask, DHO_ROUTER))
return rt;
- r = xmalloc(sizeof(*r));
+ r = malloc(sizeof(*r));
+ if (r == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
r->dest.s_addr = INADDR_ANY;
r->net.s_addr = INADDR_ANY;
r->gate.s_addr = D_CSTATE(iface)->dst.s_addr;
}
syslog(LOG_WARNING, "%s: router %s requires a host route",
ifp->name, inet_ntoa(rtp->gate));
- rtn = xmalloc(sizeof(*rtn));
+ rtn = malloc(sizeof(*rtn));
+ if (rtn == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ continue;
+ }
rtn->dest.s_addr = rtp->gate.s_addr;
rtn->net.s_addr = INADDR_BROADCAST;
rtn->gate.s_addr = rtp->gate.s_addr;
}
if (ifa) {
- in6 = xmalloc(sizeof(*in6));
+ in6 = malloc(sizeof(*in6));
+ if (in6 == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
memcpy(in6, &sa6->sin6_addr, sizeof(*in6));
} else
in6 = NULL;
}
}
- nrs = xmalloc(sizeof(*nrs));
+ nrs = malloc(sizeof(*nrs));
+ if (nrs == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return;
+ }
TAILQ_INIT(nrs);
have_default = 0;
TAILQ_FOREACH_SAFE(rt, &dnr, next, rtn) {
} else
new_rap = 0;
if (rap->data_len == 0) {
- rap->data = xmalloc(len);
+ rap->data = malloc(len);
+ if (rap->data == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ if (new_rap)
+ free(rap);
+ return;
+ }
memcpy(rap->data, icp, len);
rap->data_len = len;
}
!(pi->nd_opt_pi_flags_reserved &
ND_OPT_PI_FLAG_ONLINK))
break;
- ap = xmalloc(sizeof(*ap));
+ ap = malloc(sizeof(*ap));
+ if (ap == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ break;
+ }
ap->new = 1;
ap->onlink = 0;
ap->prefix_len = pi->nd_opt_pi_prefix_len;
}
if (rao == NULL) {
- rao = xmalloc(sizeof(*rao));
+ rao = malloc(sizeof(*rao));
+ if (rao == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ continue;
+ }
rao->type = ndo->nd_opt_type;
rao->option = opt;
TAILQ_INSERT_TAIL(&rap->options, rao, next);
if (env) {
snprintf(buffer, sizeof(buffer),
"ra%d_from", i);
- setvar(&env, prefix, buffer, rap->sfrom);
+ if (setvar(&env, prefix, buffer, rap->sfrom) == -1)
+ return -1;
}
l++;
new = realloc(**var,
strlen(**var) + 1 +
strlen(rao->option) + 1);
- if (new) {
- **var = new;
- new += strlen(new);
- *new++ = ' ';
- strcpy(new, rao->option);
- continue;
- }
+ if (new == NULL)
+ return -1;
+ **var = new;
+ new += strlen(new);
+ *new++ = ' ';
+ strcpy(new, rao->option);
+ continue;
}
if (env) {
snprintf(buffer, sizeof(buffer),
"ra%d_%s", i, optn);
- setvar(&env, prefix, buffer, rao->option);
+ if (setvar(&env, prefix, buffer, rao->option)
+ == -1)
+ return -1;
}
}
}
- if (env)
- setvard(&env, prefix, "ra_count", i);
+ if (env) {
+ if (setvard(&env, prefix, "ra_count", i) == -1)
+ return -1;
+ }
l++;
return l;
}
char *v;
len = strlen(prefix) + strlen(var) + 2;
- v = xmalloc(len);
+ v = malloc(len);
+ if (v == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
snprintf(v, len, "%s_%s", prefix, var);
return v;
}
-static void
+static int
append_config(char ***env, ssize_t *len,
const char *prefix, const char *const *config)
{
ssize_t i, j, e1;
- char **ne, *eq, **nep;
+ char **ne, *eq, **nep, *p;
if (config == NULL)
- return;
+ return 0;
ne = *env;
for (i = 0; config[i] != NULL; i++) {
{
free(ne[j]);
ne[j] = make_var(prefix, config[i]);
+ if (ne[j] == NULL)
+ return -1;
break;
}
}
if (j == *len) {
j++;
+ p = make_var(prefix, config[i]);
+ if (p == NULL)
+ return -1;
nep = realloc(ne, sizeof(char *) * (j + 1));
if (nep == NULL) {
syslog(LOG_ERR, "%s: %m", __func__);
- break;
+ return -1;
}
ne = nep;
- ne[j - 1] = make_var(prefix, config[i]);
+ ne[j - 1] = p;
*len = j;
}
}
*env = ne;
+ return 0;
}
#endif
ap = argv;
while (*ap)
len += strlen(*ap++) + 1;
- *s = p = xmalloc(len);
+ *s = p = malloc(len);
+ if (p == NULL)
+ return -1;
ap = argv;
while (*ap) {
l = strlen(*ap) + 1;
else
elen = 10;
+#define EMALLOC(i, l) if ((env[(i)] = malloc(l)) == NULL) goto eexit;
/* Make our env */
env = calloc(1, sizeof(char *) * (elen + 1));
if (env == NULL)
goto eexit;
e = strlen("interface") + strlen(ifp->name) + 2;
- env[0] = xmalloc(e);
+ EMALLOC(0, e);
snprintf(env[0], e, "interface=%s", ifp->name);
e = strlen("reason") + strlen(reason) + 2;
- env[1] = xmalloc(e);
+ EMALLOC(1, e);
snprintf(env[1], e, "reason=%s", reason);
if (options & DHCPCD_DUMPLEASE)
goto dumplease;
-
e = 20;
- env[2] = xmalloc(e);
+ EMALLOC(2, e);
snprintf(env[2], e, "pid=%d", getpid());
- env[3] = xmalloc(e);
+ EMALLOC(3, e);
snprintf(env[3], e, "ifmetric=%d", ifp->metric);
- env[4] = xmalloc(e);
+ EMALLOC(4, e);
snprintf(env[4], e, "ifwireless=%d", ifp->wireless);
- env[5] = xmalloc(e);
+ EMALLOC(5, e);
snprintf(env[5], e, "ifflags=%u", ifp->flags);
- env[6] = xmalloc(e);
+ EMALLOC(6, e);
snprintf(env[6], e, "ifmtu=%d", get_mtu(ifp->name));
l = e = strlen("interface_order=");
for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next)
e += strlen(ifp2->name) + 1;
- p = env[7] = xmalloc(e);
+ EMALLOC(7, e);
+ p = env[7];
strlcpy(p, "interface_order=", e);
e -= l;
p += l;
env[8] = strdup("if_up=false");
env[9] = strdup("if_down=true");
}
+ if (env[8] == NULL || env[9] == NULL)
+ goto eexit;
if (*ifp->profile) {
e = strlen("profile=") + strlen(ifp->profile) + 2;
- env[elen] = xmalloc(e);
+ EMALLOC(elen, e);
snprintf(env[elen++], e, "profile=%s", ifp->profile);
}
if (ifp->wireless) {
if (nenv == NULL)
goto eexit;
env = nenv;
- env[elen] = xmalloc(e);
+ EMALLOC(elen, e);
snprintf(env[elen++], e, "new_ssid=%s", ifp->ssid);
}
else if (strcmp(reason, "NOCARRIER") == 0) {
if (nenv == NULL)
goto eexit;
env = nenv;
- env[elen] = xmalloc(e);
+ EMALLOC(elen, e);
snprintf(env[elen++], e, "old_ssid=%s", ifp->ssid);
}
}
if (nenv == NULL)
goto eexit;
env = nenv;
- elen += dhcp_env(env + elen, "old", state->old, ifp);
+ l = dhcp_env(env + elen, "old", state->old, ifp);
+ if (l == -1)
+ goto eexit;
+ elen += l;
}
- append_config(&env, &elen, "old",
- (const char *const *)ifo->config);
+ if (append_config(&env, &elen, "old",
+ (const char *const *)ifo->config) == -1)
+ goto eexit;
}
#endif
#ifdef INET6
if (nenv == NULL)
goto eexit;
env = nenv;
- elen += dhcp6_env(env + elen, "old", ifp,
+ l = dhcp6_env(env + elen, "old", ifp,
d6_state->old, d6_state->old_len);
+ if (l == -1)
+ goto eexit;
+ elen += l;
}
}
#endif
if (nenv == NULL)
goto eexit;
env = nenv;
- elen += dhcp_env(env + elen, "new",
+ l = dhcp_env(env + elen, "new",
state->new, ifp);
+ if (l == -1)
+ goto eexit;
+ elen += l;
}
- append_config(&env, &elen, "new",
- (const char *const *)ifo->config);
+ if (append_config(&env, &elen, "new",
+ (const char *const *)ifo->config) == -1)
+ goto eexit;
}
#endif
#ifdef INET6
if (nenv == NULL)
goto eexit;
env = nenv;
- elen += dhcp6_env(env + elen, "new", ifp,
+ l = dhcp6_env(env + elen, "new", ifp,
d6_state->new, d6_state->new_len);
+ if (l == -1)
+ goto eexit;
+ elen += l;
}
}
if (ra) {
if (nenv == NULL)
goto eexit;
env = nenv;
- elen += ipv6rs_env(env + elen, NULL, ifp);
+ l = ipv6rs_env(env + elen, NULL, ifp);
+ if (l == -1)
+ goto eexit;
+ elen += l;
}
}
#endif
struct iovec iov[2];
int retval;
- make_env(iface, reason, &env);
+ if (make_env(iface, reason, &env) == -1)
+ return -1;
elen = arraytostr((const char *const *)env, &s);
+ if (elen == -1)
+ return -1;
iov[0].iov_base = &elen;
iov[0].iov_len = sizeof(ssize_t);
iov[1].iov_base = s;
path = getenv("PATH");
if (path) {
e = strlen("PATH") + strlen(path) + 2;
- env[elen] = xmalloc(e);
+ env[elen] = malloc(e);
+ if (env[elen] == NULL) {
+ elen = -1;
+ goto out;
+ }
snprintf(env[elen], e, "PATH=%s", path);
} else {
env[elen] = strdup(DEFAULT_PATH);