进程创建时文件处理

 

static int copy_files(unsigned long clone_flags, struct task_struct *tsk) {  struct files_struct *oldf, *newf;  int error = 0;

 /*   * A background process may not have any files ...   */  oldf = current->files;  if (!oldf)   goto out;

 if (clone_flags & CLONE_FILES) {   atomic_inc(&oldf->count);   goto out;  }

 newf = dup_fd(oldf, &error);  if (!newf)   goto out;

 tsk->files = newf;  error = 0; out:  return error; }

/*  * Allocate a new files structure and copy contents from the  * passed in files structure.  * errorp will be valid only when the returned files_struct is NULL.  */ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp) {  struct files_struct *newf;  struct file **old_fds, **new_fds;  unsigned int open_files, i;  struct fdtable *old_fdt, *new_fdt;

 *errorp = -ENOMEM;  newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);  if (!newf)   goto out;

 atomic_set(&newf->count, 1);

 spin_lock_init(&newf->file_lock);  newf->resize_in_progress = false;  init_waitqueue_head(&newf->resize_wait);  newf->next_fd = 0;  new_fdt = &newf->fdtab;  new_fdt->max_fds = NR_OPEN_DEFAULT;  new_fdt->close_on_exec = newf->close_on_exec_init;  new_fdt->open_fds = newf->open_fds_init;  new_fdt->full_fds_bits = newf->full_fds_bits_init;  new_fdt->fd = &newf->fd_array[0];

 spin_lock(&oldf->file_lock);  old_fdt = files_fdtable(oldf);  open_files = count_open_files(old_fdt);

 /*   * Check whether we need to allocate a larger fd array and fd set.   */  while (unlikely(open_files > new_fdt->max_fds)) {   spin_unlock(&oldf->file_lock);

  if (new_fdt != &newf->fdtab)    __free_fdtable(new_fdt);

  new_fdt = alloc_fdtable(open_files - 1);   if (!new_fdt) {    *errorp = -ENOMEM;    goto out_release;   }

  /* beyond sysctl_nr_open; nothing to do */   if (unlikely(new_fdt->max_fds < open_files)) {    __free_fdtable(new_fdt);    *errorp = -EMFILE;    goto out_release;   }

  /*    * Reacquire the oldf lock and a pointer to its fd table    * who knows it may have a new bigger fd table. We need    * the latest pointer.    */   spin_lock(&oldf->file_lock);   old_fdt = files_fdtable(oldf);   open_files = count_open_files(old_fdt);  }

 copy_fd_bitmaps(new_fdt, old_fdt, open_files);

 old_fds = old_fdt->fd;  new_fds = new_fdt->fd;

 for (i = open_files; i != 0; i--) {   struct file *f = *old_fds++;   if (f) {    get_file(f);   } else {    /*     * The fd may be claimed in the fd bitmap but not yet     * instantiated in the files array if a sibling thread     * is partway through open().  So make sure that this     * fd is available to the new process.     */    __clear_open_fd(open_files - i, new_fdt);   }   rcu_assign_pointer(*new_fds++, f);  }  spin_unlock(&oldf->file_lock);

 /* clear the remainder */  memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));

 rcu_assign_pointer(newf->fdt, new_fdt);

 return newf;

out_release:  kmem_cache_free(files_cachep, newf); out:  return NULL; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值