atexit()注册终止函数和t权限

来源:互联网 发布:linux ntp服务器 源码 编辑:程序博客网 时间:2024/05/22 18:22

1.atexit()函数

   atexit()注册终止函数,函数注册的函数应为不接受任何参数的void函数,exit()调用函数的顺序与函数注册的顺序正好相反。

1 #include<stdio.h>

  2 #include<stdlib.h>  3 void fun1()  4 {  5     printf("this is fun1()\n");  6 }  7 void fun2()  8 {  9     printf("this is fun2()\n"); 10 } 11 void fun3() 12 { 13     printf("this is fun3()\n"); 14 } 15 int main() 16 { 17     atexit(fun1); 18     atexit(fun2); 19     atexit(fun3); 20     return 0; 21 }
结果如图所示:



2.t权限

t权限只针对目录生效,它表示只能让所属主以及root可以删除(重命名/移动)该目录下的文件。

如下例:

在用户g33时,建立一个目录myt,并给它添加t权限


在目录myt中创建一个文件myt.c;然后切换到用户yy中,尝试删除文件myt.c,显示删除失败


然后再切换回原来的用户,删除文件成功。



3.strcut file---文件结构体

文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 struct file。


原型如下:


struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head    fu_list;
struct rcu_head     fu_rcuhead;
} f_u;
struct path        f_path;
#define f_dentry    f_path.dentry   //该成员是对应的 目录结构 。
#define f_vfsmnt    f_path.mnt
const struct file_operations    *f_op;  //该操作 是定义文件关联的操作的。内核在执行open时对这个 指针赋值。 

/*内核安排这个指针作为它的 open 实现的一部分,当需要分派什么操作时,会读取它。filp->f_op 因为不会被内核保存起来以在其后之用,所以我们可以改变我们对相关文件的操作,在对文件使用新的操作方法时,我们就会转移到相应调用上。*/

atomic_long_t        f_count;
 unsigned int         f_flags;  //该成员是文件标志。 
mode_t            f_mode;

/*文件模式根据 FMMODE_READ FMODE_WRITE 位来识别文件是否可读或可写,或是可读可写。在read()write()系统调用中,没有必要对此权限进行检查,因为内核已经在你的系统调用之前已经做了检查。如果文件没有相应的读或写权限,那么如果尝试读写都将被拒绝,驱动程序甚至对此情况毫无知觉*/

loff_t            f_pos;

  /*此变量表示当前的文件读写位置。loff_t 在所有的平台上都是 64 位的变量( long long, gcc专用术语)。驱动程序如果想知道当前在文件中所处位置,那么可以通过读取此变量得知,但是一般地不应直接对此进行更改。通过llseek()方法可以改变文件位置。*/


struct fown_struct    f_owner;
unsigned int        f_uid, f_gid;
struct file_ra_state    f_ra;

u64            f_version;
#ifdef CONFIG_SECURITY
void            *f_security;
#endif
/* needed for tty driver, and maybe others */
void            *private_data;//该成员是系统调用时保存状态信息非常有用的资源。 

#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head    f_ep_links;
spinlock_t        f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space    *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};





0 0