Page MenuHomeFreeBSD

D62081.1795754884.diff
No OneTemporary

Authored By
Unknown
Size
13 KB
Referenced Files
None
Subscribers
None

D62081.1795754884.diff

diff --git a/usr.bin/env/env.1 b/usr.bin/env/env.1
--- a/usr.bin/env/env.1
+++ b/usr.bin/env/env.1
@@ -28,7 +28,7 @@
.\" SUCH DAMAGE.
.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp
.\"
-.Dd October 8, 2024
+.Dd November 25, 2026
.Dt ENV 1
.Os
.Sh NAME
@@ -47,6 +47,10 @@
.Op Fl P Ar altpath
.Op Fl S Ar string
.Op Fl u Ar name
+.Op Fl -block-signal Ns Op = Ns Ar sig
+.Op Fl -default-signal Ns Op = Ns Ar sig
+.Op Fl -ignore-signal Ns Op = Ns Ar sig
+.Op Fl -list-signal-handling
.Op Ar name Ns = Ns Ar value ...
.Ar utility Op Ar argument ...
.Sh DESCRIPTION
@@ -158,8 +162,65 @@
Additional information will be printed if
.Fl v
is specified multiple times.
+.It Fl -block-signal Ns Op = Ns Ar sig
+Block delivery of the signals listed in
+.Ar sig
+to the
+.Ar utility .
+.It Fl -default-signal Ns Op = Ns Ar sig
+Reset the action for the signals listed in
+.Ar sig
+to the default, and unblock them.
+.It Fl -ignore-signal Ns Op = Ns Ar sig
+Set the action for the signals listed in
+.Ar sig
+to ignore them.
+.It Fl -list-signal-handling
+Print the signals that are blocked or ignored, after processing any of the
+above options, to standard error before executing the
+.Ar utility .
.El
.Pp
+For the signal options,
+.Ar sig
+is a comma-separated list of signal names, with or without the
+.Dq SIG
+prefix, or signal numbers, as accepted by
+.Xr str2sig 3 .
+If
+.Ar sig
+is omitted, the option applies to all signals, silently skipping any
+whose action cannot be changed.
+Naming such a signal explicitly, for example
+.Dv SIGKILL
+or
+.Dv SIGSTOP
+with
+.Fl -ignore-signal ,
+is an error.
+.Dv SIGKILL
+and
+.Dv SIGSTOP
+cannot be blocked, and requests to block them are ignored.
+The signal options may be specified more than once, and later options
+take precedence for any signal listed more than once.
+.Pp
+Signal actions and the signal mask are inherited across
+.Xr execve 2 ,
+so these options can be used to give the
+.Ar utility
+a predictable signal state regardless of how
+.Nm
+itself was started.
+The exception is an ignored
+.Dv SIGCHLD ,
+which
+.Xr execve 2
+resets to the default action, so
+.Fl -ignore-signal Ns = Ns Cm CHLD
+has no effect on the
+.Ar utility .
+.Pp
The above options are only recognized when they are specified
before any
.Ar name Ns = Ns Ar value
@@ -494,6 +555,12 @@
.Bd -literal -offset indent
env name=value ... command foo=bar arg ...
.Ed
+.Pp
+Run a foo daemon with all signals unblocked and at their default actions, even
+if the caller has blocked or ignored some:
+.Bd -literal -offset indent
+env --default-signal /usr/local/bin/food
+.Ed
.Sh COMPATIBILITY
The
.Nm
@@ -504,7 +571,10 @@
.Sh SEE ALSO
.Xr printenv 1 ,
.Xr sh 1 ,
+.Xr sigaction 2 ,
+.Xr sigprocmask 2 ,
.Xr execvp 3 ,
+.Xr str2sig 3 ,
.Xr login.conf 5 ,
.Xr environ 7
.Sh STANDARDS
@@ -519,6 +589,12 @@
options are non-standard extensions supported by
.Fx ,
but which may not be available on other operating systems.
+The
+.Fl -block-signal , -default-signal , -ignore-signal
+and
+.Fl -list-signal-handling
+options are compatible with GNU
+.Nm .
.Sh HISTORY
The
.Nm
@@ -540,6 +616,12 @@
.Fl C
option was added in
.Fx 14.2 .
+The
+.Fl -block-signal , -default-signal , -ignore-signal
+and
+.Fl -list-signal-handling
+options were added in
+.Fx 16.0 .
.Sh BUGS
The
.Nm
diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c
--- a/usr.bin/env/env.c
+++ b/usr.bin/env/env.c
@@ -33,8 +33,11 @@
#include <err.h>
#include <errno.h>
+#include <getopt.h>
+#include <limits.h>
#include <login_cap.h>
#include <pwd.h>
+#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -56,6 +59,146 @@
#define EXIT_CANNOT_INVOKE 126 /* Program located, but not usable. */
#define EXIT_ENOENT 127 /* Could not find program to exec. */
+enum {
+ OPT_BLOCK_SIGNAL = CHAR_MAX + 1,
+ OPT_DEFAULT_SIGNAL,
+ OPT_IGNORE_SIGNAL,
+ OPT_LIST_SIGNAL_HANDLING,
+};
+
+static const struct option longopts[] = {
+ { "block-signal", optional_argument, NULL, OPT_BLOCK_SIGNAL },
+ { "default-signal", optional_argument, NULL, OPT_DEFAULT_SIGNAL },
+ { "ignore-signal", optional_argument, NULL, OPT_IGNORE_SIGNAL },
+ { "list-signal-handling", no_argument, NULL, OPT_LIST_SIGNAL_HANDLING },
+ { NULL, 0, NULL, 0 },
+};
+
+/*
+ * Signal dispositions and mask changes requested on the command line.
+ */
+#define SIGDISP_DEFAULT 0x01
+#define SIGDISP_IGNORE 0x02
+#define SIGDISP_NOERR 0x04 /* Set for all signals; ignore failures. */
+
+static int sigdisp[SIGRTMAX + 1];
+static sigset_t block_set, unblock_set;
+static bool sigchanged;
+
+static void
+signal_opt(int opt, int sig, bool all)
+{
+ switch (opt) {
+ case OPT_DEFAULT_SIGNAL:
+ sigdisp[sig] = SIGDISP_DEFAULT | (all ? SIGDISP_NOERR : 0);
+ /* Like GNU env, resetting to the default also unblocks. */
+ sigaddset(&unblock_set, sig);
+ sigdelset(&block_set, sig);
+ break;
+ case OPT_IGNORE_SIGNAL:
+ sigdisp[sig] = SIGDISP_IGNORE | (all ? SIGDISP_NOERR : 0);
+ break;
+ case OPT_BLOCK_SIGNAL:
+ sigaddset(&block_set, sig);
+ sigdelset(&unblock_set, sig);
+ break;
+ }
+}
+
+/*
+ * Parse the argument to one of the signal options: a comma-separated
+ * list of signal names or numbers, or all signals if there is none.
+ */
+static void
+parse_signal_opt(int opt, const char *optname, const char *arg)
+{
+ char *list, *name, *p;
+ int sig;
+
+ sigchanged = true;
+ if (arg == NULL) {
+ if (env_verbosity)
+ fprintf(stderr, "#env %s:\tall\n", optname);
+ for (sig = 1; sig <= SIGRTMAX; sig++) {
+ /* Reserved for the thread library; leave it alone. */
+ if (sig == SIGTHR)
+ continue;
+ signal_opt(opt, sig, true);
+ }
+ return;
+ }
+
+ if ((list = strdup(arg)) == NULL)
+ err(EXIT_CANCELED, "strdup");
+
+ for (p = list; (name = strsep(&p, ",")) != NULL; ) {
+ if (*name == '\0')
+ continue;
+ if (str2sig(name, &sig) != 0)
+ errx(EXIT_CANCELED, "%s: invalid signal", name);
+ if (env_verbosity)
+ fprintf(stderr, "#env %s:\t%s\n", optname, name);
+ signal_opt(opt, sig, false);
+ }
+ free(list);
+}
+
+static void
+apply_signals(void)
+{
+ struct sigaction sa;
+ int disp, sig;
+
+ memset(&sa, 0, sizeof(sa));
+ sigemptyset(&sa.sa_mask);
+ for (sig = 1; sig <= SIGRTMAX; sig++) {
+ disp = sigdisp[sig];
+ if ((disp & (SIGDISP_DEFAULT | SIGDISP_IGNORE)) == 0)
+ continue;
+ sa.sa_handler = (disp & SIGDISP_IGNORE) != 0 ?
+ SIG_IGN : SIG_DFL;
+ if (sigaction(sig, &sa, NULL) != 0 &&
+ (disp & SIGDISP_NOERR) == 0)
+ err(EXIT_CANCELED,
+ "failed to set signal action for signal %d", sig);
+ }
+
+ if (sigprocmask(SIG_UNBLOCK, &unblock_set, NULL) != 0 ||
+ sigprocmask(SIG_BLOCK, &block_set, NULL) != 0)
+ err(EXIT_CANCELED, "sigprocmask");
+}
+
+/*
+ * List blocked and ignored signals.
+ */
+static void
+list_signals(void)
+{
+ struct sigaction sa;
+ sigset_t mask;
+ char name[SIG2STR_MAX];
+ int sig;
+ bool blocked, ignored;
+
+ if (sigprocmask(SIG_BLOCK, NULL, &mask) != 0)
+ err(EXIT_CANCELED, "sigprocmask");
+ for (sig = 1; sig <= SIGRTMAX; sig++) {
+ if (sigaction(sig, NULL, &sa) != 0)
+ continue;
+
+ blocked = sigismember(&mask, sig);
+ ignored = sa.sa_handler == SIG_IGN;
+ if (!blocked && !ignored)
+ continue;
+
+ (void)sig2str(sig, name);
+ fprintf(stderr, "%-8s (%3d): %s%s%s\n", name, sig,
+ blocked ? "BLOCK" : "",
+ blocked && ignored ? "," : "",
+ ignored ? "IGNORE" : "");
+ }
+}
+
int
main(int argc, char **argv)
{
@@ -66,8 +209,9 @@
login_cap_t *lc;
bool login_as_user;
uid_t uid;
- int ch, want_clear;
+ int ch, longindex, want_clear;
int rtrn;
+ bool list_sigs;
altpath = NULL;
altwd = NULL;
@@ -78,7 +222,23 @@
login_as_user = false;
want_clear = 0;
term = '\n';
- while ((ch = getopt(argc, argv, "-0C:iL:P:S:U:u:v")) != -1)
+ list_sigs = false;
+ sigemptyset(&block_set);
+ sigemptyset(&unblock_set);
+
+ for (;;) {
+ /*
+ * `env -` needs special handling, because getopt_long(3)
+ * won't accept it like getopt(3) will. Check before calling
+ * it, so that a "-" following "--" is still a utility.
+ */
+ if (optind < argc && strcmp(argv[optind], "-") == 0) {
+ optind++;
+ ch = '-';
+ } else if ((ch = getopt_long(argc, argv, "+0C:iL:P:S:U:u:v",
+ longopts, &longindex)) == -1) {
+ break;
+ }
switch(ch) {
case '-':
case 'i':
@@ -119,10 +279,19 @@
fprintf(stderr, "#env verbosity now at %d\n",
env_verbosity);
break;
+ case OPT_BLOCK_SIGNAL:
+ case OPT_DEFAULT_SIGNAL:
+ case OPT_IGNORE_SIGNAL:
+ parse_signal_opt(ch, longopts[longindex].name, optarg);
+ break;
+ case OPT_LIST_SIGNAL_HANDLING:
+ list_sigs = true;
+ break;
case '?':
default:
usage();
}
+ }
if (want_clear) {
environ = cleanenv;
cleanenv[0] = NULL;
@@ -190,6 +359,11 @@
if (*argv) {
if (term == '\0')
errx(EXIT_CANCELED, "cannot specify command with -0");
+ /* As in GNU env, signal options only affect the utility. */
+ if (sigchanged)
+ apply_signals();
+ if (list_sigs)
+ list_signals();
if (altwd && chdir(altwd) != 0)
err(EXIT_CANCELED, "cannot change directory to '%s'",
altwd);
@@ -224,6 +398,8 @@
{
(void)fprintf(stderr,
"usage: env [-0iv] [-C workdir] [-L|-U user[/class]] [-P utilpath] [-S string]\n"
- " [-u name] [name=value ...] [utility [argument ...]]\n");
+ " [-u name] [--block-signal[=sig]] [--default-signal[=sig]]\n"
+ " [--ignore-signal[=sig]] [--list-signal-handling]\n"
+ " [name=value ...] [utility [argument ...]]\n");
exit(1);
}
diff --git a/usr.bin/env/tests/env_test.sh b/usr.bin/env/tests/env_test.sh
--- a/usr.bin/env/tests/env_test.sh
+++ b/usr.bin/env/tests/env_test.sh
@@ -44,6 +44,22 @@
atf_check env -i
}
+atf_test_case dash
+dash_head()
+{
+ atf_set descr "Historical - synonym for -i"
+}
+dash_body()
+{
+ export MAGIC_WORDS="${magic_words}"
+ atf_check -o inline:"FOO=bar\n" env - FOO=bar
+ atf_check -o inline:"FOO=bar\n" env - -u MAGIC_WORDS FOO=bar
+ atf_check -o inline:"FOO=bar\n" env -S "- FOO=bar"
+ unset MAGIC_WORDS
+ # After --, a lone - is the utility.
+ atf_check -s exit:127 -e match:"^env: -: " env -- -
+}
+
atf_test_case true
true_head()
{
@@ -149,15 +165,139 @@
atf_check -o match:"stdout" cat stderr
}
+atf_test_case block_signal
+block_signal_head()
+{
+ atf_set descr "Block signals"
+}
+block_signal_body()
+{
+ # Start each test from a known state with --default-signal.
+ atf_check -e match:"^CHLD +\( *20\): BLOCK\$" \
+ env --default-signal --block-signal=CHLD \
+ env --list-signal-handling true
+ atf_check -e match:"^HUP +\( *1\): BLOCK\$" \
+ -e match:"^TERM +\( *15\): BLOCK\$" \
+ env --default-signal --block-signal=SIGHUP,15 \
+ env --list-signal-handling true
+ atf_check -e match:"^USR1 +\( *30\): BLOCK\$" \
+ env --default-signal --block-signal \
+ env --list-signal-handling true
+ # The listing reflects changes made by the same invocation.
+ atf_check -e match:"^HUP +\( *1\): BLOCK\$" \
+ env --default-signal --block-signal=HUP \
+ --list-signal-handling true
+}
+
+atf_test_case default_signal
+default_signal_head()
+{
+ atf_set descr "Reset signals to their default action and unblock them"
+}
+default_signal_body()
+{
+ atf_check -e match:"^TERM +\( *15\): BLOCK\$" \
+ -e not-match:"^CHLD " \
+ env --default-signal --block-signal=CHLD,TERM \
+ env --default-signal=CHLD env --list-signal-handling true
+ atf_check -e not-match:"^PIPE " \
+ env --default-signal --ignore-signal=PIPE \
+ env --default-signal=PIPE env --list-signal-handling true
+ atf_check -e empty \
+ env --default-signal --block-signal --ignore-signal=INT,QUIT \
+ env --default-signal env --list-signal-handling true
+}
+
+atf_test_case ignore_signal
+ignore_signal_head()
+{
+ atf_set descr "Ignore signals"
+}
+ignore_signal_body()
+{
+ atf_check -e match:"^PIPE +\( *13\): IGNORE\$" \
+ env --default-signal --ignore-signal=PIPE \
+ env --list-signal-handling true
+ # Ignoring a signal does not unblock it.
+ atf_check -e match:"^INT +\( *2\): BLOCK,IGNORE\$" \
+ env --default-signal --block-signal=INT \
+ env --ignore-signal=INT env --list-signal-handling true
+ # execve(2) resets an ignored SIGCHLD to the default action.
+ atf_check -e not-match:"^CHLD " \
+ env --default-signal --ignore-signal=CHLD \
+ env --list-signal-handling true
+}
+
+atf_test_case signal_order
+signal_order_head()
+{
+ atf_set descr "Later signal options take precedence"
+}
+signal_order_body()
+{
+ atf_check -e not-match:"^USR1 " \
+ env --default-signal \
+ --block-signal=USR1 --default-signal=USR1 \
+ env --list-signal-handling true
+ atf_check -e match:"^USR1 +\( *30\): BLOCK\$" \
+ env --default-signal \
+ --default-signal=USR1 --block-signal=USR1 \
+ env --list-signal-handling true
+}
+
+atf_test_case signal_split
+signal_split_head()
+{
+ atf_set descr "Signal options from -S"
+}
+signal_split_body()
+{
+ opts="--default-signal --block-signal=HUP"
+ atf_check -e match:"^HUP +\( *1\): BLOCK\$" \
+ env -S "${opts} env --list-signal-handling true"
+}
+
+atf_test_case signal_noutility
+signal_noutility_head()
+{
+ atf_set descr "Signal options without a utility"
+}
+signal_noutility_body()
+{
+ atf_check -o inline:"FOO=1\n" -e empty \
+ env --block-signal=CHLD env -i --list-signal-handling FOO=1
+}
+
+atf_test_case signal_invalid
+signal_invalid_head()
+{
+ atf_set descr "Invalid signal names"
+}
+signal_invalid_body()
+{
+ atf_check -s exit:125 -e match:"BOGUS: invalid signal" \
+ env --default-signal=PIPE,BOGUS true
+ atf_check -s exit:125 -e match:"signal 9" \
+ env --ignore-signal=KILL true
+}
+
atf_init_test_cases()
{
atf_add_test_case basic
atf_add_test_case unset
atf_add_test_case empty
+ atf_add_test_case dash
atf_add_test_case true
atf_add_test_case false
atf_add_test_case altpath
atf_add_test_case equal
atf_add_test_case chdir
atf_add_test_case stdout
+ atf_add_test_case block_signal
+ atf_add_test_case default_signal
+ atf_add_test_case ignore_signal
+ atf_add_test_case signal_order
+ atf_add_test_case signal_split
+ atf_add_test_case signal_noutility
+ atf_add_test_case signal_invalid
}

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 27, 4:48 AM (19 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42035237
Default Alt Text
D62081.1795754884.diff (13 KB)

Event Timeline