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; }
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); }
/* * 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 */
/* * 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); }
ExtFunc void RestoreSignals(MySigSet *saved, MySigSet *set) { #ifdef HAS_SIGPROCMASK sigprocmask(SIG_SETMASK, set, saved); #else if (saved) *saved = sigsetmask(*set); else sigsetmask(*set); #endif }
void sig_unblock(int sig) { #ifdef HASSIGPROCMASK sigset_t ss; sigemptyset(&ss); sigaddset(&ss,sig); sigprocmask(SIG_UNBLOCK,&ss,(sigset_t *) 0); #else sigsetmask(sigsetmask(~0) & ~(1 << (sig - 1))); #endif }
void cleanup(int sig) { #ifdef _CRAY static int incleanup = 0; int t; int child_status; /* status of child process as returned by waitpid */ int flags = WNOHANG|WUNTRACED; /* * 1: Pick up the zombie, if we are being called * as the signal handler. * 2: If we are a nested cleanup(), return. * 3: Try to clean up TMPDIR. * 4: Fill in utmp with shutdown of process. * 5: Close down the network and pty connections. * 6: Finish up the TMPDIR cleanup, if needed. */ if (sig == SIGCHLD) { while (waitpid(-1, &child_status, flags) > 0) ; /* VOID */ /* Check if the child process was stopped * rather than exited. We want cleanup only if * the child has died. */ if (WIFSTOPPED(child_status)) { return; } } t = sigblock(sigmask(SIGCHLD)); if (incleanup) { sigsetmask(t); return; } incleanup = 1; sigsetmask(t); t = cleantmp(&wtmp); setutent(); /* just to make sure */ #endif /* CRAY */ rmut(line); close(ourpty); shutdown(net, 2); #ifdef _CRAY if (t == 0) cleantmp(&wtmp); #endif /* CRAY */ exit(1); }
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; }
RETSIGTYPE restart_doit(int a) { char * name = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE); snmp_shutdown(name); /* This signal handler may run with SIGALARM blocked. * Since the signal mask is preserved accross execv(), we must * make sure that SIGALARM is unblocked prior of execv'ing. * Otherwise SIGALARM will be ignored in the next incarnation * of snmpd, because the signal is blocked. And thus, the * restart doesn't work anymore. */ #if HAVE_SIGBLOCK sigsetmask(0); #endif /* * do the exec */ #if HAVE_EXECV execv(argvrestartname, argvrestartp); setPerrorstatus(argvrestartname); #endif }
/* 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); } }
static void async_stop_sig (gdb_client_data arg) { char *prompt = get_prompt (); #if STOP_SIGNAL == SIGTSTP signal (SIGTSTP, SIG_DFL); #if HAVE_SIGPROCMASK { sigset_t zero; sigemptyset (&zero); sigprocmask (SIG_SETMASK, &zero, 0); } #elif HAVE_SIGSETMASK sigsetmask (0); #endif raise (SIGTSTP); signal (SIGTSTP, handle_stop_sig); #else signal (STOP_SIGNAL, handle_stop_sig); #endif printf_unfiltered ("%s", prompt); gdb_flush (gdb_stdout); /* Forget about any previous command -- null line now will do nothing. */ dont_repeat (); }
abort() { sigblock(~0); signal(SIGILL, SIG_DFL); sigsetmask(~sigmask(SIGILL)); kill(getpid(), SIGILL); }
f77_abort( err_val, act_core ) { char first_char, *env_var; int core_dump; env_var = getenv("f77_dump_flag"); first_char = (env_var == NULL) ? 0 : *env_var; signal(SIGILL, SIG_DFL); sigsetmask(0); /* don't block */ /* see if we want a core dump: first line checks for signals like hangup - don't dump then. second line checks if -lg specified to ld (e.g. by saying -g to f77) and checks the f77_dump_flag var. */ core_dump = ((nargs() != 2) || act_core) && ( (_lg_flag && (first_char != 'n')) || first_char == 'y'); if( !core_dump ) fprintf(units[STDERR].ufd,"*** Execution terminated\n"); f_exit(); _cleanup(); if( nargs() ) errno = err_val; else errno = -2; /* prior value will be meaningless, so set it to undefined value */ if( core_dump ) abort(); else exit( errno ); }
void plog(int lvl, 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); sigprocmask(SIG_BLOCK, &chld, &old); #else /* not HAVE_SIGACTION */ mask = sigblock(sigmask(SIGCHLD)); #endif /* not HAVE_SIGACTION */ if (!logfp) logfp = stderr; /* initialize before possible first use */ va_start(ap, fmt); real_plog(lvl, fmt, ap); va_end(ap); #ifdef HAVE_SIGACTION sigprocmask(SIG_SETMASK, &old, NULL); #else /* not HAVE_SIGACTION */ sigsetmask(mask); #endif /* not HAVE_SIGACTION */ }
int sigprocmask (int mode, sigset_t * mask, sigset_t * omask) { sigset_t current = sigsetmask(0); if (omask) *omask = current; if (mode==SIG_BLOCK) current |= *mask; else if (mode==SIG_UNBLOCK) current &= ~*mask; else if (mode==SIG_SETMASK) current = *mask; sigsetmask(current); return 0; }
/* ========================================================================== * machdep_sys_sigprocmask() * This isn't a real implementation; we can make the assumption that the * pthreads library is not using oset, and that it is always blocking or * unblocking all signals at once. */ int machdep_sys_sigprocmask(int how, const sigset_t *set, sigset_t *oset) { switch(how) { case SIG_BLOCK: sigblock(*set); break; case SIG_UNBLOCK: sigsetmask(~*set); break; case SIG_SETMASK: sigsetmask(*set); break; default: return -EINVAL; } return(OK); }
/* * 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; }
/* static int forkX(){ */ static int forkX(PCStr(what)) { int nmask,smask; int pmask,tmask; int pid; vfuncp opsig = 0; vfuncp otsig = 0; forker_pid = getpid(); nmask = sigmask(SIGPIPE)|sigmask(SIGTERM)|sigmask(SIGINT); nmask |= sigmask(SIGHUP); pmask = proc_sigblock(nmask); /* sigblock can be for a thread only */ tmask = sigblock(nmask); /* smask = sigblock(nmask); */ opsig = signal(SIGPIPE,sigPIPEi); pid = fork(); signal(SIGPIPE,opsig); /* sigsetmask(smask); */ if( pid ) { sigsetmask(tmask); proc_sigsetmask(pmask); } else { /* * 9.9.4 MTSS clear inherited env. first not to let signals * be processed under the thread env. of the parent that is * not to be inherited. */ MyPID = my_pid = getpid(); on_fork(MyPID); execCloseOnFork(what); /* * try to capture pending SIGTERM and exit */ otsig = signal(SIGTERM,sigTERMx); sigsetmask(tmask); proc_sigsetmask(pmask); usleep(1); signal(SIGTERM,otsig); } return pid; }
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; }
/* 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); }
static void restore_sigs (masktype *oldmask) { #ifdef POSIX_SIGNALS sigprocmask(SIG_SETMASK, oldmask, (sigset_t*)0); #else sigsetmask(*oldmask); #endif /* POSIX_SIGNALS */ }
/* * ****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); } } }
void Sig2HandlerFunction(int) { restore_modex = TRUE; sigaction(SIGUSR1, &sig1handler, NULL); sigaction(SIGUSR2, &oldsig2handler, NULL); sigsetmask (0); raise(SIGUSR2); }
void sig_blocknone(void) { #ifdef HASSIGPROCMASK sigset_t ss; sigemptyset(&ss); sigprocmask(SIG_SETMASK,&ss,(sigset_t *) 0); #else sigsetmask(0); #endif }
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 setcontext(const ucontext_t *uctx) { mcontext_t mctx = (mcontext_t)&uctx->__mcontext_data; ucontext_t *_uctx = (ucontext_t *)uctx; if (mctx != _uctx->uc_mcontext) _uctx->uc_mcontext = mctx; sigsetmask(uctx->uc_sigmask); return _setcontext(mctx); }
/* * We have just gotten a susp. Suspend and prepare to resume. */ void onsusp(int unused __unused) { signal(SIGTSTP, SIG_DFL); sigsetmask(0); kill(0, SIGTSTP); signal(SIGTSTP, onsusp); if (!mailing) longjmp(tstpbuf, 0); }
void Sig1HandlerFunction(int) { extern void StopTimer (); StopTimer (); sigaction(SIGUSR2, &sig2handler, NULL); sigaction(SIGUSR1, &oldsig1handler, NULL); sigsetmask (0); raise(SIGUSR1); }
void sig_all_unblock(void) { #ifdef HASSIGPROCMASK sigset_t set; sigemptyset(&set); sigprocmask(SIG_UNBLOCK, &set, 0); #else sigsetmask(0); #endif }
void unblock_io_and_alarm(void) { int mask, omask; mask = sigmask(SIGIO) | sigmask(SIGALRM); omask = sigblock(0); omask &= ~mask; (void) sigsetmask(omask); }