/* Call a script with a par file and env vars */ void FAST_FUNC udhcp_run_script(struct dhcp_packet *packet, const char *name) { char **envp, **curr; char *argv[3]; if (client_config.script == NULL) return; envp = fill_envp(packet); /* call script */ log1("Executing %s", client_config.script); argv[0] = (char*) client_config.script; argv[1] = (char*) name; argv[2] = NULL; wait4pid(spawn(argv)); for (curr = envp; *curr; curr++) { log2(" %s", *curr); bb_unsetenv(*curr); free(*curr); } free(envp); }
int uevent_main(int argc UNUSED_PARAM, char **argv) { struct sockaddr_nl sa; int fd; argv++; // Subscribe for UEVENT kernel messages sa.nl_family = AF_NETLINK; sa.nl_pad = 0; sa.nl_pid = getpid(); sa.nl_groups = 1 << 0; fd = xsocket(AF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); xbind(fd, (struct sockaddr *) &sa, sizeof(sa)); close_on_exec_on(fd); // Without a sufficiently big RCVBUF, a ton of simultaneous events // can trigger ENOBUFS on read, which is unrecoverable. // Reproducer: // uevent mdev & // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' // // SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &RCVBUF, sizeof(RCVBUF)); setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &RCVBUF, sizeof(RCVBUF)); if (0) { int z; socklen_t zl = sizeof(z); getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &z, &zl); bb_error_msg("SO_RCVBUF:%d", z); } for (;;) { char *netbuf; char *s, *end; ssize_t len; int idx; // In many cases, a system sits for *days* waiting // for a new uevent notification to come in. // We use a fresh mmap so that buffer is not allocated // until kernel actually starts filling it. netbuf = mmap(NULL, BUFFER_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, /* ignored: */ -1, 0); if (netbuf == MAP_FAILED) bb_perror_msg_and_die("mmap"); // Here we block, possibly for a very long time len = safe_read(fd, netbuf, BUFFER_SIZE - 1); if (len < 0) bb_perror_msg_and_die("read"); end = netbuf + len; *end = '\0'; // Each netlink message starts with "ACTION@/path" // (which we currently ignore), // followed by environment variables. if (!argv[0]) putchar('\n'); idx = 0; s = netbuf; while (s < end) { if (!argv[0]) puts(s); if (strchr(s, '=') && idx < MAX_ENV) env[idx++] = s; s += strlen(s) + 1; } env[idx] = NULL; idx = 0; while (env[idx]) putenv(env[idx++]); if (argv[0]) spawn_and_wait(argv); idx = 0; while (env[idx]) bb_unsetenv(env[idx++]); munmap(netbuf, BUFFER_SIZE); } return 0; // not reached }
void FAST_FUNC bb_unsetenv_and_free(char *var) { bb_unsetenv(var); free(var); }