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

📄 signal.c

📁 一个简单的操作系统minix的核心代码
💻 C
📖 第 1 页 / 共 2 页
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				src/mm/signal.c	 	 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

17800	/* This file handles signals, which are asynchronous events and are generally
17801	 * a messy and unpleasant business.  Signals can be generated by the KILL
17802	 * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
17803	 * In all cases control eventually passes to check_sig() to see which processes
17804	 * can be signaled.  The actual signaling is done by sig_proc().
17805	 *
17806	 * The entry points into this file are:
17807	 *   do_sigaction:   perform the SIGACTION system call
17808	 *   do_sigpending:  perform the SIGPENDING system call
17809	 *   do_sigprocmask: perform the SIGPROCMASK system call
17810	 *   do_sigreturn:   perform the SIGRETURN system call
17811	 *   do_sigsuspend:  perform the SIGSUSPEND system call
17812	 *   do_kill:   perform the KILL system call
17813	 *   do_ksig:   accept a signal originating in the kernel (e.g., SIGINT)
17814	 *   do_alarm:  perform the ALARM system call by calling set_alarm()
17815	 *   set_alarm: tell the clock task to start or stop a timer
17816	 *   do_pause:  perform the PAUSE system call
17817	 *   do_reboot: kill all processes, then reboot system
17818	 *   sig_proc:  interrupt or terminate a signaled process
17819	 *   check_sig: check which processes to signal with sig_proc()
17820	 */
17821	
17822	#include "mm.h"
17823	#include <sys/stat.h>
17824	#include <minix/callnr.h>
17825	#include <minix/com.h>
17826	#include <signal.h>
17827	#include <sys/sigcontext.h>
17828	#include <string.h>
17829	#include "mproc.h"
17830	#include "param.h"
17831	
17832	#define CORE_MODE       0777    /* mode to use on core image files */
17833	#define DUMPED          0200    /* bit set in status when core dumped */
17834	#define DUMP_SIZE       ((INT_MAX / BLOCK_SIZE) * BLOCK_SIZE)
17835	                                /* buffer size for core dumps */
17836	
17837	FORWARD _PROTOTYPE( void check_pending, (void)                          );
17838	FORWARD _PROTOTYPE( void dump_core, (struct mproc *rmp)                 );
17839	FORWARD _PROTOTYPE( void unpause, (int pro)                             );
17840	
17841	
17842	/*===========================================================================*
17843	 *                             do_sigaction                                  *
17844	 *===========================================================================*/
17845	PUBLIC int do_sigaction()
17846	{
17847	  int r;
17848	  struct sigaction svec;
17849	  struct sigaction *svp;
17850	
17851	  if (sig_nr == SIGKILL) return(OK);
17852	  if (sig_nr < 1 || sig_nr > _NSIG) return (EINVAL);
17853	  svp = &mp->mp_sigact[sig_nr];
17854	  if ((struct sigaction *) sig_osa != (struct sigaction *) NULL) {
17855	        r = sys_copy(MM_PROC_NR,D, (phys_bytes) svp,
17856	                who, D, (phys_bytes) sig_osa, (phys_bytes) sizeof(svec));
17857	        if (r != OK) return(r);
17858	  }
17859	
17860	  if ((struct sigaction *) sig_nsa == (struct sigaction *) NULL) return(OK);
17861	
17862	  /* Read in the sigaction structure. */
17863	  r = sys_copy(who, D, (phys_bytes) sig_nsa,
17864	                MM_PROC_NR, D, (phys_bytes) &svec, (phys_bytes) sizeof(svec));
17865	  if (r != OK) return(r);
17866	
17867	  if (svec.sa_handler == SIG_IGN) {
17868	        sigaddset(&mp->mp_ignore, sig_nr);
17869	        sigdelset(&mp->mp_sigpending, sig_nr);
17870	        sigdelset(&mp->mp_catch, sig_nr);
17871	  } else {
17872	        sigdelset(&mp->mp_ignore, sig_nr);
17873	        if (svec.sa_handler == SIG_DFL)
17874	                sigdelset(&mp->mp_catch, sig_nr);
17875	        else
17876	                sigaddset(&mp->mp_catch, sig_nr);
17877	  }
17878	  mp->mp_sigact[sig_nr].sa_handler = svec.sa_handler;
17879	  sigdelset(&svec.sa_mask, SIGKILL);
17880	  mp->mp_sigact[sig_nr].sa_mask = svec.sa_mask;
17881	  mp->mp_sigact[sig_nr].sa_flags = svec.sa_flags;
17882	  mp->mp_sigreturn = (vir_bytes) sig_ret;
17883	  return(OK);
17884	}
	
17886	/*===========================================================================*
17887	 *                            do_sigpending                                  *
17888	 *===========================================================================*/
17889	PUBLIC int do_sigpending()
17890	{
17891	  ret_mask = (long) mp->mp_sigpending;
17892	  return OK;
17893	}
	
