MINIX:更改chmod系统调用,打印访问权限

时间:2017-12-17 08:33:23

标签: c chmod minix access-rights

作为minix操作系统(版本3.2.1)的家庭作业的一部分,我的任务是更改chmod系统调用,就像我在调用它之后立即打印名称和访问权限一样。例如,以下命令:

chmod 755 file.c

应该在它之后打印一条信息:

chmod : file.c 755

我想我需要在

中的do_chmod函数中添加/更改代码
  

usr / src目录/服务器/ VFS / protect.c

我在下面添加了此功能的源代码以及我的更改:

/* This file deals with protection in the file system.It contains the code

* for four system calls that relate to protection.
 *

* The entry points into this file are
 *
   do_chmod:    perform the CHMOD and FCHMOD system calls
 *
   do_chown:    perform the CHOWN and FCHOWN system calls
 *
   do_umask:    perform the UMASK system call
 *
   do_access:   perform the ACCESS system call
 */


#include "fs.h"

#include <sys/stat.h>

#include <unistd.h>

#include <minix/callnr.h>

#include "file.h"

#include "fproc.h"

#include "path.h"

#include "param.h"

#include <minix/vfsif.h>

#include "vnode.h"

#include "vmnt.h"


/*===========================================================================*

*               do_chmod                     *

*===========================================================================*/

int do_chmod()

{

/* Perform the chmod(name, mode) and fchmod(fd, mode) system calls.

* syscall might provide 'name' embedded in the message.
 */


  struct filp *flp;

  struct vnode *vp;

  struct vmnt *vmp;

  int r, rfd;

  mode_t result_mode;

  char fullpath[PATH_MAX];

  struct lookup resolve;

  vir_bytes vname;

  size_t vname_length;

  mode_t new_mode;

/*int x;           --> THIS IS MY CODE

int y;             --> THIS IS MY CODE

int z;             --> THIS IS MY CODE */

  flp = NULL;

  vname = (vir_bytes) job_m_in.name;

  vname_length = (size_t) job_m_in.name_length;

  rfd = job_m_in.fd;

  new_mode = (mode_t) job_m_in.mode;


  lookup_init(&resolve, fullpath, PATH_NOFLAGS, &vmp, &vp);

  resolve.l_vmnt_lock = VMNT_READ;

  resolve.l_vnode_lock = VNODE_WRITE;


  if (job_call_nr == CHMOD) {

/* Temporarily open the file */

    if (copy_name(vname_length, fullpath) != OK) {

        /* Direct copy failed, try fetching from user space */

        if (fetch_name(vname, vname_length, fullpath) != OK)

            return(err_code);

    }

    if ((vp = eat_path(&resolve, fp)) == NULL) return(err_code);

  } else {
    /* call_nr == FCHMOD */

    /* File is already opened; get a pointer to vnode from filp. */

    if ((flp = get_filp(rfd, VNODE_WRITE)) == NULL) return(err_code);

    vp = flp->filp_vno;

    dup_vnode(vp);

  }


  /* Only the owner or the super_user may change the mode of a file.

* No one may change the mode of a file on a read-only file system.
   */

  if (vp->v_uid != fp->fp_effuid && fp->fp_effuid != SU_UID)

    r = EPERM;

  else
    r = read_only(vp);


  if (r == OK) {

    /* Now make the change. Clear setgid bit if file is not in caller's

* group */

    if (fp->fp_effuid != SU_UID && vp->v_gid != fp->fp_effgid)

        new_mode &= ~I_SET_GID_BIT;


    r = req_chmod(vp->v_fs_e, vp->v_inode_nr, new_mode, &result_mode);

    if (r == OK)

        vp->v_mode = result_mode;

  }


  if (job_call_nr == CHMOD)
 {

    unlock_vnode(vp);

    unlock_vmnt(vmp);

  }
 else { 
/* FCHMOD */

    unlock_filp(flp);

}

/*if(bits & S_IRUSR) x = x + 4;   --> THIS IS MY CODE

if(bits & S_IWUSR) x = x + 2;     --> THIS IS MY CODE

if(bits & S_IXUSR) x = x + 1;     --> THIS IS MY CODE

if(bits & S_IRWXG) y = y + 4;     --> THIS IS MY CODE

if(bits & S_IWGRP) y = y + 2;     --> THIS IS MY CODE

if(bits & S_IXGRP) y = y + 1;     --> THIS IS MY CODE

if(bits & S_IROTH) z = z + 4;     --> THIS IS MY CODE

if(bits & S_IWOTH) z = z + 2;     --> THIS IS MY CODE

if(bits & S_IXOTH) z = z + 1;     --> THIS IS MY CODE

printf("chmod: %s %d%d%d",fullpath,x,y,z);   --> THIS IS MY CODE */


  put_vnode(vp);

  return(r);

}

现在我在mkdir系统调用中完成了类似的任务,其目标有点类似,并且这些修改有效。但是在编译代码期间,它会弹出一个错误,它无法识别位变量。我不知道为什么这个错误,因为该文件包含sys / stat.h库。有什么想法吗?

0 个答案:

没有答案