Skip to content

Commit

Permalink
Merge branch 'master' into 6.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Sep 27, 2020
2 parents f6b73e4 + 9141fa7 commit 3fabc84
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 36 deletions.
21 changes: 21 additions & 0 deletions c_sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,26 @@ c_exec(char **wp)
return 0;
}

static int
c_suspend(char **wp)
{
if (wp[1] != NULL) {
bi_errorf("too many arguments");
return 1;
}
if (Flag(FLOGIN)) {
/* Can't suspend an orphaned process group. */
pid_t parent = getppid();
if (getpgid(parent) == getpgid(0) ||
getsid(parent) != getsid(0)) {
bi_errorf("can't suspend a login shell");
return 1;
}
}
j_suspend();
return 0;
}

/* dummy function, special case in comexec() */
int
c_builtin(char **wp)
Expand Down Expand Up @@ -882,5 +902,6 @@ const struct builtin shbuiltins [] = {
{"ulimit", c_ulimit},
{"+umask", c_umask},
{"*=unset", c_unset},
{"suspend", c_suspend},
{NULL, NULL}
};
2 changes: 2 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

meson --buildtype=release --prefix=/usr build
DESTDIR=../dst ninja -C build install
./dst/usr/bin/ksh -c "kill -l"
[ -n "`./dst/usr/bin/ksh -c 'echo &' 2>&1 | grep 'internal error'`" ] && exit 1
[ "`./dst/usr/bin/ksh -c 'echo $((1337 * 2))'`" -ne 2674 ] && exit 1
[ "`./dst/usr/bin/ksh -c 'seq 1337 | sort -rn | head -n 1'`" -ne 1337 ] && exit 1
CC=clang meson --buildtype=release build-clang
Expand Down
51 changes: 51 additions & 0 deletions jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,57 @@ j_init(int mflagset)
tty_init(true);
}

/* suspend the shell */
void
j_suspend(void)
{
struct sigaction sa, osa;

/* Restore tty and pgrp. */
if (ttypgrp_ok) {
tcsetattr(tty_fd, TCSADRAIN, &tty_state);
if (restore_ttypgrp >= 0) {
if (tcsetpgrp(tty_fd, restore_ttypgrp) == -1) {
warningf(false, "%s: tcsetpgrp() failed: %s",
__func__, strerror(errno));
} else {
if (setpgid(0, restore_ttypgrp) == -1) {
warningf(false,
"%s: setpgid() failed: %s",
__func__, strerror(errno));
}
}
}
}

/* Suspend the shell. */
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &sa, &osa);
kill(0, SIGTSTP);

/* Back from suspend, reset signals, pgrp and tty. */
sigaction(SIGTSTP, &osa, NULL);
if (ttypgrp_ok) {
if (restore_ttypgrp >= 0) {
if (setpgid(0, kshpid) == -1) {
warningf(false, "%s: setpgid() failed: %s",
__func__, strerror(errno));
ttypgrp_ok = 0;
} else {
if (tcsetpgrp(tty_fd, kshpid) == -1) {
warningf(false,
"%s: tcsetpgrp() failed: %s",
__func__, strerror(errno));
ttypgrp_ok = 0;
}
}
}
tty_init(true);
}
}

/* job cleanup before shell exit */
void
j_exit(void)
Expand Down
1 change: 1 addition & 0 deletions sh.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ void coproc_cleanup(int);
struct temp *maketemp(Area *, Temp_type, struct temp **);
/* jobs.c */
void j_init(int);
void j_suspend(void);
void j_exit(void);
void j_change(void);
int exchild(struct op *, int, volatile int *, int);
Expand Down
2 changes: 1 addition & 1 deletion subprojects/lolibc
51 changes: 16 additions & 35 deletions trap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,28 @@

#include "sh.h"

Trap sigtraps[NSIG + 1] = {
{ SIGEXIT_, "EXIT", "Signal 0" },
{ 1 , "HUP", "Hangup" },
{ 2 , "INT", "Interrupt" },
{ 3 , "QUIT", "Quit" },
{ 4 , "ILL", "Illegal instruction" },
{ 5 , "TRAP", "Trace trap" },
{ 6 , "ABRT", "Abort" },
{ 7 , "BUS", "Bus error" },
{ 8 , "FPE", "Floating point exception" },
{ 9 , "KILL", "Killed" },
{ 10 , "USR1", "User defined signal 1" },
{ 11 , "SEGV", "Memory fault" },
{ 12 , "USR2", "User defined signal 2" },
{ 13 , "PIPE", "Broken pipe" },
{ 14 , "ALRM", "Alarm clock" },
{ 15 , "TERM", "Terminated" },
{ 16 , "STKFLT", "Stack fault" },
{ 17 , "CHLD", "Child exited" },
{ 18 , "CONT", "Continued" },
{ 19 , "STOP", "Stopped (signal)" },
{ 20 , "TSTP", "Stopped" },
{ 21 , "TTIN", "Stopped (tty input)" },
{ 22 , "TTOU", "Stopped (tty output)" },
{ 23 , "URG", "Urgent I/O condition" },
{ 24 , "XCPU", "CPU time limit exceeded" },
{ 25 , "XFSZ", "File size limit exceeded" },
{ 26 , "VTALRM", "Virtual timer expired" },
{ 27 , "PROF", "Profiling timer expired" },
{ 28 , "WINCH", "Window size change" },
{ 29 , "IO", "I/O possible" },
{ 30 , "PWR", "Power-fail/Restart" },
{ 31 , "UNUSED", "Unused" },
{ SIGERR_, "ERR", "Error handler" }
};
Trap sigtraps[NSIG + 1];

static struct sigaction Sigact_ign, Sigact_trap;

void
inittraps(void)
{
int i;

/* Populate sigtraps based on sys_signame and sys_siglist. */
for (i = 0; i <= NSIG; i++) {
sigtraps[i].signal = i;
if (i == SIGERR_) {
sigtraps[i].name = "ERR";
sigtraps[i].mess = "Error handler";
} else {
sigtraps[i].name = sys_signame[i];
sigtraps[i].mess = sys_siglist[i];
}
}
sigtraps[SIGEXIT_].name = "EXIT"; /* our name for signal 0 */

sigemptyset(&Sigact_ign.sa_mask);
Sigact_ign.sa_flags = 0; /* interruptible */
Sigact_ign.sa_handler = SIG_IGN;
Expand Down

0 comments on commit 3fabc84

Please sign in to comment.