深入理解系统调用

本人学号最后两位为30,根据 syscall_32.tbl 中的相应映射,选择 utime 系统调用展开分析。

utime 系统调用的功能为“更改一个文件的访问和修改时间”,实际使用的效果如下:

可以看到,在 03:11 创建的 file1 和 file2 文件,未经过 utime 系统调用的处理,最后修改时间、最后访问时间、更改状态时间均为 03:11。

经过 ./utime file1 的命令后,可以看到 file1 的最后修改时间和最后访问时间未变,但是更改状态时间则更改为程序运行时的时间,程序成功运行。

以下为通过汇编指令触发的具体的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/stat.h>

int main (int argc, char **argv)
{
 int i, fd;
 struct stat statbuf;
 struct utimbuf timebuf;
 
 for(i = 1; i < argc; i++)
 {
 if(stat(argv[i], &statbuf) < 0)
 {
 printf ("%s: stat error\n", argv[i]);
 continue;
 }
 if((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0)
 {
 printf ("%s: open error\n", argv[i]);
 continue;
 }
 close(fd);
 timebuf.actime = statbuf.st_atime;
 timebuf.modtime = statbuf.st_mtime;

 int res; // 存储函数运算结果
 asm volatile(
    "movl %1, %%ebx\n\t"  // 将第一个参数 argv[i] 放入 ebx 寄存器
    "movl %2, %%ecx\n\t"  // 将第二个参数 &timebuf 放入 ecx 寄存器
    "movl $30, %%eax\n\t" // utime 的系统调用号为30,将其放入 eax 寄存器
    "int $0x80\n\t" // 触发系统中断
    "movl %%eax, %0\n\t" // 将函数处理结果返回给 res 变量中
    :"=m"(res)
    :"b"(argv[i]),"c"(&timebuf)
 );
//  if(utime(argv[i], &timebuf) < 0)
if(res < 0)
 {
 printf ("%s: utime error\n", argv[i]);
 continue;
 }
 }
 exit(0);
}

猜你喜欢

转载自www.cnblogs.com/lygttxs/p/12902591.html