⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nojobs.c

📁 android-w.song.android.widget
💻 C
📖 第 1 页 / 共 2 页
字号:
/* nojobs.c - functions that make children, remember them, and handle their termination. *//* This file works under BSD, System V, minix, and Posix systems.  It does   not implement job control. *//* Copyright (C) 1987-2009 Free Software Foundation, Inc.   This file is part of GNU Bash, the Bourne Again SHell.   Bash is free software: you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation, either version 3 of the License, or   (at your option) any later version.   Bash is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.   You should have received a copy of the GNU General Public License   along with Bash.  If not, see <http://www.gnu.org/licenses/>.*/#include "config.h"#include "bashtypes.h"#include "filecntl.h"#if defined (HAVE_UNISTD_H)#  include <unistd.h>#endif#include <stdio.h>#include <signal.h>#include <errno.h>#if defined (BUFFERED_INPUT)#  include "input.h"#endif/* Need to include this up here for *_TTY_DRIVER definitions. */#include "shtty.h"#include "bashintl.h"#include "shell.h"#include "jobs.h"#include "execute_cmd.h"#include "builtins/builtext.h"	/* for wait_builtin */#define DEFAULT_CHILD_MAX 32#if defined (_POSIX_VERSION) || !defined (HAVE_KILLPG)#  define killpg(pg, sig)		kill(-(pg),(sig))#endif /* USG || _POSIX_VERSION */#if !defined (HAVE_SIGINTERRUPT) && !defined (HAVE_POSIX_SIGNALS)#  define siginterrupt(sig, code)#endif /* !HAVE_SIGINTERRUPT && !HAVE_POSIX_SIGNALS */#if defined (HAVE_WAITPID)#  define WAITPID(pid, statusp, options) waitpid (pid, statusp, options)#else#  define WAITPID(pid, statusp, options) wait (statusp)#endif /* !HAVE_WAITPID *//* Return the fd from which we are actually getting input. */#define input_tty() (shell_tty != -1) ? shell_tty : fileno (stderr)#if !defined (errno)extern int errno;#endif /* !errno */extern int interactive, interactive_shell, login_shell;extern int subshell_environment;extern int last_command_exit_value, last_command_exit_signal;extern int interrupt_immediately;extern sh_builtin_func_t *this_shell_builtin;#if defined (HAVE_POSIX_SIGNALS)extern sigset_t top_level_mask;#endifextern procenv_t wait_intr_buf;extern int wait_signal_received;pid_t last_made_pid = NO_PID;pid_t last_asynchronous_pid = NO_PID;/* Call this when you start making children. */int already_making_children = 0;/* The controlling tty for this shell. */int shell_tty = -1;/* If this is non-zero, $LINES and $COLUMNS are reset after every process   exits from get_tty_state(). */int check_window_size;/* STATUS and FLAGS are only valid if pid != NO_PID   STATUS is only valid if (flags & PROC_RUNNING) == 0 */struct proc_status {  pid_t pid;  int status;	/* Exit status of PID or 128 + fatal signal number */  int flags;};/* Values for proc_status.flags */#define PROC_RUNNING	0x01#define PROC_NOTIFIED	0x02#define PROC_ASYNC	0x04#define PROC_SIGNALED	0x10/* Return values from find_status_by_pid */#define PROC_BAD	 -1#define PROC_STILL_ALIVE -2static struct proc_status *pid_list = (struct proc_status *)NULL;static int pid_list_size;static int wait_sigint_received;static long child_max = -1L;static void alloc_pid_list __P((void));static int find_proc_slot __P((void));static int find_index_by_pid __P((pid_t));static int find_status_by_pid __P((pid_t));static int process_exit_status __P((WAIT));static int find_termsig_by_pid __P((pid_t));static int get_termsig __P((WAIT));static void set_pid_status __P((pid_t, WAIT));static void set_pid_flags __P((pid_t, int));static void unset_pid_flags __P((pid_t, int));static int get_pid_flags __P((pid_t));static void add_pid __P((pid_t, int));static void mark_dead_jobs_as_notified __P((int));static sighandler wait_sigint_handler __P((int));static char *j_strsignal __P((int));#if defined (HAVE_WAITPID)static void reap_zombie_children __P((void));#endif#if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)static int siginterrupt __P((int, int));#endifstatic void restore_sigint_handler __P((void));/* Allocate new, or grow existing PID_LIST. */static voidalloc_pid_list (){  register int i;  int old = pid_list_size;  pid_list_size += 10;  pid_list = (struct proc_status *)xrealloc (pid_list, pid_list_size * sizeof (struct proc_status));  /* None of the newly allocated slots have process id's yet. */  for (i = old; i < pid_list_size; i++)    pid_list[i].pid = NO_PID;}/* Return the offset within the PID_LIST array of an empty slot.  This can   create new slots if all of the existing slots are taken. */static intfind_proc_slot (){  register int i;  for (i = 0; i < pid_list_size; i++)    if (pid_list[i].pid == NO_PID)      return (i);  if (i == pid_list_size)    alloc_pid_list ();  return (i);}/* Return the offset within the PID_LIST array of a slot containing PID,   or the value NO_PID if the pid wasn't found. */static intfind_index_by_pid (pid)     pid_t pid;{  register int i;  for (i = 0; i < pid_list_size; i++)    if (pid_list[i].pid == pid)      return (i);  return (NO_PID);}/* Return the status of PID as looked up in the PID_LIST array.  A   return value of PROC_BAD indicates that PID wasn't found. */static intfind_status_by_pid (pid)     pid_t pid;{  int i;  i = find_index_by_pid (pid);  if (i == NO_PID)    return (PROC_BAD);  if (pid_list[i].flags & PROC_RUNNING)    return (PROC_STILL_ALIVE);  return (pid_list[i].status);}static intprocess_exit_status (status)     WAIT status;{  if (WIFSIGNALED (status))    return (128 + WTERMSIG (status));  else    return (WEXITSTATUS (status));}/* Return the status of PID as looked up in the PID_LIST array.  A   return value of PROC_BAD indicates that PID wasn't found. */static intfind_termsig_by_pid (pid)     pid_t pid;{  int i;  i = find_index_by_pid (pid);  if (i == NO_PID)    return (0);  if (pid_list[i].flags & PROC_RUNNING)    return (0);  return (get_termsig ((WAIT)pid_list[i].status));}/* Set LAST_COMMAND_EXIT_SIGNAL depending on STATUS.  If STATUS is -1, look   up PID in the pid array and set LAST_COMMAND_EXIT_SIGNAL appropriately   depending on its flags and exit status. */static intget_termsig (status)     WAIT status;{  if (WIFSTOPPED (status) == 0 && WIFSIGNALED (status))    return (WTERMSIG (status));  else    return (0);}/* Give PID the status value STATUS in the PID_LIST array. */static voidset_pid_status (pid, status)     pid_t pid;     WAIT status;{  int slot;#if defined (COPROCESS_SUPPORT)  coproc_pidchk (pid, status);#endif  slot = find_index_by_pid (pid);  if (slot == NO_PID)    return;  pid_list[slot].status = process_exit_status (status);  pid_list[slot].flags &= ~PROC_RUNNING;  if (WIFSIGNALED (status))    pid_list[slot].flags |= PROC_SIGNALED;  /* If it's not a background process, mark it as notified so it gets     cleaned up. */  if ((pid_list[slot].flags & PROC_ASYNC) == 0)    pid_list[slot].flags |= PROC_NOTIFIED;}/* Give PID the flags FLAGS in the PID_LIST array. */static voidset_pid_flags (pid, flags)     pid_t pid;     int flags;{  int slot;  slot = find_index_by_pid (pid);  if (slot == NO_PID)    return;  pid_list[slot].flags |= flags;}/* Unset FLAGS for PID in the pid list */static voidunset_pid_flags (pid, flags)     pid_t pid;     int flags;{  int slot;  slot = find_index_by_pid (pid);  if (slot == NO_PID)    return;  pid_list[slot].flags &= ~flags;}/* Return the flags corresponding to PID in the PID_LIST array. */static intget_pid_flags (pid)     pid_t pid;{  int slot;  slot = find_index_by_pid (pid);  if (slot == NO_PID)    return 0;  return (pid_list[slot].flags);}static voidadd_pid (pid, async)     pid_t pid;     int async;{  int slot;  slot = find_proc_slot ();  pid_list[slot].pid = pid;  pid_list[slot].status = -1;  pid_list[slot].flags = PROC_RUNNING;  if (async)    pid_list[slot].flags |= PROC_ASYNC;}static voidmark_dead_jobs_as_notified (force)     int force;{  register int i, ndead;  /* first, count the number of non-running async jobs if FORCE == 0 */  for (i = ndead = 0; force == 0 && i < pid_list_size; i++)    {      if (pid_list[i].pid == NO_PID)	continue;      if (((pid_list[i].flags & PROC_RUNNING) == 0) &&	   (pid_list[i].flags & PROC_ASYNC))	ndead++;    }  if (child_max < 0)    child_max = getmaxchild ();  if (child_max < 0)    child_max = DEFAULT_CHILD_MAX;  if (force == 0 && ndead <= child_max)    return;  /* If FORCE == 0, we just mark as many non-running async jobs as notified     to bring us under the CHILD_MAX limit. */  for (i = 0; i < pid_list_size; i++)    {      if (pid_list[i].pid == NO_PID)	continue;      if (((pid_list[i].flags & PROC_RUNNING) == 0) &&	   pid_list[i].pid != last_asynchronous_pid)	{	  pid_list[i].flags |= PROC_NOTIFIED;	  if (force == 0 && (pid_list[i].flags & PROC_ASYNC) && --ndead <= child_max)	    break;	}    }}/* Remove all dead, notified jobs from the pid_list. */intcleanup_dead_jobs (){  register int i;#if defined (HAVE_WAITPID)  reap_zombie_children ();#endif  for (i = 0; i < pid_list_size; i++)    {      if ((pid_list[i].flags & PROC_RUNNING) == 0 &&	  (pid_list[i].flags & PROC_NOTIFIED))	pid_list[i].pid = NO_PID;    }#if defined (COPROCESS_SUPPORT)  coproc_reap ();#endif  return 0;}voidreap_dead_jobs (){  mark_dead_jobs_as_notified (0);  cleanup_dead_jobs ();}/* Initialize the job control mechanism, and set up the tty stuff. */initialize_job_control (force)     int force;{  shell_tty = fileno (stderr);  if (interactive)    get_tty_state ();}/* Setup this shell to handle C-C, etc. */voidinitialize_job_signals (){  set_signal_handler (SIGINT, sigint_sighandler);  /* If this is a login shell we don't wish to be disturbed by     stop signals. */  if (login_shell)    ignore_tty_job_signals ();}#if defined (HAVE_WAITPID)/* Collect the status of all zombie children so that their system   resources can be deallocated. */static voidreap_zombie_children (){#  if defined (WNOHANG)  pid_t pid;  WAIT status;  CHECK_TERMSIG;  while ((pid = waitpid (-1, (int *)&status, WNOHANG)) > 0)    set_pid_status (pid, status);#  endif /* WNOHANG */  CHECK_TERMSIG;}#endif /* WAITPID */#if !defined (HAVE_SIGINTERRUPT) && defined (HAVE_POSIX_SIGNALS)static intsiginterrupt (sig, flag)     int sig, flag;{  struct sigaction act;  sigaction (sig, (struct sigaction *)NULL, &act);  if (flag)    act.sa_flags &= ~SA_RESTART;  else    act.sa_flags |= SA_RESTART;  return (sigaction (sig, &act, (struct sigaction *)NULL));}#endif /* !HAVE_SIGINTERRUPT && HAVE_POSIX_SIGNALS *//* Fork, handling errors.  Returns the pid of the newly made child, or 0.   COMMAND is just for remembering the name of the command; we don't do   anything else with it.  ASYNC_P says what to do with the tty.  If   non-zero, then don't give it away. */pid_tmake_child (command, async_p)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -