Skip to content

Commit dde72a5

Browse files
Unmask all signals in child, since we've no idea what the caller's done with their signal mask
1 parent 5f997ce commit dde72a5

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

shell.cpp

+42-2
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ void *shell_new(t_symbol *s, long ac, t_atom *av)
704704
// http://rachid.koucha.free.fr/tech_corner/pty_pdip.html
705705
// using posix_openpt()
706706

707+
#ifndef NSIG
708+
# define NSIG 32
709+
#endif
710+
707711
int shell_pipe_open(t_shell *x, t_fildes *masterfd_r, t_fildes *masterfd_w, char *cmd, char *argv[], t_procid *ppid, int merge_stderr)
708712
{
709713
#ifdef MAC_VERSION
@@ -712,7 +716,9 @@ int shell_pipe_open(t_shell *x, t_fildes *masterfd_r, t_fildes *masterfd_w, char
712716
char *slavedevice;
713717
int rc;
714718
char workingdir[MAX_PATH_CHARS] = "";
715-
719+
sigset_t oldmask, newmask;
720+
struct sigaction sig_action;
721+
716722
*ppid = 0;
717723

718724
if (masterfd == -1
@@ -740,15 +746,24 @@ int shell_pipe_open(t_shell *x, t_fildes *masterfd_r, t_fildes *masterfd_w, char
740746
}
741747
}
742748

749+
sigfillset(&newmask);
750+
if (pthread_sigmask(SIG_SETMASK, &newmask, &oldmask) < 0) {
751+
object_error((t_object *)x, "error setting sigmask: %s", strerror(errno));
752+
return 0;
753+
}
754+
743755
*ppid = vfork();
744756
if (*ppid < 0) {
745757
close(masterfd);
746758
close(slavefd);
747759
return 0; // error
748760
}
761+
749762
if (*ppid == 0) { // child
750763
struct termios orig_termios, new_termios;
751-
764+
765+
umask(0);
766+
752767
close(masterfd); // close the master
753768

754769
// Save the default parameters of the slave side of the PTY
@@ -770,6 +785,26 @@ int shell_pipe_open(t_shell *x, t_fildes *masterfd_r, t_fildes *masterfd_w, char
770785
// As the child is a session leader, set the controlling terminal to be the slave side of the PTY
771786
// (Mandatory for programs like the shell to make them manage correctly their outputs)
772787
ioctl(0, TIOCSCTTY, 1);
788+
789+
// reset signals
790+
sig_action.sa_handler = SIG_DFL;
791+
sig_action.sa_flags = 0;
792+
sigemptyset(&sig_action.sa_mask);
793+
794+
for (int i = 0 ; i < NSIG ; i++) {
795+
// Only possible errors are EFAULT or EINVAL
796+
// The former wont happen, the latter we
797+
// expect, so no need to check return value
798+
sigaction(i, &sig_action, NULL);
799+
}
800+
801+
// Unmask all signals in child, since we've no idea
802+
// what the caller's done with their signal mask
803+
if (pthread_sigmask(SIG_SETMASK, &oldmask, NULL) < 0) {
804+
object_error((t_object *)x, "error resetting sigmask (child): %s", strerror(errno));
805+
return 0;
806+
}
807+
773808
if (*workingdir) {
774809
chdir(workingdir);
775810
}
@@ -778,6 +813,11 @@ int shell_pipe_open(t_shell *x, t_fildes *masterfd_r, t_fildes *masterfd_w, char
778813
} else { // parent
779814
close(slavefd); // close the slave
780815
*masterfd_r = *masterfd_w = masterfd;
816+
817+
if (pthread_sigmask(SIG_SETMASK, &oldmask, NULL) < 0) {
818+
object_error((t_object *)x, "error resetting sigmask: %s", strerror(errno));
819+
return masterfd;
820+
}
781821
}
782822
return masterfd;
783823
#else

0 commit comments

Comments
 (0)