reboot复位函数、access函数、popen函数、pclose函数
- == 1. access函数说明==
- == 2. popen函数说明==
- == 3. pclose函数说明==
- == 4. reboot函数说明==
== 1. access函数说明==
- 函数原型
- int access(constchar * pathname,int mode);
- 相关函数
- stat,open,chmod,chown,setuid,setgid
- 表头文件
- #include<unistd.h>
- 函数说明
① 参数pathname——文件名称
② 参数mode——要判断的访问权限。有几种情况组合:
R_OK ——文件可读
W_OK——文件可写
X_OK ——文件可执行
F_OK ——文件存在
有时我们需要判断文件是否可以进行某种操作(读、写等等),这时可以使用access函数。access()会检查是否可以读/写某一已存在的文件。
由于access()只作权限的核查,并不理会文件形态或文件内容,因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。例如,你会发现DOS的文件都具有“可执行”权限,但用execve()执行时则会失败。 - 返回值
- 【当测试成功时,返回0】若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1。
- 错误代码
EACCESS ——参数pathname 所指定的文件不符合所要求测试的权限。
EROFS ——欲测试写入权限的文件存在于只读文件系统内。
EFAULT ——参数pathname指针超出可存取内存空间。
EINVAL ——参数mode 不正确。
ENAMETOOLONG ——参数pathname太长。
ENOTDIR ——参数pathname为一目录。
ENOMEM ——核心内存不足
ELOOP ——参数pathname有过多符号连接问题。
EIO I/O ——存取错误。 - 附加说明
- 使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题
描述
/* 判断是否允许读取/etc/passwd*/
#include<unistd.h>
int main()
{
if (access(“/etc/passwd”,R_OK) = =0)
printf(“/etc/passwd can be read\n”);
}
/etc/passwd can be read
== 2. popen函数说明==
- popen() 函数 用 创建管道 的 方式启动一个 进程, 并调用 shell. 因为 管道是被定义成单向的, 所以 type 参数 只能定义成 只读或者 只写, 不能是 两者同时, 结果流也相应的 是只读 或者 只写.
- command 参数 是 一个 字符串指针, 指向的是一个 以 null 结束符 结尾的字符串, 这个字符串包含 一个 shell 命令. 这个命令 被送到 /bin/sh 以 -c 参数 执行, 即由 shell 来执行. type 参数 也是 一个 指向 以 null 结束符结尾的 字符串的指针, 这个字符串 必须是 ‘r’ 或者 'w’ 来指明 是 读还是写.
- popen() 函数 的 返回值 是一个普通的 标准I/O流, 它只能用 pclose() 函数 来关闭, 而不是 fclose(). 函数. 向这个流 的 写入被转化为 对 command 命令的标准输入; 而 command 命令的 标准输出 则是和 调用 popen(), 函数 的 进程 相同,除非 这个被command命令 自己改变. 相反的, 读取 一个 “被popen了的” 流, 就相当于 读取 command 命令的标准输出, 而 command 的标准输入 则是和 调用 popen, 函数的进程 相同.
+== 注意, popen 函数的 输出流默认是被全缓冲的.==
== 3. pclose函数说明==
- pclose 函数 等待 相关的进程结束并返回 一个 command 命令的 退出状态, 就像 wait4 函数 一样
- popen使用FIFO管道执行外部程序。
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
- popen 通过type是r还是w确定command的输入/输出方向,r和w是相对command的管道而言的。r表示command从管道中读入,w表示 command通过管道输出到它的stdout,popen返回FIFO管道的文件流指针。pclose则用于使用结束后关闭这个指针。
- 下面看一个例子:
/*******************************************************************************************
** Name:popen.c
** This program is used to show the usage of popen() .
*******************************************************************************************/
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main( void )
{
FILE *stream;
FILE *wstream;
char buf[1024];
memset( buf, '/0', sizeof(buf) );//初始化buf,以免后面写如乱码到文件中
stream = popen( "ls -l", "r" ); //将“ls -l”命令的输出 通过管道读取(“r”参数)到FILE* stream
wstream = fopen( "test_popen.txt", "w+"); //新建一个可写的文件
fread( buf, sizeof(char), sizeof(buf), stream); //将刚刚FILE* stream的数据流读取到buf中
fwrite( buf, 1, sizeof(buf), wstream );//将buf中的数据写到FILE *wstream对应的流中,也是写到文件中
pclose( stream );
fclose( wstream );
return 0;
}
[root@localhost src]# gcc popen.c
[root@localhost src]# ./a.out
[root@localhost src]# cat test_popen.txt
总计 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root 542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root 480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root 162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root 443 09-30 00:55 system.c
-rwxr-xr-x 1 root root 0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt
== 4. reboot函数说明==
#include <unistd.h>
#include <sys/reboot.h>
int reboot(int flag);
------------------------------------
#include <unistd.h>
#include <sys/reboot.h>
int main()
{
/* 同步磁盘数据,将缓存数据回写到硬盘,以防数据丢失 */
sync();
return reboot(RB_AUTOBOOT);
}
vim /usr/include/sys/reboot.h
--------------------------------------------------
#define RB_AUTOBOOT 0x01234567
#define RB_HALT_SYSTEM 0xcdef0123
#define RB_ENABLE_CAD 0x89abcdef
#define RB_DISABLE_CAD 0
#define RB_POWER_OFF 0x4321fedc
1 sys_reboot() -->
2 kernel_restart() -->
3 kernel_restart_prepare()
4 machine_restart()
linux-2.6.21.7/include/linux/reboot.h
--------------------------------------------
#define LINUX_REBOOT_CMD_RESTART 0x01234567
#define LINUX_REBOOT_CMD_HALT 0xCDEF0123
#define LINUX_REBOOT_CMD_CAD_ON 0x89ABCDEF
#define LINUX_REBOOT_CMD_CAD_OFF 0x00000000
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321FEDC
#define LINUX_REBOOT_CMD_RESTART2 0xA1B2C3D4
#define LINUX_REBOOT_CMD_SW_SUSPEND 0xD000FCE2
#define LINUX_REBOOT_CMD_KEXEC 0x45584543