The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/servers/pm/utility.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* This file contains some utility routines for PM.
    2  *
    3  * The entry points are:
    4  *   find_param:        look up a boot monitor parameter
    5  *   get_free_pid:      get a free process or group id
    6  *   allowed:           see if an access is permitted
    7  *   no_sys:            called for invalid system call numbers
    8  *   panic:             PM has run aground of a fatal error 
    9  *   tell_fs:           interface to FS
   10  *   get_mem_map:       get memory map of given process
   11  *   get_stack_ptr:     get stack pointer of given process      
   12  *   proc_from_pid:     return process pointer from pid number
   13  */
   14 
   15 #include "pm.h"
   16 #include <sys/stat.h>
   17 #include <minix/callnr.h>
   18 #include <minix/com.h>
   19 #include <fcntl.h>
   20 #include <signal.h>             /* needed only because mproc.h needs it */
   21 #include "mproc.h"
   22 #include "param.h"
   23 
   24 #include <minix/config.h>
   25 #include <timers.h>
   26 #include <string.h>
   27 #include "../../kernel/const.h"
   28 #include "../../kernel/config.h"
   29 #include "../../kernel/type.h"
   30 #include "../../kernel/proc.h"
   31 
   32 /*===========================================================================*
   33  *                              get_free_pid                                 *
   34  *===========================================================================*/
   35 PUBLIC pid_t get_free_pid()
   36 {
   37   static pid_t next_pid = INIT_PID + 1;         /* next pid to be assigned */
   38   register struct mproc *rmp;                   /* check process table */
   39   int t;                                        /* zero if pid still free */
   40 
   41   /* Find a free pid for the child and put it in the table. */
   42   do {
   43         t = 0;                  
   44         next_pid = (next_pid < NR_PIDS ? next_pid + 1 : INIT_PID + 1);
   45         for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++)
   46                 if (rmp->mp_pid == next_pid || rmp->mp_procgrp == next_pid) {
   47                         t = 1;
   48                         break;
   49                 }
   50   } while (t);                                  /* 't' = 0 means pid free */
   51   return(next_pid);
   52 }
   53 
   54 /*===========================================================================*
   55  *                              allowed                                      *
   56  *===========================================================================*/
   57 PUBLIC int allowed(name_buf, s_buf, mask)
   58 char *name_buf;                 /* pointer to file name to be EXECed */
   59 struct stat *s_buf;             /* buffer for doing and returning stat struct*/
   60 int mask;                       /* R_BIT, W_BIT, or X_BIT */
   61 {
   62 /* Check to see if file can be accessed.  Return EACCES or ENOENT if the access
   63  * is prohibited.  If it is legal open the file and return a file descriptor.
   64  */
   65   int fd;
   66   int save_errno;
   67 
   68   /* Use the fact that mask for access() is the same as the permissions mask.
   69    * E.g., X_BIT in <minix/const.h> is the same as X_OK in <unistd.h> and
   70    * S_IXOTH in <sys/stat.h>.  tell_fs(DO_CHDIR, ...) has set PM's real ids
   71    * to the user's effective ids, so access() works right for setuid programs.
   72    */
   73   if (access(name_buf, mask) < 0) return(-errno);
   74 
   75   /* The file is accessible but might not be readable.  Make it readable. */
   76   tell_fs(SETUID, PM_PROC_NR, (int) SUPER_USER, (int) SUPER_USER);
   77 
   78   /* Open the file and fstat it.  Restore the ids early to handle errors. */
   79   fd = open(name_buf, O_RDONLY | O_NONBLOCK);
   80   save_errno = errno;           /* open might fail, e.g. from ENFILE */
   81   tell_fs(SETUID, PM_PROC_NR, (int) mp->mp_effuid, (int) mp->mp_effuid);
   82   if (fd < 0) return(-save_errno);
   83   if (fstat(fd, s_buf) < 0) panic(__FILE__,"allowed: fstat failed", NO_NUM);
   84 
   85   /* Only regular files can be executed. */
   86   if (mask == X_BIT && (s_buf->st_mode & I_TYPE) != I_REGULAR) {
   87         close(fd);
   88         return(EACCES);
   89   }
   90   return(fd);
   91 }
   92 
   93 /*===========================================================================*
   94  *                              no_sys                                       *
   95  *===========================================================================*/
   96 PUBLIC int no_sys()
   97 {
   98 /* A system call number not implemented by PM has been requested. */
   99 
  100   return(ENOSYS);
  101 }
  102 
  103 /*===========================================================================*
  104  *                              panic                                        *
  105  *===========================================================================*/
  106 PUBLIC void panic(who, mess, num)
  107 char *who;                      /* who caused the panic */
  108 char *mess;                     /* panic message string */
  109 int num;                        /* number to go with it */
  110 {
  111 /* An unrecoverable error has occurred.  Panics are caused when an internal
  112  * inconsistency is detected, e.g., a programming error or illegal value of a
  113  * defined constant. The process manager decides to shut down. This results 
  114  * in a HARD_STOP notification to all system processes to allow local cleanup.
  115  */
  116   message m;
  117   int s;
  118 
  119   /* Switch to primary console and print panic message. */
  120   check_sig(mproc[TTY_PROC_NR].mp_pid, SIGTERM);
  121   printf("PM panic (%s): %s", who, mess);
  122   if (num != NO_NUM) printf(": %d",num);
  123   printf("\n");
  124 
  125   /* Allow for debug dumps if the IS server is available. */
  126   m.m_type = PANIC_DUMPS;
  127   if (OK == (s= nb_send(11, &m))) {
  128       return;                           /* IS responsible for exit */
  129   }
  130   printf("Shutting down: IS is not answering: %d\n", s);
  131   sys_abort(RBT_PANIC);
  132 }
  133 
  134 /*===========================================================================*
  135  *                              tell_fs                                      *
  136  *===========================================================================*/
  137 PUBLIC void tell_fs(what, p1, p2, p3)
  138 int what, p1, p2, p3;
  139 {
  140 /* This routine is only used by PM to inform FS of certain events:
  141  *      tell_fs(CHDIR, slot, dir, 0)
  142  *      tell_fs(EXEC, proc, 0, 0)
  143  *      tell_fs(EXIT, proc, 0, 0)
  144  *      tell_fs(FORK, parent, child, pid)
  145  *      tell_fs(SETGID, proc, realgid, effgid)
  146  *      tell_fs(SETSID, proc, 0, 0)
  147  *      tell_fs(SETUID, proc, realuid, effuid)
  148  *      tell_fs(UNPAUSE, proc, signr, 0)
  149  *      tell_fs(STIME, time, 0, 0)
  150  * Ignore this call if the FS is already dead, e.g. on shutdown.
  151  */
  152   message m;
  153 
  154   if ((mproc[FS_PROC_NR].mp_flags & (IN_USE|ZOMBIE)) != IN_USE)
  155       return;
  156 
  157   m.tell_fs_arg1 = p1;
  158   m.tell_fs_arg2 = p2;
  159   m.tell_fs_arg3 = p3;
  160   _taskcall(FS_PROC_NR, what, &m);
  161 }
  162 
  163 /*===========================================================================*
  164  *                              find_param                                   *
  165  *===========================================================================*/
  166 PUBLIC char *find_param(name)
  167 const char *name;
  168 {
  169   register const char *namep;
  170   register char *envp;
  171 
  172   for (envp = (char *) monitor_params; *envp != 0;) {
  173         for (namep = name; *namep != 0 && *namep == *envp; namep++, envp++)
  174                 ;
  175         if (*namep == '\0' && *envp == '=') 
  176                 return(envp + 1);
  177         while (*envp++ != 0)
  178                 ;
  179   }
  180   return(NULL);
  181 }
  182 
  183 /*===========================================================================*
  184  *                              get_mem_map                                  *
  185  *===========================================================================*/
  186 PUBLIC int get_mem_map(proc_nr, mem_map)
  187 int proc_nr;                                    /* process to get map of */
  188 struct mem_map *mem_map;                        /* put memory map here */
  189 {
  190   struct proc p;
  191   int s;
  192 
  193   if ((s=sys_getproc(&p, proc_nr)) != OK)
  194         return(s);
  195   memcpy(mem_map, p.p_memmap, sizeof(p.p_memmap));
  196   return(OK);
  197 }
  198 
  199 /*===========================================================================*
  200  *                              get_stack_ptr                                *
  201  *===========================================================================*/
  202 PUBLIC int get_stack_ptr(proc_nr, sp)
  203 int proc_nr;                                    /* process to get sp of */
  204 vir_bytes *sp;                                  /* put stack pointer here */
  205 {
  206   struct proc p;
  207   int s;
  208 
  209   if ((s=sys_getproc(&p, proc_nr)) != OK)
  210         return(s);
  211   *sp = p.p_reg.sp;
  212   return(OK);
  213 }
  214 
  215 /*===========================================================================*
  216  *                              proc_from_pid                                *
  217  *===========================================================================*/
  218 PUBLIC int proc_from_pid(mp_pid)
  219 pid_t mp_pid;
  220 {
  221         int rmp;
  222 
  223         for (rmp = 0; rmp < NR_PROCS; rmp++)
  224                 if (mproc[rmp].mp_pid == mp_pid)
  225                         return rmp;
  226 
  227         return -1;
  228 }
  229 

Cache object: 10877f86ab2eb23ab432ca6fa68c1270


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.