From: Ganael Laplanche <ganael.laplanche@martymac.org>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Steffen Nurpmeso <steffen@sdaoden.eu>,
jilles@stack.nl, dash@vger.kernel.org
Subject: Re: Monitor mode handling (bug ?)
Date: Fri, 27 Jan 2023 12:46:35 +0100 [thread overview]
Message-ID: <2744018.j5hJsU0yqa@dmc12.centralesupelec.fr> (raw)
In-Reply-To: <Y9Olps/fwxaItE4j@gondor.apana.org.au>
On Friday, January 27, 2023 11:21:26 AM CET Herbert Xu wrote:
Hello Herbert,
> Could you please resubmit the patch inline? For some reason it's
> not getting picked up by patchwork and the quoted-printable format
> does not a bit iffy.
Here it is:
8<----
From dedaa3fa370ea9c4aeb1771b5568a7bef4065b04 Mon Sep 17 00:00:00 2001
Message-Id: <dedaa3fa370ea9c4aeb1771b5568a7bef4065b04.1674153195.git.steffen@sdaoden.eu>
From: Steffen Nurpmeso <steffen@sdaoden.eu>
Date: Thu, 19 Jan 2023 19:32:55 +0100
Subject: [PATCH] Allow enabling job control without a tty in non-interactive
mode..
This is a take-over of the FreeBSD bin/sh
commit cd60e2c67d52e1f957841af19128c7227880743a
Author: Jilles Tjoelker <jilles@FreeBSD.org>
AuthorDate: 2014-09-04 21:48:33 +0000
Commit: Jilles Tjoelker <jilles@FreeBSD.org>
CommitDate: 2014-09-04 21:48:33 +0000
sh: Allow enabling job control without a tty in non-interactive mode.
If no tty is available, 'set -m' is still useful to put jobs in their own
process groups.
---
src/jobs.c | 114 +++++++++++++++++++++++++++++++----------------------
1 file changed, 67 insertions(+), 47 deletions(-)
diff --git a/src/jobs.c b/src/jobs.c
index f3b9ffc285..db1f204045 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -125,7 +125,7 @@ STATIC int getstatus(struct job *);
#if JOBS
static int restartjob(struct job *, int);
-static void xtcsetpgrp(int, pid_t);
+static void xtcsetpgrp(pid_t);
#endif
STATIC void
@@ -174,70 +174,84 @@ set_curjob(struct job *jp, unsigned mode)
}
}
-#if JOBS
/*
* Turn job control on and off.
- *
- * Note: This code assumes that the third arg to ioctl is a character
- * pointer, which is true on Berkeley systems but not System V. Since
- * System V doesn't have job control yet, this isn't a problem now.
- *
- * Called with interrupts off.
*/
int jobctl;
+#if JOBS
+static void
+jobctl_notty(void)
+{
+ if (ttyfd >= 0) {
+ close(ttyfd);
+ ttyfd = -1;
+ }
+ if (!iflag) {
+ setsignal(SIGTSTP);
+ setsignal(SIGTTOU);
+ setsignal(SIGTTIN);
+ jobctl = 1;
+ return;
+ }
+ sh_warnx("can't access tty; job control turned off");
+ mflag = 0;
+}
+
void
setjobctl(int on)
{
- int fd;
- int pgrp;
+ int i;
if (on == jobctl || rootshell == 0)
return;
if (on) {
- int ofd;
- ofd = fd = sh_open(_PATH_TTY, O_RDWR, 1);
- if (fd < 0) {
- fd += 3;
- while (!isatty(fd))
- if (--fd < 0)
- goto out;
+ if (ttyfd != -1)
+ close(ttyfd);
+ if ((ttyfd = sh_open(_PATH_TTY, O_RDWR, 1)) < 0) {
+ i = 0;
+ while (i <= 2 && !isatty(i))
+ i++;
+ if (i > 2 ||
+ (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0) {
+ jobctl_notty();
+ return;
+ }
}
- fd = savefd(fd, ofd);
+ ttyfd = savefd(ttyfd, ttyfd);
do { /* while we are in the background */
- if ((pgrp = tcgetpgrp(fd)) < 0) {
-out:
- sh_warnx("can't access tty; job control turned off");
- mflag = on = 0;
- goto close;
+ initialpgrp = tcgetpgrp(ttyfd);
+ if (initialpgrp < 0) {
+ jobctl_notty();
+ return;
}
- if (pgrp == getpgrp())
- break;
- killpg(0, SIGTTIN);
- } while (1);
- initialpgrp = pgrp;
-
+ if (initialpgrp != getpgrp()) {
+ if (!iflag) {
+ initialpgrp = -1;
+ jobctl_notty();
+ return;
+ }
+ kill(0, SIGTTIN);
+ continue;
+ }
+ } while (0);
setsignal(SIGTSTP);
setsignal(SIGTTOU);
setsignal(SIGTTIN);
- pgrp = rootpid;
- setpgid(0, pgrp);
- xtcsetpgrp(fd, pgrp);
- } else {
- /* turning job control off */
- fd = ttyfd;
- pgrp = initialpgrp;
- xtcsetpgrp(fd, pgrp);
- setpgid(0, pgrp);
+ setpgid(0, rootpid);
+ tcsetpgrp(ttyfd, rootpid);
+ } else { /* turning job control off */
+ setpgid(0, initialpgrp);
+ if (ttyfd >= 0) {
+ tcsetpgrp(ttyfd, initialpgrp);
+ close(ttyfd);
+ ttyfd = -1;
+ }
setsignal(SIGTSTP);
setsignal(SIGTTOU);
setsignal(SIGTTIN);
-close:
- close(fd);
- fd = -1;
}
- ttyfd = fd;
jobctl = on;
}
#endif
@@ -394,7 +408,7 @@ restartjob(struct job *jp, int mode)
jp->state = JOBRUNNING;
pgid = jp->ps->pid;
if (mode == FORK_FG)
- xtcsetpgrp(ttyfd, pgid);
+ xtcsetpgrp(pgid);
killpg(pgid, SIGCONT);
ps = jp->ps;
i = jp->nprocs;
@@ -457,6 +471,9 @@ showjob(struct output *out, struct job *jp, int mode)
int indent;
char s[80];
+ if (!iflag)
+ return;
+
ps = jp->ps;
if (mode & SHOW_PGID) {
@@ -878,7 +895,7 @@ static void forkchild(struct job *jp, union node *n, int mode)
/* This can fail because we are doing it in the parent also */
(void)setpgid(0, pgrp);
if (mode == FORK_FG)
- xtcsetpgrp(ttyfd, pgrp);
+ xtcsetpgrp(pgrp);
setsignal(SIGTSTP);
setsignal(SIGTTOU);
} else
@@ -1018,7 +1035,7 @@ waitforjob(struct job *jp)
st = getstatus(jp);
#if JOBS
if (jp->jobctl) {
- xtcsetpgrp(ttyfd, rootpid);
+ xtcsetpgrp(rootpid);
/*
* This is truly gross.
* If we're doing job control, then we did a TIOCSPGRP which
@@ -1508,12 +1525,15 @@ showpipe(struct job *jp, struct output *out)
#if JOBS
STATIC void
-xtcsetpgrp(int fd, pid_t pgrp)
+xtcsetpgrp(pid_t pgrp)
{
int err;
+ if (ttyfd < 0)
+ return;
+
sigblockall(NULL);
- err = tcsetpgrp(fd, pgrp);
+ err = tcsetpgrp(ttyfd, pgrp);
sigclearmask();
if (err)
--
2.39.0
8<----
Thanks for your help!
Best regards,
--
Ganael LAPLANCHE <ganael.laplanche@martymac.org>
http://www.martymac.org | http://contribs.martymac.org
FreeBSD: martymac <martymac@FreeBSD.org>, http://www.FreeBSD.org
next prev parent reply other threads:[~2023-01-27 12:01 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-09 15:18 Monitor mode handling (bug ?) Ganael Laplanche
2023-01-10 21:28 ` Jilles Tjoelker
2023-01-11 16:25 ` Ganael Laplanche
2023-01-11 16:37 ` Ganael Laplanche
2023-01-11 17:31 ` Steffen Nurpmeso
2023-01-12 11:40 ` Ganael Laplanche
2023-01-12 15:00 ` Steffen Nurpmeso
2023-01-13 11:44 ` Ganael Laplanche
2023-01-13 21:03 ` Steffen Nurpmeso
2023-01-16 11:15 ` Ganael Laplanche
2023-01-16 19:41 ` Steffen Nurpmeso
2023-01-17 15:05 ` Ganael Laplanche
2023-01-17 21:04 ` Steffen Nurpmeso
2023-01-18 11:40 ` Ganael Laplanche
2023-01-18 12:03 ` Ganael Laplanche
2023-01-18 23:15 ` Steffen Nurpmeso
2023-01-19 11:38 ` Ganael Laplanche
2023-01-19 18:33 ` Steffen Nurpmeso
2023-01-20 11:40 ` Ganael Laplanche
2023-01-27 10:21 ` Herbert Xu
2023-01-27 11:46 ` Ganael Laplanche [this message]
2023-01-30 7:21 ` Herbert Xu
2023-01-30 11:37 ` Ganael Laplanche
2023-01-31 3:20 ` Herbert Xu
2023-01-31 11:46 ` Ganael Laplanche
2023-03-08 11:47 ` Ganael Laplanche
2023-03-08 17:00 ` Steffen Nurpmeso
2023-03-09 5:55 ` Herbert Xu
2023-03-09 6:40 ` Ganael Laplanche
2023-03-09 9:22 ` Herbert Xu
2023-03-09 9:55 ` Herbert Xu
2023-03-09 11:34 ` Ganael Laplanche
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2744018.j5hJsU0yqa@dmc12.centralesupelec.fr \
--to=ganael.laplanche@martymac.org \
--cc=dash@vger.kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=jilles@stack.nl \
--cc=steffen@sdaoden.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox