read()的包装函数无法编译

时间:2012-04-20 04:23:29

标签: c linux system-calls

我正在尝试在Linux下为read()编写一个包装函数。

请放轻松,因为这是我第一次使用Wrappers:)

给定代码my_wrappers.c文件:

#include "my_wrappers.h"
#include <unistd.h>
#include <sys/types.h>

ssize_t my_read (int fd, void *buf, size_t count)
{
  long ret;
  extern int errno;

  __asm__ __volatile__ ("pushl %%ebx\n\t"
                        "movl %%esi,%%ebx\n\t"
                        "int $0x80\n\t"
                        "popl %%ebx"
                      : "=a" (ret)
                      : "0" (SYS_read), "S" ((long) fd),
                        "c" ((long) buf) "d" ((long) count): "bx");
  if (ret >= 0)
  {
    return (int) ret;
  }
  errno = -ret;
  return -1;
}

my_wrappers.h文件:

#ifndef __MY_WRAPPERS_H_
#define __MY_WRAPPERS_H_

#include <unistd.h>
#include <sys/types.h>


int my_open(const char *pathname, int flags, mode_t mode);
ssize_t my_write(int fd, const void *buf, size_t count);
ssize_t my_read(int fd, void *buf, size_t count);
int my_close(int fd);

pid_t my_fork(void);

#endif

我只允许使用调用libc包装器,即我不允许调用open(),read()等。

这段代码有什么问题?也许是因为我没有使用系统调用表的数量?

Eclipse的错误: - ‘SYS_read’ undeclared (first use in this function)

问候

罗恩

1 个答案:

答案 0 :(得分:3)

您需要#include <syscall.h>或可能#include <sys/syscall.h>