commit fce2eac2ac0adcf5ad9cb2534a5ef531d9324374
parent 1f0147a27e3d65754b01dbdfd3c510500d89d1d9
Author: xplshn <anto@xplshn.com.ar>
Date: Mon, 23 Nov 2026 21:52:45 -0300
make: bmake's .if and .include directives behind FEATURE_MAKE_BMAKE, sinclude behind gmake's. pidof, killall5 and pwdx over the process table interface
Diffstat:
7 files changed, 148 insertions(+), 105 deletions(-)
diff --git a/cmd/posix/make/parser.c b/cmd/posix/make/parser.c
@@ -360,6 +360,9 @@ sinclude(char *s)
free(fil);
}
+#if FEATURE_MAKE_BMAKE
+/* bmake compat: .if .ifdef .ifndef .elif .else .endif and .error .warning
+ * conditionals, which POSIX 2024 does not have */
#define MAXCOND 32
static struct {
int active;
@@ -497,12 +500,23 @@ eval_if(char *expr)
return neg ? !result : result;
}
+#else
+static int
+cond_active(void)
+{
+ return 1;
+}
+#endif
+
static char *
nextline(void)
{
int c;
FILE *fp;
- char *s, *lim, d[BUFSIZ];
+ char *s, *lim;
+#if FEATURE_MAKE_BMAKE
+ char d[BUFSIZ];
+#endif
assert(input->type == FTFILE);
@@ -532,7 +546,8 @@ repeat:
if (!strcmp(input->buf, ""))
goto repeat;
- /* bmake style .include / .sinclude / .-include */
+#if FEATURE_MAKE_BMAKE
+ /* bmake compat: .include .sinclude .-include and the conditionals */
if (input->buf[0] == '.') {
size_t dl;
/* copy and strip trailing whitespace/newline so strcmp works */
@@ -622,6 +637,8 @@ repeat:
}
}
+#endif
+
/* POSIX include */
if (!strncmp(input->buf, "include", 7) && isblank(input->buf[7])) {
input->pos = input->siz;
@@ -630,9 +647,12 @@ repeat:
goto repeat;
}
- /* sinclude / -include (GNU/POSIX style without dot) */
- if ((!strncmp(input->buf, "sinclude", 8) && isblank(input->buf[8]))
- || (!strncmp(input->buf, "-include", 8) && isblank(input->buf[8]))) {
+ /* -include is POSIX 2024's, sinclude is gmake's */
+ if ((!strncmp(input->buf, "-include", 8) && isblank(input->buf[8]))
+#if FEATURE_MAKE_GMAKE
+ || (!strncmp(input->buf, "sinclude", 8) && isblank(input->buf[8]))
+#endif
+ ) {
input->pos = input->siz;
if (cond_active())
sinclude(input->buf + 8);
diff --git a/cmd/pseudo/killall5.c b/cmd/pseudo/killall5.c
@@ -1,6 +1,5 @@
/* see LICENSE file for copyright and license details */
-#include <dirent.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
@@ -37,6 +36,27 @@ usage(void)
eprintf("usage: %s [-o pid1,pid2,..,pidN] [-s signal]\n", argv0);
}
+struct killall {
+ int sig, oflag;
+};
+
+static int
+each(const struct procinfo *p, void *arg)
+{
+ struct killall *w = arg;
+ struct pidentry *pe;
+
+ if (p->pid == 1 || p->pid == getpid() || getsid(p->pid) == getsid(0) || getsid(p->pid) == 0)
+ return 0;
+ if (w->oflag) {
+ SLIST_FOREACH(pe, &omitpid_head, entry)
+ if (pe->pid == p->pid)
+ return 0;
+ }
+ kill(p->pid, w->sig);
+ return 0;
+}
+
// ?man killall5: send signal to all processes
// ?man arguments: -o pid1
// ?man send a signal to all processes except kernel threads
@@ -44,26 +64,22 @@ int
main(int argc, char *argv[])
{
struct pidentry *pe;
- struct dirent *entry;
- DIR *dp;
+ struct killall w = {SIGTERM, 0};
char *p, *arg = NULL;
char *end, *v;
- int oflag = 0;
- int sig = SIGTERM;
- pid_t pid;
size_t i;
ARGBEGIN
{
// ?man -s:str: silent mode or print summary
case 's':
- v = EARGF(usage());
- sig = strtol(v, &end, 0);
+ v = EARGF(usage());
+ w.sig = strtol(v, &end, 0);
if (*end == '\0')
break;
for (i = 0; i < LEN(sigs); i++) {
if (strcasecmp(v, sigs[i].name) == 0) {
- sig = sigs[i].sig;
+ w.sig = sigs[i].sig;
break;
}
}
@@ -72,8 +88,8 @@ main(int argc, char *argv[])
break;
// ?man -o:str: specify output file
case 'o':
- oflag = 1;
- arg = EARGF(usage());
+ w.oflag = 1;
+ arg = EARGF(usage());
break;
default:
usage();
@@ -82,7 +98,7 @@ main(int argc, char *argv[])
SLIST_INIT(&omitpid_head);
- if (oflag) {
+ if (w.oflag) {
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
pe = emalloc(sizeof(*pe));
pe->pid = estrtol(p, 10);
@@ -90,29 +106,13 @@ main(int argc, char *argv[])
}
}
- if (sig != SIGSTOP && sig != SIGCONT)
+ if (w.sig != SIGSTOP && w.sig != SIGCONT)
kill(-1, SIGSTOP);
- if (!(dp = opendir("/proc")))
- eprintf("opendir /proc:");
- while ((entry = readdir(dp))) {
- if (pidfile(entry->d_name) == 0)
- continue;
- pid = estrtol(entry->d_name, 10);
- if (pid == 1 || pid == getpid() || getsid(pid) == getsid(0) || getsid(pid) == 0)
- continue;
- if (oflag == 1) {
- SLIST_FOREACH(pe, &omitpid_head, entry)
- if (pe->pid == pid)
- break;
- if (pe)
- continue;
- }
- kill(pid, sig);
- }
- closedir(dp);
+ if (proc_each(each, &w) < 0)
+ eprintf("the process table cannot be read:");
- if (sig != SIGSTOP && sig != SIGCONT)
+ if (w.sig != SIGSTOP && w.sig != SIGCONT)
kill(-1, SIGCONT);
return 0;
diff --git a/cmd/pseudo/pidof.c b/cmd/pseudo/pidof.c
@@ -27,31 +27,71 @@ usage(void)
eprintf("usage: %s [-o pid1,pid2,...pidN] [-s] [program...]\n", argv0);
}
+/* what main sets for the walk of the process table */
+struct pidof {
+ int argc;
+ char **argv;
+ int sflag, oflag, found;
+};
+
+static int
+each(const struct procinfo *p, void *arg)
+{
+ struct pidof *w = arg;
+ struct pidentry *pe;
+ char cmdline[BUFSIZ], *cmd, *cmdbase, *sp;
+ char word[16];
+ int i;
+
+ if (w->oflag) {
+ SLIST_FOREACH(pe, &omitpid_head, entry)
+ if (pe->pid == p->pid)
+ return 0;
+ }
+ if (proc_args(p->pid, cmdline, sizeof(cmdline)) < 0) {
+ cmd = (char *)p->comm;
+ cmdbase = cmd;
+ } else {
+ if ((sp = strchr(cmdline, ' ')))
+ *sp = '\0';
+ cmd = cmdline;
+ cmdbase = basename(cmdline);
+ }
+ /* workaround for login shells */
+ if (cmd[0] == '-')
+ cmd++;
+ snprintf(word, sizeof(word), "%d", (int)p->pid);
+ for (i = 0; i < w->argc; i++) {
+ if (strcmp(cmd, w->argv[i]) == 0 || strcmp(cmdbase, w->argv[i]) == 0) {
+ putword(stdout, word);
+ w->found++;
+ if (w->sflag)
+ return 1;
+ }
+ }
+ return 0;
+}
+
// ?man pidof: find process ids
// ?man arguments: -o pid1
// ?man find the process identity numbers of running programs
int
main(int argc, char *argv[])
{
- DIR *dp;
- struct dirent *entry;
- pid_t pid;
- struct procstat ps;
- char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *p, *arg = NULL;
- int i, found = 0;
- int sflag = 0, oflag = 0;
+ char *p, *arg = NULL;
+ struct pidof w = {0};
struct pidentry *pe;
ARGBEGIN
{
// ?man -s: silent mode or print summary
case 's':
- sflag = 1;
+ w.sflag = 1;
break;
// ?man -o:str: specify output file
case 'o':
- oflag = 1;
- arg = EARGF(usage());
+ w.oflag = 1;
+ arg = EARGF(usage());
break;
default:
usage();
@@ -60,10 +100,12 @@ main(int argc, char *argv[])
if (!argc)
return 1;
+ w.argc = argc;
+ w.argv = argv;
SLIST_INIT(&omitpid_head);
- if (oflag) {
+ if (w.oflag) {
for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
pe = emalloc(sizeof(*pe));
if (strcmp(p, "%PPID") == 0)
@@ -74,49 +116,10 @@ main(int argc, char *argv[])
}
}
- if (!(dp = opendir("/proc")))
- eprintf("opendir /proc:");
-
- while ((entry = readdir(dp))) {
- if (!pidfile(entry->d_name))
- continue;
- pid = estrtol(entry->d_name, 10);
- if (oflag) {
- SLIST_FOREACH(pe, &omitpid_head, entry)
- if (pe->pid == pid)
- break;
- if (pe)
- continue;
- }
- if (parsestat(pid, &ps) < 0)
- continue;
- if (parsecmdline(ps.pid, cmdline, sizeof(cmdline)) < 0) {
- cmd = ps.comm;
- cmdbase = cmd;
- } else {
- if ((p = strchr(cmdline, ' ')))
- *p = '\0';
- cmd = cmdline;
- cmdbase = basename(cmdline);
- }
- /* workaround for login shells */
- if (cmd[0] == '-')
- cmd++;
- for (i = 0; i < argc; i++) {
- if (strcmp(cmd, argv[i]) == 0 || strcmp(cmdbase, argv[i]) == 0) {
- putword(stdout, entry->d_name);
- found++;
- if (sflag)
- goto out;
- }
- }
- }
-
-out:
- if (found)
+ if (proc_each(each, &w) < 0)
+ eprintf("the process table cannot be read:");
+ if (w.found)
putchar('\n');
- closedir(dp);
-
- return found ? 0 : 1;
+ return w.found ? 0 : 1;
}
diff --git a/cmd/pseudo/pwdx.c b/cmd/pseudo/pwdx.c
@@ -6,6 +6,7 @@
#include <string.h>
#include <unistd.h>
+#include "proc.h"
#include "util.h"
static void
@@ -20,10 +21,8 @@ usage(void)
int
main(int argc, char *argv[])
{
- int ret = 0;
- char path[PATH_MAX];
- char target[PATH_MAX + sizeof(" (deleted)")];
- ssize_t n;
+ int ret = 0;
+ char target[PATH_MAX + sizeof(" (deleted)")];
ARGBEGIN
{
@@ -36,16 +35,11 @@ main(int argc, char *argv[])
usage();
for (; argc > 0; argc--, argv++) {
- n = snprintf(path, sizeof(path), "/proc/%s/cwd", *argv);
- if (n < 0 || (size_t)n >= sizeof(path)) {
- errno = ESRCH;
- } else {
- n = readlink(path, target, sizeof(target) - 1);
- if (n >= 0) {
- target[n] = '\0';
- printf("%s: %s\n", *argv, target);
- continue;
- }
+ pid_t pid = (pid_t)estrtonum(*argv, 1, INT_MAX);
+
+ if (proc_cwd(pid, target, sizeof(target)) == 0) {
+ printf("%s: %s\n", *argv, target);
+ continue;
}
if (errno == ENOENT)
errno = ESRCH;
diff --git a/shared/libutil/proc_linux.c b/shared/libutil/proc_linux.c
@@ -236,6 +236,19 @@ proc_args(pid_t pid, char *buf, size_t n)
return parsecmdline(pid, buf, n);
}
+int
+proc_cwd(pid_t pid, char *buf, size_t n)
+{
+ char path[PATH_MAX];
+ ssize_t k;
+
+ snprintf(path, sizeof(path), ARUU_LINUX_PATH_PROC "/%d/cwd", (int)pid);
+ if (n == 0 || (k = readlink(path, buf, n - 1)) < 0)
+ return -1;
+ buf[k] = '\0';
+ return 0;
+}
+
void
proc_ttyname(unsigned major, unsigned minor, char *buf, size_t n)
{
diff --git a/shared/libutil/proc_other.c b/shared/libutil/proc_other.c
@@ -71,6 +71,16 @@ proc_args(pid_t pid, char *buf, size_t n)
return parsecmdline(pid, buf, n);
}
+int
+proc_cwd(pid_t pid, char *buf, size_t n)
+{
+ (void)pid;
+ (void)buf;
+ (void)n;
+ errno = ENOTSUP;
+ return -1;
+}
+
void
proc_ttyname(unsigned major, unsigned minor, char *buf, size_t n)
{
diff --git a/shared/proc.h b/shared/proc.h
@@ -78,6 +78,9 @@ int proc_info(pid_t pid, struct procinfo *p);
/* the command line, its words joined by spaces. -1 for a process with none,
* a kernel thread or a zombie */
int proc_args(pid_t pid, char *buf, size_t n);
+/* the working directory of a process, -1 when it cannot be told: it is gone,
+ * it is another user's, or the kernel keeps no such thing */
+int proc_cwd(pid_t pid, char *buf, size_t n);
/* the terminal's name as ps shows it: pts/3, tty1 or ? for none */
void proc_ttyname(unsigned major, unsigned minor, char *buf, size_t n);