📄 syscalls.c
字号:
/* vi: set sw=4 ts=4: *//* * Syscalls for uClibc * * Copyright (C) 2000 by Lineo, inc * Copyright (C) 2001, 2002 by Erik Andersen * Written by Erik Andersen <andersen@codpoet.org> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License * for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#define _GNU_SOURCE#define _LARGEFILE64_SOURCE#include <features.h>#undef __OPTIMIZE__/* We absolutely do _NOT_ want interfaces silently * * * renamed under us or very bad things will happen... */#ifdef __USE_FILE_OFFSET64# undef __USE_FILE_OFFSET64#endif#include <errno.h>#include <sys/types.h>#include <sys/syscall.h>#include <endian.h>//#define __NR_exit 1//See _exit.c//#define __NR_fork 2#ifdef L___libc_fork#include <unistd.h># ifdef __UCLIBC_HAS_MMU__#define __NR___libc_fork __NR_fork _syscall0(pid_t, __libc_fork); weak_alias (__libc_fork, fork)# else pid_t fork(void) { __set_errno(ENOSYS); return -1; }# endif#endif//#define __NR_read 3#ifdef L___libc_read#include <unistd.h>#define __NR___libc_read __NR_read_syscall3(ssize_t, __libc_read, int, fd, __ptr_t, buf, size_t, count);weak_alias(__libc_read, read)#endif//#define __NR_write 4#ifdef L___libc_write#include <unistd.h>#define __NR___libc_write __NR_write_syscall3(ssize_t, __libc_write, int, fd, const __ptr_t, buf, size_t, count);weak_alias(__libc_write, write)/* Stupid libgcc.a from gcc 2.95.x uses __write in pure.o * which is a blatent GNU libc-ism... */weak_alias (__libc_write, __write)#endif//#define __NR_open 5#ifdef L___syscall_open#define __NR___syscall_open __NR_open#include <stdarg.h>/* Do not include fcntl.h, so gcc doesn't whine the prototype */static inline_syscall3(int, __syscall_open, const char *, fn, int, flags, __kernel_mode_t, mode);int __libc_open (const char * fn, int flags, mode_t mode){ return __syscall_open(fn, flags, mode);}weak_alias(__libc_open, open)#endif//#define __NR_close 6#ifdef L___libc_close#include <unistd.h>#define __NR___libc_close __NR_close_syscall1(int, __libc_close, int, fd);weak_alias(__libc_close, close)#endif//#define __NR_waitpid 7// Implemented using wait4 //#define __NR_creat 8#ifdef L___syscall_creat#include <fcntl.h>#ifdef __NR_creat#define __NR___syscall_creat __NR_creat_syscall2(int, __syscall_creat, const char *, file, __kernel_mode_t, mode);int creat (const char *file, mode_t mode){ return __syscall_creat (file, mode);}#elseextern int __libc_open (const char *file, int flags, mode_t mode);int creat (const char *file, mode_t mode){ return __libc_open (file, O_WRONLY|O_CREAT|O_TRUNC, mode);}#endif#endif//#define __NR_link 9#ifdef L_link#include <unistd.h>_syscall2(int, link, const char *, oldpath, const char *, newpath);#endif//#define __NR_unlink 10#ifdef L_unlink#include <unistd.h>_syscall1(int, unlink, const char *, pathname);#endif//#define __NR_execve 11#ifdef L_execve#include <unistd.h>_syscall3(int, execve, const char *, filename, char *const *, argv, char *const *, envp);#endif//#define __NR_chdir 12#ifdef L_chdir#include <unistd.h>_syscall1(int, chdir, const char *, path);#endif//#define __NR_time 13#ifdef L_time#include <time.h>#include <sys/time.h>#ifdef __NR_time_syscall1(time_t, time, time_t *, t);#elsetime_t time (time_t *t){ time_t result; struct timeval tv; if (gettimeofday (&tv, (struct timezone *) NULL)) { result = (time_t) -1; } else { result = (time_t) tv.tv_sec; } if (t != NULL) { *t = result; } return result;}#endif#endif//#define __NR_mknod 14#ifdef L___syscall_mknod#define __NR___syscall_mknod __NR_mknod#include <sys/stat.h>_syscall3(int, __syscall_mknod, const char *, path, __kernel_mode_t, mode, __kernel_dev_t, dev);int mknod(const char *path, mode_t mode, dev_t dev){ __kernel_dev_t k_dev; /* We must convert the dev_t value to a __kernel_dev_t */ k_dev = ((major(dev) & 0xff) << 8) | (minor(dev) & 0xff); return __syscall_mknod(path, mode, k_dev);}#endif//#define __NR_chmod 15#ifdef L___syscall_chmod#include <sys/stat.h>#define __NR___syscall_chmod __NR_chmodstatic inline _syscall2(int, __syscall_chmod, const char *, path, __kernel_mode_t, mode);int chmod(const char *path, mode_t mode){ return __syscall_chmod(path, mode);}#endif/* Old kernels don't have lchown -- do chown instead. This * is sick and wrong, but at least things will compile. * They may not follow links when they should though... */#ifndef __NR_lchown #define __NR_lchown __NR_chown#endif//#define __NR_lchown 16#ifdef L___syscall_lchown#include <unistd.h>#define __NR___syscall_lchown __NR_lchownstatic inline _syscall3(int, __syscall_lchown, const char *, path, __kernel_uid_t, owner, __kernel_gid_t, group);int lchown(const char *path, uid_t owner, gid_t group){ return __syscall_lchown(path, owner, group);}#endif//#define __NR_break 17//#define __NR_oldstat 18//#define __NR_lseek 19#ifdef L___libc_lseek#include <unistd.h>#define __NR___libc_lseek __NR_lseek_syscall3(__off_t, __libc_lseek, int, fildes, __off_t, offset, int, whence);weak_alias(__libc_lseek, lseek)#endif//#define __NR_getpid 20#ifdef L___libc_getpid#include <unistd.h>#if defined (__alpha__)#define __NR_getpid __NR_getxpid#endif#define __NR___libc_getpid __NR_getpid_syscall0(pid_t, __libc_getpid);weak_alias(__libc_getpid, getpid)weak_alias(__libc_getpid, __getpid)#endif//#define __NR_mount 21#ifdef L_mount#include <sys/mount.h>_syscall5(int, mount, const char *, specialfile, const char *, dir, const char *, filesystemtype, unsigned long, rwflag, const void *, data);#endif//#define __NR_umount 22#ifdef L_umount#include <sys/mount.h>_syscall1(int, umount, const char *, specialfile);#endif//#define __NR_setuid 23#ifdef L___syscall_setuid#define __NR___syscall_setuid __NR_setuid#include <unistd.h>static inline _syscall1(int, __syscall_setuid, __kernel_uid_t, uid);int setuid(uid_t uid){ if (uid == (uid_t) ~0 || uid != (uid_t) ((int) uid)) { __set_errno (EINVAL); return -1; } return(__syscall_setuid(uid));}#endif//#define __NR_getuid 24#ifdef L_getuid#include <unistd.h>#if defined (__alpha__)#define __NR_getuid __NR_getxuid#endif_syscall0(uid_t, getuid);#endif//#define __NR_stime 25#ifdef L_stime#include <time.h>#include <sys/time.h>#ifdef _NR_stime_syscall1(int, stime, const time_t *, t);#elseint stime(const time_t *when){ struct timeval tv; if (when == NULL) { __set_errno (EINVAL); return -1; } tv.tv_sec = *when; tv.tv_usec = 0; return settimeofday (&tv, (struct timezone *) 0);}#endif#endif//#define __NR_ptrace 26#ifdef L___ptrace#include <sys/ptrace.h>#define __NR___ptrace __NR_ptrace_syscall4(long, __ptrace, enum __ptrace_request, request, __kernel_pid_t, pid, void*, addr, void*, data);#endif//#define __NR_alarm 27#ifdef L_alarm#include <unistd.h>#ifdef __NR_alarm_syscall1(unsigned int, alarm, unsigned int, seconds);#else#include <sys/time.h>unsigned int alarm (unsigned int seconds){ struct itimerval old, new; unsigned int retval; new.it_value.tv_usec = 0; new.it_interval.tv_sec = 0; new.it_interval.tv_usec = 0; new.it_value.tv_sec = (long int) seconds; if (setitimer (ITIMER_REAL, &new, &old) < 0) { return 0; } retval = old.it_value.tv_sec; if (old.it_value.tv_usec) { ++retval; } return retval;}#endif#endif//#define __NR_oldfstat 28//#define __NR_pause 29#ifdef L___libc_pause#include <unistd.h>#ifdef __NR_pause#define __NR___libc_pause __NR_pause_syscall0(int, __libc_pause);weak_alias(__libc_pause, pause)#else#include <signal.h>int __libc_pause (void){ return(__sigpause(sigblock(0), 0));}weak_alias(__libc_pause, pause)#endif#endif//#define __NR_utime 30#ifdef L_utime#include <utime.h>#ifdef __NR_utime_syscall2(int, utime, const char *, file, const struct utimbuf *, times);#else#include <stdlib.h>#include <sys/time.h>int utime(const char *file, const struct utimbuf *times){ struct timeval timevals[2]; if (times != NULL) { timevals[0].tv_usec = 0L; timevals[1].tv_usec = 0L; timevals[0].tv_sec = (long int) times->actime; timevals[1].tv_sec = (long int) times->modtime; } else { if (gettimeofday (&timevals[0], NULL) < 0) { return -1; } timevals[1] = timevals[0]; } return utimes(file, timevals);}#endif#endif//#define __NR_utimed#ifdef L_utimes#include <utime.h>#ifdef __NR_utimes_syscall2(int, utimes, const char *, file, const struct timeval *, tvp);#else#include <stdlib.h>#include <sys/time.h>int utimes (const char *file, const struct timeval tvp[2]){ struct utimbuf buf, *times; if (tvp) { times = &buf; times->actime = tvp[0].tv_sec; times->modtime = tvp[1].tv_sec; } else { times = NULL; } return utime(file, times);}#endif#endif//#define __NR_stty 31#ifdef L_stty#include <sgtty.h>int stty (int __fd, __const struct sgttyb *__params);{ __set_errno(ENOSYS); return -1;}#endif//#define __NR_gtty 32#ifdef L_gtty#include <sgtty.h>int gtty (int __fd, struct sgttyb *__params){ __set_errno(ENOSYS); return -1;}#endif//#define __NR_access 33#ifdef L_access#include <unistd.h>_syscall2(int, access, const char *, pathname, int, mode);#endif//#define __NR_nice 34#ifdef L_nice#include <unistd.h>#ifdef __NR_nice_syscall1(int, nice, int, inc);#else#include <sys/resource.h>int nice (int incr){ int save, prio, result; save = errno; __set_errno (0); prio = getpriority (PRIO_PROCESS, 0); if (prio == -1) { if (errno != 0) { return -1; } else { __set_errno (save); } } result = setpriority (PRIO_PROCESS, 0, prio + incr); if (result != -1) { return prio + incr; } else { return -1; }}#endif#endif//#define __NR_ftime 35//#define __NR_sync 36//See sync.c//#define __NR_kill 37#ifdef L___syscall_kill#include <signal.h>#undef kill#define __NR___syscall_kill __NR_killstatic inline_syscall2(int, __syscall_kill, __kernel_pid_t, pid, int, sig);int kill(pid_t pid, int sig){ return(__syscall_kill(pid, sig));}#endif//#define __NR_rename 38#ifdef L_rename#include <stdio.h>_syscall2(int, rename, const char *, oldpath, const char *, newpath);#endif//#define __NR_mkdir 39#ifdef L___syscall_mkdir#include <sys/stat.h>#define __NR___syscall_mkdir __NR_mkdirstatic inline _syscall2(int, __syscall_mkdir, const char *, pathname, __kernel_mode_t, mode);int mkdir(const char * pathname, mode_t mode){ return(__syscall_mkdir(pathname, mode));}#endif//#define __NR_rmdir 40#ifdef L_rmdir#include <unistd.h>_syscall1(int, rmdir, const char *, pathname);#endif//#define __NR_dup 41#ifdef L_dup#include <unistd.h>_syscall1(int, dup, int, oldfd);#endif//#define __NR_pipe 42#ifdef L_pipe#include <unistd.h>_syscall1(int, pipe, int *, filedes);#endif//#define __NR_times 43#ifdef L_times#include <sys/times.h>_syscall1(clock_t, times, struct tms *, buf);#endif//#define __NR_prof 44//#define __NR_brk 45//#define __NR_setgid 46#ifdef L___syscall_setgid#include <unistd.h>#define __NR___syscall_setgid __NR_setgidstatic inline _syscall1(int, __syscall_setgid, __kernel_gid_t, gid);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -