void dplog(const char *fmt, ...) { #ifdef HAVE_SIGACTION sigset_t old, chld; #else /* not HAVE_SIGACTION */ int mask; #endif /* not HAVE_SIGACTION */ va_list ap; #ifdef HAVE_SIGACTION sigemptyset(&chld); sigaddset(&chld, SIGCHLD); #else /* not HAVE_SIGACTION */ mask = sigblock(sigmask(SIGCHLD)); #endif /* not HAVE_SIGACTION */ sigprocmask(SIG_BLOCK, &chld, &old); if (!logfp) logfp = stderr; /* initialize before possible first use */ va_start(ap, fmt); real_plog(XLOG_DEBUG, fmt, ap); va_end(ap); #ifdef HAVE_SIGACTION sigprocmask(SIG_SETMASK, &old, NULL); #else /* not HAVE_SIGACTION */ mask = sigblock(sigmask(SIGCHLD)); #endif /* not HAVE_SIGACTION */ }
int readTimeoutBlocked(int fd,PVStr(buf),int siz,int timeout) { int omask,nmask; int rcc; int serno; int serrno; alertVStr(buf,siz); rcc = -1; omask = sigblock(sigmask(SIGCHLD)); serno = ++readSERNO; if( 0 < PollIn(fd,timeout) ){ errno = 0; rcc = read(fd,(char*)buf,QVSSize(buf,siz)); serrno = errno; if( rcc != siz ){ sv1log("##ERROR: readTimeoutB insufficient read %d/%d (%d)%X\n", rcc,siz,errno,sigblock(0)); } errno = serrno; } if( serno != readSERNO ){ sv1log("##ERROR: readTimeoutB broken %d/%d (%d)%X\n", serno,readSERNO,errno,sigblock(0)); sleep(10); } nmask = sigsetmask(omask); return rcc; }
/* * ****TIPOUT TIPOUT**** */ void tipout(void) { char buf[BUFSIZ]; char *cp; int cnt; int omask; signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); signal(SIGEMT, intEMT); /* attention from TIPIN */ signal(SIGTERM, intTERM); /* time to go signal */ signal(SIGIOT, intIOT); /* scripting going on signal */ signal(SIGHUP, intTERM); /* for dial-ups */ signal(SIGSYS, intSYS); /* beautify toggle */ (void) setjmp(sigbuf); for (omask = 0;; sigsetmask(omask)) { cnt = read(FD, buf, BUFSIZ); if (cnt <= 0) { /* lost carrier */ if (cnt < 0 && errno == EIO) { sigblock(sigmask(SIGTERM)); intTERM(0); /*NOTREACHED*/ } else if (cnt == 0 && errno == ENOENT) { if (getppid() != 1) kill(getppid(),SIGUSR1); sigblock(sigmask(SIGTERM)); intTERM(0); /*NOTREACHED*/ } else if (cnt < 0) { if (getppid() != 1) kill(getppid(),SIGUSR1); sigblock(sigmask(SIGTERM)); intTERM(0); /*NOTREACHED*/ } continue; } #define ALLSIGS sigmask(SIGEMT)|sigmask(SIGTERM)|sigmask(SIGIOT)|sigmask(SIGSYS) omask = sigblock(ALLSIGS); for (cp = buf; cp < buf + cnt; cp++) *cp &= 0177; if (write(1, buf, cnt) < 0) exit(1); if (boolean(value(SCRIPT)) && fscript != NULL) { if (!boolean(value(BEAUTIFY))) { fwrite(buf, 1, cnt, fscript); continue; } for (cp = buf; cp < buf + cnt; cp++) if ((*cp >= ' ' && *cp <= '~') || any(*cp, value(EXCEPTIONS))) putc(*cp, fscript); } } }
ExtFunc void BlockSignals(MySigSet *saved, ...) { MySigSet set; va_list args; int sig; va_start(args, saved); #ifdef HAS_SIGPROCMASK sigemptyset(&set); #else set = 0; #endif while ((sig = va_arg(args, int))) { #ifdef HAS_SIGPROCMASK sigaddset(&set, sig); #else sig |= sigmask(sig); #endif } #ifdef HAS_SIGPROCMASK sigprocmask(SIG_BLOCK, &set, saved); #else *saved = sigblock(set); #endif va_end(args); }
/* * exec_shell -- * Execute a shell command using passed use_shell and use_name * arguments. */ static int exec_shell(const char *command, const char *use_shell, const char *use_name) { pid_t pid; int omask, pstat; sig_t intsave, quitsave; if (!command) /* just checking... */ return(1); omask = sigblock(sigmask(SIGCHLD)); switch(pid = vfork()) { case -1: /* error */ err(1, "vfork"); case 0: /* child */ (void)sigsetmask(omask); execl(use_shell, use_name, "-c", command, (char *)NULL); warn("%s", use_shell); _exit(1); } intsave = signal(SIGINT, SIG_IGN); quitsave = signal(SIGQUIT, SIG_IGN); pid = waitpid(pid, &pstat, 0); (void)sigsetmask(omask); (void)signal(SIGINT, intsave); (void)signal(SIGQUIT, quitsave); return(pid == -1 ? -1 : pstat); }
/* * call-seq: * Signal.trap( signal, proc ) => obj * Signal.trap( signal ) {| | block } => obj * * Specifies the handling of signals. The first parameter is a signal * name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a * signal number. The characters ``SIG'' may be omitted from the * signal name. The command or block specifies code to be run when the * signal is raised. If the command is the string ``IGNORE'' or * ``SIG_IGN'', the signal will be ignored. If the command is * ``DEFAULT'' or ``SIG_DFL'', the operating system's default handler * will be invoked. If the command is ``EXIT'', the script will be * terminated by the signal. Otherwise, the given command or block * will be run. * The special signal name ``EXIT'' or signal number zero will be * invoked just prior to program termination. * trap returns the previous handler for the given signal. * * Signal.trap(0, proc { puts "Terminating: #{$$}" }) * Signal.trap("CLD") { puts "Child died" } * fork && Process.wait * * produces: * Terminating: 27461 * Child died * Terminating: 27460 */ static VALUE sig_trap(int argc, VALUE *argv) { struct trap_arg arg; rb_secure(2); if (argc == 0 || argc > 2) { rb_raise(rb_eArgError, "wrong number of arguments -- trap(sig, cmd)/trap(sig){...}"); } arg.sig = argv[0]; if (argc == 1) { arg.cmd = rb_block_proc(); } else if (argc == 2) { arg.cmd = argv[1]; } if (OBJ_TAINTED(arg.cmd)) { rb_raise(rb_eSecurityError, "Insecure: tainted signal trap"); } #ifndef _WIN32 /* disable interrupt */ # ifdef HAVE_SIGPROCMASK sigfillset(&arg.mask); sigprocmask(SIG_BLOCK, &arg.mask, &arg.mask); # else arg.mask = sigblock(~0); # endif return rb_ensure(trap, (VALUE)&arg, trap_ensure, (VALUE)&arg); #else return trap(&arg); #endif }
int vsystem(const char *fmt, ...) { va_list args; int pstat; pid_t pid; int omask; sig_t intsave, quitsave; char *cmd; int i; struct stat sb; cmd = (char *)alloca(FILENAME_MAX); cmd[0] = '\0'; va_start(args, fmt); vsnprintf(cmd, FILENAME_MAX, fmt, args); va_end(args); omask = sigblock(sigmask(SIGCHLD)); if (Fake) { msgDebug("vsystem: Faked execution of `%s'\n", cmd); return 0; } if (isDebug()) msgDebug("Executing command `%s'\n", cmd); pid = fork(); if (pid == -1) { (void)sigsetmask(omask); i = 127; } else if (!pid) { /* Junior */ (void)sigsetmask(omask); if (DebugFD != -1) { dup2(DebugFD, 0); dup2(DebugFD, 1); dup2(DebugFD, 2); } else { close(1); open("/dev/null", O_WRONLY); dup2(1, 2); } if (stat("/stand/sh", &sb) == 0) execl("/stand/sh", "/stand/sh", "-c", cmd, (char *)NULL); else execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL); exit(1); } else { intsave = signal(SIGINT, SIG_IGN); quitsave = signal(SIGQUIT, SIG_IGN); pid = waitpid(pid, &pstat, 0); (void)sigsetmask(omask); (void)signal(SIGINT, intsave); (void)signal(SIGQUIT, quitsave); i = (pid == -1) ? -1 : WEXITSTATUS(pstat); if (isDebug()) msgDebug("Command `%s' returns status of %d\n", cmd, i); } return i; }
abort() { sigblock(~0); signal(SIGILL, SIG_DFL); sigsetmask(~sigmask(SIGILL)); kill(getpid(), SIGILL); }
static void vox_close (struct sound_device *sd) { if (sd->fd >= 0) { /* On GNU/Linux, it seems that the device driver doesn't like to be interrupted by a signal. Block the ones we know to cause troubles. */ #ifdef SIGIO sigblock (sigmask (SIGIO)); #endif turn_on_atimers (0); /* Flush sound data, and reset the device. */ ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL); turn_on_atimers (1); #ifdef SIGIO sigunblock (sigmask (SIGIO)); #endif /* Close the device. */ emacs_close (sd->fd); sd->fd = -1; } }
void cpu_mask_all_signals(void) { sigblock(sigmask(SIGALRM)|sigmask(SIGIO)|sigmask(SIGQUIT)| sigmask(SIGUSR1)|sigmask(SIGTERM)|sigmask(SIGWINCH)| sigmask(SIGUSR2)); }
/* * The argument is the new rate in old_tick units. */ int SetClockRate( long rate ) { long mask; if (lseek(kmem, (off_t)nl[0].n_value, 0) == -1L) return (-1); mask = sigblock(sigmask(SIGALRM)); if (write(kmem, (caddr_t)&rate, sizeof(rate)) != sizeof(rate)) { sigsetmask(mask); return (-1); } sigsetmask(mask); if (rate != default_rate) { if (verbose > 3) { printf("adjtimed: clock rate (%lu) %ldus/s\n", rate, (rate - default_rate) * tick_rate); } if (sysdebug > 3) { msyslog(LOG_INFO, "clock rate (%lu) %ldus/s", rate, (rate - default_rate) * tick_rate); } } return (0); } /* SetClockRate */
/* ARGSUSED */ static void writeroob(int signum) { int mask; if (!dosigwinch) { /* * Start tracking window size. It doesn't matter which * order the next two are in, because we'll be unconditionally * sending a size notification in a moment. */ (void) sigset(SIGWINCH, sigwinch); dosigwinch = B_TRUE; /* * It would be bad if a SIGWINCH came in between the ioctl * and sending the data. It could result in the SIGWINCH * handler sending a good message, and then us sending an * outdated or inconsistent message. * * Instead, if the change is made before the * ioctl, the sigwinch handler will send a size message * and we'll send another, identical, one. If the change * is made after the ioctl, we'll send a message with the * old value, and then the sigwinch handler will send * a revised, correct one. */ mask = sigblock(sigmask(SIGWINCH)); if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize) == 0) sendwindow(); sigsetmask(mask); } }
/* Cause SIGINT to not be delivered until the corresponding call to release_sigint(). */ static void block_sigint () { if (sigint_blocked) return; #if defined (__EMX__) sigemptyset (&sigint_set); sigemptyset (&sigint_oset); sigaddset (&sigint_set, SIGINT); sigaddset (&sigint_set, SIGBREAK); sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset); #else #if defined (HAVE_POSIX_SIGNALS) sigemptyset (&sigint_set); sigemptyset (&sigint_oset); sigaddset (&sigint_set, SIGINT); sigprocmask (SIG_BLOCK, &sigint_set, &sigint_oset); #else /* !HAVE_POSIX_SIGNALS */ # if defined (HAVE_BSD_SIGNALS) sigint_oldmask = sigblock (sigmask (SIGINT)); # else /* !HAVE_BSD_SIGNALS */ # if defined (HAVE_USG_SIGHOLD) sighold (SIGINT); # endif /* HAVE_USG_SIGHOLD */ # endif /* !HAVE_BSD_SIGNALS */ #endif /* !HAVE_POSIX_SIGNALS */ #endif sigint_blocked = 1; }
int editit(void) { int pid, xpid; int status, omask; const char *ed; omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP)); while ((pid = fork()) < 0) { if (errno == EPROCLIM) { warnx("you have too many processes"); return(0); } if (errno != EAGAIN) { warn("fork"); return(0); } sleep(1); } if (pid == 0) { sigsetmask(omask); setgid(getgid()); setuid(getuid()); if ((ed = getenv("EDITOR")) == NULL) ed = DEFEDITOR; execlp(ed, ed, tmpfil, NULL); err(1, "%s", ed); } while ((xpid = wait(&status)) >= 0) if (xpid == pid) break; sigsetmask(omask); return(!status); }
void suspend (int n) { + sigset_t set; + restoreterm(); signal(SIGTSTP, SIG_DFL); - sigsetmask(sigblock(0) &~ mask(SIGTSTP)); + sigaddset(&set, SIGTSTP); + sigprocmask(SIG_UNBLOCK, &set, NULL); kill(0, SIGTSTP); - sigblock(mask(SIGTSTP)); + sigprocmask(SIG_BLOCK, &set, NULL); signal(SIGTSTP, suspend); icbterm(); continued = 1;
void testValues() { f = 2; sigblock(anyint()); //@ assert f == 2; //@ assert vacuous: \false; }
void __ostimer_blockall(void) { #ifndef NO_POSIX_SIGS (void) sigprocmask( SIG_BLOCK, &timer_block_mask, SIGSET_NULL ) ; #else (void) sigblock( timer_block_mask ) ; #endif }
/* * Check that we are not burning resources */ static void checkup(void) { static int max_fd = 0; static char *max_mem = 0; int next_fd = dup(0); caddr_t next_mem = sbrk(0); close(next_fd); if (max_fd < next_fd) { dlog("%d new fds allocated; total is %d", next_fd - max_fd, next_fd); max_fd = next_fd; } if (max_mem < next_mem) { #ifdef HAVE_GETPAGESIZE dlog("%#lx bytes of memory allocated; total is %#lx (%ld pages)", (long) (next_mem - max_mem), (unsigned long) next_mem, ((long) next_mem + getpagesize() - 1) / (long) getpagesize()); #else /* not HAVE_GETPAGESIZE */ dlog("%#lx bytes of memory allocated; total is %#lx", (long) (next_mem - max_mem), (unsigned long) next_mem); #endif /* not HAVE_GETPAGESIZE */ max_mem = next_mem; } } #else /* not DEBUG */ #define checkup() #endif /* not DEBUG */ static int #ifdef HAVE_SIGACTION do_select(sigset_t smask, int fds, fd_set *fdp, struct timeval *tvp) #else /* not HAVE_SIGACTION */ do_select(int smask, int fds, fd_set *fdp, struct timeval *tvp) #endif /* not HAVE_SIGACTION */ { int sig; int nsel; if ((sig = setjmp(select_intr))) { select_intr_valid = 0; /* Got a signal */ switch (sig) { case SIGINT: case SIGTERM: amd_state = Finishing; reschedule_timeout_mp(); break; } nsel = -1; errno = EINTR; } else { select_intr_valid = 1; /* * Invalidate the current clock value */ clock_valid = 0; /* * Allow interrupts. If a signal * occurs, then it will cause a longjmp * up above. */ #ifdef HAVE_SIGACTION sigprocmask(SIG_SETMASK, &smask, NULL); #else /* not HAVE_SIGACTION */ (void) sigsetmask(smask); #endif /* not HAVE_SIGACTION */ /* * Wait for input */ nsel = select(fds, fdp, (fd_set *) 0, (fd_set *) 0, tvp->tv_sec ? tvp : (struct timeval *) 0); } #ifdef HAVE_SIGACTION sigprocmask(SIG_BLOCK, &masked_sigs, NULL); #else /* not HAVE_SIGACTION */ (void) sigblock(MASKED_SIGS); #endif /* not HAVE_SIGACTION */ /* * Perhaps reload the cache? */ if (do_mapc_reload < clocktime()) { mapc_reload(); do_mapc_reload = clocktime() + gopt.map_reload_interval; } return nsel; }
void block_io_and_alarm(void) { int mask; mask = sigmask(SIGIO) | sigmask(SIGALRM); if (sigblock(mask)) msyslog(LOG_ERR, "block_io_and_alarm: sigblock() failed: %m"); }
int main(int argc, char *argv[]) { sigblock(sigmask(SIGPIPE)); /* something to make the mask interesting */ signal(SIGTSTP, my_handler); asm {brk 0} setjmp(my_jump); asm {brk 1} for (;;); }
/* trap a signal, unless it is being ignored. */ static void setsignal(int sig) { int omask = sigblock(sigmask(sig)); if (signal(sig, exit) == SIG_IGN) (void)signal(sig, SIG_IGN); (void)sigsetmask(omask); }
pid_t vfork_and_run(void (*fn)(void*) /*NORETURN*/, void *arg) { /* GNO's fork2 call will return immediately and allow the parent and * child processes to execute concurrently using the same memory * space. To prevent them stomping on each other, we want to get * behavior like a traditional vfork() implementation, where the * parent blocks until the child terminates or execs. * * Our approach is to check the process tables to make sure the * child has actually finished or exec'd. If not, we loop and try again. * We can't just rely on the fact that the child signaled us, because * it may still be running in libc's implementation of exec*. */ long oldmask; pid_t pid; kvmt *kvm_context; struct pentry *proc_entry; int done = 0; /* Isolate child process's environment from parent */ if (environPush() != 0) return -1; /* Block all signals for now */ oldmask = sigblock(-1); pid = fork2(fork_thunk, CHILD_STACKSIZE, 0, forked_child_name, (sizeof(fn) + sizeof(arg) + sizeof(oldmask) + 1) / 2, fn, arg, oldmask); if (pid < 0) goto ret; while (!done) { /* Wait for ~100 ms. If procsend worked, the child could send a * message with it to end the waiting earlier, but this isn't * possible in GNO 2.0.6 because procsend is broken. This isn't * too big an issue, since 100ms isn't very long to wait anyhow. */ procrecvtim(1); /* Check if the child is really dead or forked by inspecting * the kernel's process entry for it. */ kvm_context = kvm_open(); if (kvm_context == NULL) break; proc_entry = kvmgetproc(kvm_context, pid); if (proc_entry == NULL || (proc_entry->args != NULL && strcmp(forked_child_name, proc_entry->args + 8) != 0)) done = 1; kvm_close(kvm_context); } ret: sigsetmask(oldmask); environPop(); return pid; }
/* * handle stop and start signals * * @(#)tstp.c 5.6 (Berkeley) 3/3/91 */ void tstp() { # ifdef SIGTSTP SGTTY tty; #ifdef POSIX sigset_t mask, omask; #else int omask; #endif # ifdef DEBUG if (outf) fflush(outf); # endif tty = _tty; mvcur(0, COLS - 1, LINES - 1, 0); endwin(); fflush(stdout); /* reset signal handler so kill below stops us */ signal(SIGTSTP, SIG_DFL); #ifdef POSIX sigemptyset(&mask); sigaddset(&mask, SIGTSTP); sigprocmask(SIG_SETMASK, &mask, &omask); kill(0, SIGTSTP); sigprocmask(SIG_SETMASK, &omask, 0); #else #define mask(s) (1 << ((s)-1)) omask = sigsetmask(sigblock(0) &~ mask(SIGTSTP)); kill(0, SIGTSTP); sigblock(mask(SIGTSTP)); #endif signal(SIGTSTP, tstp); _tty = tty; #ifdef POSIX tcsetattr(_tty_ch, TCSADRAIN, &_tty); #else ioctl(_tty_ch, TIOCSETP, &_tty); #endif wrefresh(curscr); # endif SIGTSTP }
/* Hold a signal =========================================================== */ int sighold(int signal_Number) { /* Block a specific signal */ if (sigblock(sigmask(signal_Number)) == -1) { return -1; } return 0; }
void unblock_io_and_alarm(void) { int mask, omask; mask = sigmask(SIGIO) | sigmask(SIGALRM); omask = sigblock(0); omask &= ~mask; (void) sigsetmask(omask); }
void __ostimer_unblockall(void) { #ifndef NO_POSIX_SIGS (void) sigprocmask( SIG_UNBLOCK, &timer_block_mask, SIGSET_NULL ) ; #else int old_mask = sigblock( ~0 ) ; (void) sigsetmask( old_mask & ~timer_block_mask ) ; #endif }
int ftpd_pclose (FILE * iop) { struct file_pid *fpid = file_pids, *prev_fpid = 0; int status; #ifdef HAVE_SIGACTION sigset_t sigs, osigs; #else int omask; #endif pid_t pid; /* * pclose returns -1 if stream is not associated with a * `popened' command, or, if already `pclosed'. */ while (fpid && fpid->file != iop) { prev_fpid = fpid; fpid = fpid->next; } if (!fpid) return -1; if (prev_fpid) prev_fpid->next = fpid->next; else file_pids = fpid->next; fclose (iop); #ifdef HAVE_SIGACTION sigemptyset (&sigs); sigaddset (&sigs, SIGINT); sigaddset (&sigs, SIGQUIT); sigaddset (&sigs, SIGHUP); sigprocmask (SIG_BLOCK, &sigs, &osigs); #else omask = sigblock (sigmask (SIGINT) | sigmask (SIGQUIT) | sigmask (SIGHUP)); #endif while ((pid = waitpid (fpid->pid, &status, 0)) < 0 && errno == EINTR) continue; free (fpid); #ifdef HAVE_SIGACTION sigprocmask (SIG_SETMASK, &osigs, 0); #else sigsetmask (omask); #endif if (pid < 0) return (pid); if (WIFEXITED (status)) return (WEXITSTATUS (status)); return (1); }
/* * Remove a child that has died (exited) * from the list of active children */ static void removechild(CHILD *child) { CHILD *pc, *prevpc; debugmsg(DM_CALL, "removechild(%s, %d, %d) start", child->c_name, child->c_pid, child->c_readfd); /* * Find the child in the list */ for (pc = childlist, prevpc = NULL; pc != NULL; prevpc = pc, pc = pc->c_next) if (pc == child) break; if (pc == NULL) error("RemoveChild called with bad child %s %d %d", child->c_name, child->c_pid, child->c_readfd); else { /* * Remove the child */ #if defined(POSIX_SIGNALS) sigset_t set; sigemptyset(&set); sigaddset(&set, SIGCHLD); sigprocmask(SIG_BLOCK, &set, NULL); #else /* !POSIX_SIGNALS */ int oldmask; oldmask = sigblock(sigmask(SIGCHLD)); #endif /* POSIX_SIGNALS */ if (prevpc != NULL) prevpc->c_next = pc->c_next; else childlist = pc->c_next; #if defined(POSIX_SIGNALS) sigprocmask(SIG_UNBLOCK, &set, NULL); #else sigsetmask(oldmask); #endif /* POSIX_SIGNALS */ (void) free(child->c_name); --activechildren; (void) close(child->c_readfd); (void) free(pc); } debugmsg(DM_CALL, "removechild() end"); }
void sig_block(int sig) { #ifdef HASSIGPROCMASK sigset_t ss; sigemptyset(&ss); sigaddset(&ss,sig); sigprocmask(SIG_BLOCK,&ss,(sigset_t *) 0); #else sigblock(1 << (sig - 1)); #endif }
void wait_for_signal(void) { int mask, omask; mask = sigmask(SIGIO) | sigmask(SIGALRM); omask = sigblock(0); omask &= ~mask; if (sigpause(omask) && (errno != EINTR)) msyslog(LOG_ERR, "wait_for_signal: sigspause() failed: %m"); }