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

📄 utility.c

📁 一个简单的操作系统minix的核心代码
💻 C
字号:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				src/mm/utility.c	 	 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

19100	/* This file contains some utility routines for MM.
19101	 *
19102	 * The entry points are:
19103	 *   allowed:   see if an access is permitted
19104	 *   no_sys:    this routine is called for invalid system call numbers
19105	 *   panic:     MM has run aground of a fatal error and cannot continue
19106	 *   tell_fs:   interface to FS
19107	 */
19108	
19109	#include "mm.h"
19110	#include <sys/stat.h>
19111	#include <minix/callnr.h>
19112	#include <minix/com.h>
19113	#include <fcntl.h>
19114	#include <signal.h>             /* needed only because mproc.h needs it */
19115	#include "mproc.h"
19116	
19117	/*===========================================================================*
19118	 *                              allowed                                      *
19119	 *===========================================================================*/
19120	PUBLIC int allowed(name_buf, s_buf, mask)
19121	char *name_buf;                 /* pointer to file name to be EXECed */
19122	struct stat *s_buf;             /* buffer for doing and returning stat struct*/
19123	int mask;                       /* R_BIT, W_BIT, or X_BIT */
19124	{
19125	/* Check to see if file can be accessed.  Return EACCES or ENOENT if the access
19126	 * is prohibited.  If it is legal open the file and return a file descriptor.
19127	 */
19128	
19129	  int fd;
19130	  int save_errno;
19131	
19132	  /* Use the fact that mask for access() is the same as the permissions mask.
19133	   * E.g., X_BIT in <minix/const.h> is the same as X_OK in <unistd.h> and
19134	   * S_IXOTH in <sys/stat.h>.  tell_fs(DO_CHDIR, ...) has set MM's real ids
19135	   * to the user's effective ids, so access() works right for setuid programs.
19136	   */
19137	  if (access(name_buf, mask) < 0) return(-errno);
19138	
19139	  /* The file is accessible but might not be readable.  Make it readable. */
19140	  tell_fs(SETUID, MM_PROC_NR, (int) SUPER_USER, (int) SUPER_USER);
19141	
19142	  /* Open the file and fstat it.  Restore the ids early to handle errors. */
19143	  fd = open(name_buf, O_RDONLY);
19144	  save_errno = errno;           /* open might fail, e.g. from ENFILE */
19145	  tell_fs(SETUID, MM_PROC_NR, (int) mp->mp_effuid, (int) mp->mp_effuid);
19146	  if (fd < 0) return(-save_errno);
19147	  if (fstat(fd, s_buf) < 0) panic("allowed: fstat failed", NO_NUM);
19148	
19149	  /* Only regular files can be executed. */
19150	  if (mask == X_BIT && (s_buf->st_mode & I_TYPE) != I_REGULAR) {
19151	        close(fd);
19152	        return(EACCES);
19153	  }
19154	  return(fd);
19155	}
	
	
19158	/*===========================================================================*
19159	 *                              no_sys                                       *
19160	 *===========================================================================*/
19161	PUBLIC int no_sys()
19162	{
19163	/* A system call number not implemented by MM has been requested. */
19164	
19165	  return(EINVAL);
19166	}
	
	
19169	/*===========================================================================*
19170	 *                              panic                                        *
19171	 *===========================================================================*/
19172	PUBLIC void panic(format, num)
19173	char *format;                   /* format string */
19174	int num;                        /* number to go with format string */
19175	{
19176	/* Something awful has happened.  Panics are caused when an internal
19177	 * inconsistency is detected, e.g., a programming error or illegal value of a
19178	 * defined constant.
19179	 */
19180	
19181	  printf("Memory manager panic: %s ", format);
19182	  if (num != NO_NUM) printf("%d",num);
19183	  printf("\n");
19184	  tell_fs(SYNC, 0, 0, 0);       /* flush the cache to the disk */
19185	  sys_abort(RBT_PANIC);
19186	}
	
	
19189	/*===========================================================================*
19190	 *                              tell_fs                                      *
19191	 *===========================================================================*/
19192	PUBLIC void tell_fs(what, p1, p2, p3)
19193	int what, p1, p2, p3;
19194	{
19195	/* This routine is only used by MM to inform FS of certain events:
19196	 *      tell_fs(CHDIR, slot, dir, 0)
19197	 *      tell_fs(EXEC, proc, 0, 0)
19198	 *      tell_fs(EXIT, proc, 0, 0)
19199	 *      tell_fs(FORK, parent, child, pid)
19200	 *      tell_fs(SETGID, proc, realgid, effgid)
19201	 *      tell_fs(SETSID, proc, 0, 0)
19202	 *      tell_fs(SETUID, proc, realuid, effuid)
19203	 *      tell_fs(SYNC, 0, 0, 0)
19204	 *      tell_fs(UNPAUSE, proc, signr, 0)
19205	 */
19206	
19207	  message m;
19208	
19209	  m.m1_i1 = p1;
19210	  m.m1_i2 = p2;
19211	  m.m1_i3 = p3;
19212	  _taskcall(FS_PROC_NR, what, &m);
19213	}

⌨️ 快捷键说明

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