17895	/*===========================================================================*
17896	 *                            do_sigprocmask                                 *
17897	 *===========================================================================*/
17898	PUBLIC int do_sigprocmask()
17899	{
17900	/* Note that the library interface passes the actual mask in sigmask_set,
17901	 * not a pointer to the mask, in order to save a sys_copy.  Similarly,
17902	 * the old mask is placed in the return message which the library
17903	 * interface copies (if requested) to the user specified address.
17904	 *
17905	 * The library interface must set SIG_INQUIRE if the 'act' argument
17906	 * is NULL.
17907	 */
17908	
17909	  int i;
17910	
17911	  ret_mask = (long) mp->mp_sigmask;
17912	
17913	  switch (sig_how) {
17914	      case SIG_BLOCK:
17915	        sigdelset((sigset_t *)&sig_set, SIGKILL);
17916	        for (i = 1; i < _NSIG; i++) {
17917	                if (sigismember((sigset_t *)&sig_set, i))
17918	                        sigaddset(&mp->mp_sigmask, i);
17919	        }
17920	        break;
17921	
17922	      case SIG_UNBLOCK:
17923	        for (i = 1; i < _NSIG; i++) {
17924	                if (sigismember((sigset_t *)&sig_set, i))
17925	                        sigdelset(&mp->mp_sigmask, i);
17926	        }
17927	        check_pending();
17928	        break;
17929	
17930	      case SIG_SETMASK:
17931	        sigdelset((sigset_t *)&sig_set, SIGKILL);
17932	        mp->mp_sigmask = (sigset_t)sig_set;
17933	        check_pending();
17934	        break;
17935	
17936	      case SIG_INQUIRE:
17937	        break;
17938	
17939	      default:
17940	        return(EINVAL);
17941	        break;
17942	  }
17943	  return OK;
17944	}
	
17946	/*===========================================================================*
17947	 *                            do_sigsuspend                                  *
17948	 *===========================================================================*/
17949	PUBLIC int do_sigsuspend()
17950	{
17951	  mp->mp_sigmask2 = mp->mp_sigmask;     /* save the old mask */
17952	  mp->mp_sigmask = (sigset_t) sig_set;
17953	  sigdelset(&mp->mp_sigmask, SIGKILL);
17954	  mp->mp_flags |= SIGSUSPENDED;
17955	  dont_reply = TRUE;
17956	  check_pending();
17957	  return OK;
17958	}
	
	
17961	/*===========================================================================*
17962	 *                               do_sigreturn                                *
17963	 *===========================================================================*/
17964	PUBLIC int do_sigreturn()
17965	{
17966	/* A user signal handler is done.  Restore context and check for
17967	 * pending unblocked signals.
17968	 */
17969	
17970	  int r;
17971	
17972	  mp->mp_sigmask = (sigset_t) sig_set;
17973	  sigdelset(&mp->mp_sigmask, SIGKILL);
17974	
17975	  r = sys_sigreturn(who, (vir_bytes)sig_context, sig_flags);
17976	  check_pending();
17977	  return(r);
17978	}
	
17980	/*===========================================================================*
17981	 *                              do_kill                                      *
17982	 *===========================================================================*/
17983	PUBLIC int do_kill()
17984	{
17985	/* Perform the kill(pid, signo) system call. */
17986	
17987	  return check_sig(pid, sig_nr);
17988	}
	
	
17991	/*===========================================================================*
17992	 *                              do_ksig                                      *
17993	 *===========================================================================*/
17994	PUBLIC int do_ksig()
17995	{
17996	/* Certain signals, such as segmentation violations and DEL, originate in the
17997	 * kernel.  When the kernel detects such signals, it sets bits in a bit map.
17998	 * As soon as MM is awaiting new work, the kernel sends MM a message containing
17999	 * the process slot and bit map.  That message comes here.  The File System
18000	 * also uses this mechanism to signal writing on broken pipes (SIGPIPE).
18001	 */
18002	
18003	  register struct mproc *rmp;
18004	  int i, proc_nr;
18005	  pid_t proc_id, id;
18006	  sigset_t sig_map;
18007	
18008	  /* Only kernel may make this call. */
18009	  if (who != HARDWARE) return(EPERM);
18010	  dont_reply = TRUE;            /* don't reply to the kernel */
18011	  proc_nr = mm_in.SIG_PROC;
18012	  rmp = &mproc[proc_nr];
18013	  if ( (rmp->mp_flags & IN_USE) == 0 || (rmp->mp_flags & HANGING) ) return(OK);
18014	  proc_id = rmp->mp_pid;
18015	  sig_map = (sigset_t) mm_in.SIG_MAP;
18016	  mp = &mproc[0];               /* pretend kernel signals are from MM */
18017	  mp->mp_procgrp = rmp->mp_procgrp;     /* get process group right */
18018	
18019	  /* Check each bit in turn to see if a signal is to be sent.  Unlike
18020	   * kill(), the kernel may collect several unrelated signals for a
18021	   * process and pass them to MM in one blow.  Thus loop on the bit
18022	   * map. For SIGINT and SIGQUIT, use proc_id 0 to indicate a broadcast
18023	   * to the recipient's process group.  For SIGKILL, use proc_id -1 to
18024	   * indicate a systemwide broadcast.
18025	   */
18026	  for (i = 1; i <= _NSIG; i++) {
18027	        if (!sigismember(&sig_map, i)) continue;
18028	        switch (i) {
18029	            case SIGINT:
18030	            case SIGQUIT:
18031	                id = 0; break;  /* broadcast to process group */
18032	            case SIGKILL:
18033	                id = -1; break; /* broadcast to all except INIT */
18034	            case SIGALRM:
18035	                /* Disregard SIGALRM when the target process has not
18036	                 * requested an alarm.  This only applies for a KERNEL
18037	                 * generated signal.
18038	                 */
18039	                if ((rmp->mp_flags & ALARM_ON) == 0) continue;
18040	                rmp->mp_flags &= ~ALARM_ON;
18041	                /* fall through */
18042	            default:
18043	                id = proc_id;
18044	                break;
18045	        }
18046	        check_sig(id, i);
18047	        sys_endsig(proc_nr);    /* tell kernel it's done */
18048	  }
18049	  return(OK);
18050	}
	
	
18053	/*===========================================================================*
18054	 *                              do_alarm                                     *
18055	 *===========================================================================*/
18056	PUBLIC int do_alarm()
18057	{
18058	/* Perform the alarm(seconds) system call. */
18059	
18060	  return(set_alarm(who, seconds));
18061	}
	
	
18064	/*===========================================================================*
18065	 *                              set_alarm                                    *
18066	 *===========================================================================*/
18067	PUBLIC int set_alarm(proc_nr, sec)
18068	int proc_nr;                    /* process that wants the alarm */
18069	int sec;                        /* how many seconds delay before the signal */
18070	{
18071	/* This routine is used by do_alarm() to set the alarm timer.  It is also used
18072	 * to turn the timer off when a process exits with the timer still on.
18073	 */
18074	
18075	  message m_sig;
18076	  int remaining;
18077	
18078	  if (sec != 0)
18079	        mproc[proc_nr].mp_flags |= ALARM_ON;
18080	  else
18081	        mproc[proc_nr].mp_flags &= ~ALARM_ON;
18082	
18083	  /* Tell the clock task to provide a signal message when the time comes.
18084	   *
18085	   * Large delays cause a lot of problems.  First, the alarm system call
18086	   * takes an unsigned seconds count and the library has cast it to an int.
18087	   * That probably works, but on return the library will convert "negative"
18088	   * unsigneds to errors.  Presumably no one checks for these errors, so
18089	   * force this call through.  Second, If unsigned and long have the same
18090	   * size, converting from seconds to ticks can easily overflow.  Finally,
18091	   * the kernel has similar overflow bugs adding ticks.
18092	   *
18093	   * Fixing this requires a lot of ugly casts to fit the wrong interface
18094	   * types and to avoid overflow traps.  DELTA_TICKS has the right type
18095	   * (clock_t) although it is declared as long.  How can variables like
18096	   * this be declared properly without combinatorial explosion of message
18097	   * types?
18098	   */
18099	  m_sig.m_type = SET_ALARM;
18100	  m_sig.CLOCK_PROC_NR = proc_nr;
18101	  m_sig.DELTA_TICKS = (clock_t) (HZ * (unsigned long) (unsigned) sec);
18102	  if ( (unsigned long) m_sig.DELTA_TICKS / HZ != (unsigned) sec)
18103	        m_sig.DELTA_TICKS = LONG_MAX;   /* eternity (really CLOCK_T_MAX) */
18104	  if (sendrec(CLOCK, &m_sig) != OK) panic("alarm er", NO_NUM);
18105	  remaining = (int) m_sig.SECONDS_LEFT;
18106	  if (remaining != m_sig.SECONDS_LEFT || remaining < 0)
18107	        remaining = INT_MAX;    /* true value is not representable */
18108	  return(remaining);
18109	}
	
	
18112	/*===========================================================================*
18113	 *                              do_pause                                     *
18114	 *===========================================================================*/
18115	PUBLIC int do_pause()
18116	{
18117	/* Perform the pause() system call. */
18118	
18119	  mp->mp_flags |= PAUSED;
18120	  dont_reply = TRUE;
18121	  return(OK);
18122	}
	
	
18125	/*=====================================================================*
18126	 *                          do_reboot                                  *
18127	 *=====================================================================*/
18128	PUBLIC int do_reboot()
18129	{
18130	  register struct mproc *rmp = mp;
18131	  char monitor_code[64];
18132	

⌨️ 快捷键说明

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