system-calls

Inline Assembler for wrapper function doesn't work for some reason

五迷三道 提交于 2019-12-02 03:02:14
I'm trying to write a wrapper function for read() system call , using asm volatile , but it won't work , since the res doesn't change its value . Here's the code : ssize_t my_read(int fd, void *buf, size_t count) { ssize_t res; __asm__ volatile( "int $0x80" /* make the request to the OS */ : "=a" (res), /* return result in eax ("a") */ "+b" (fd), /* pass arg1 in ebx ("b") */ "+c" (buf), /* pass arg2 in ecx ("c") */ "+d" (count) /* pass arg3 in edx ("d") */ : "a" (5) /* passing the system call for read to %eax , with call number 5 */ : "memory", "cc"); /* announce to the compiler that the

How to make system call from another system call in kernel space

限于喜欢 提交于 2019-12-02 02:54:55
I am new in Linux kernel development. I have implemented a system call say my_pid in linux kernel 2.6. I want to call getpid system call from my system call. How can I do it? I want something like: pid_t my_pid(){ return getpid(); } Also from C in user-space I can call any system call using: syscall(); What is the generic way to do this in kernel mode? There is no generic way of doing this. If you are in kernel space, you should invoke kernel functions that implement the system call functionality directly instead of using syscall -type instructions, or use other means of extracting the desired

Multiple kernel modules intercepting same system call and crash during unload

大城市里の小女人 提交于 2019-12-01 22:52:41
I'm working on system call interception (for open() system call) and I got one problem: I have two kernel modules ( mod1 and mod2 ) and both of them are trying to intercept open() syscall. I've loaded mod1 first and then mod2 . The mod1 intercepted open() by: original_open1 = sys_call_table[__NR_open]; sys_call_table[__NR_open] = mod1_open; Here original_open1 would be sys_open . After this, mod2 intercepted open() by: original_open2 = sys_call_table[__NR_open]; sys_call_table[__NR_open] = mod2_open; Here, original_open2 would be mod1_open() since mod1 was loaded first. Now, the problem is:

ARM syscall as c++ template

帅比萌擦擦* 提交于 2019-12-01 22:41:41
问题 I need to call some syscalls in my newlib stubs and the current implementation uses C macros which got unreadable and awful looking over time. (And I hate macros...) However, my implementation with C++ templates does only work for one parameter: template <int nr, typename RETTYPE, typename PARAM1> inline RETTYPE syscall(PARAM1 p1) { register PARAM1 r0 asm("r0") = p1; asm volatile("svc %[nr]\n" : "=r" (r0) : [nr] "i" (nr), "r" (r0) : "memory", "r1", "r2", "r3", "r12", "lr"); return (RETTYPE)

sysinfo system call not returning correct freeram value

心不动则不痛 提交于 2019-12-01 21:15:21
问题 I recently wrote the following C code using sysinfo systemcall to display system statistics, what amused me was that the freeram variable of sysinfo structure doesn't return the amount of free RAM instead it is returning the current RAM usage. I had to use a workaround to show the correct value by subtracting freeram from totalram. I have tried googling about this specific variable but to no avail. Any insight into this weird behavior would be really helpful. /* * C program to print the

How does one programmatically determine if “write” system call is atomic on a particular file?

徘徊边缘 提交于 2019-12-01 19:38:16
In some cases the coder cannot rely on system calls being atomic, e.g. if the file is on a NFS filesystem. (c.f. NFS Overview, FAQ and HOWTO Documents ). But atomic system calls are ultimately required for most database work. (c.f. Atomicity of database systems ). Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python). Any suggestions? Subsequent notes: Atomicity on pipes is discussed in the following: unix pipe multiple writers What happens if a write system call is called on same file by 2 different processes

Seeing all the system calls that were made by a Java program

帅比萌擦擦* 提交于 2019-12-01 17:55:36
How can I see which system calls my Java program is making? Is there a tool that will do this on Linux? Use strace : strace java your_program Use strace. But there is is trick for my case. Option -f is needed. For example, the following code: public class Foo { public static void main (String [] args) { System.out.println("XXX"); } } After running javac Foo.java to compile it, strace java Foo 2>&1 | grep write print nothing. But strace -f java Foo 2>&1 | grep write prints: [pid 11655] write(3, "0x63", 4) = 4 [pid 11655] write(3, "\0", 1) = 1 [pid 11655] write(3, "\0", 1) = 1 [pid 11655] write

return value in vfork() system call

徘徊边缘 提交于 2019-12-01 17:37:06
Considering the below code : int main() { int pid; pid=vfork(); if(pid==0) printf("child\n"); else printf("parent\n"); return 0; } In case of vfork() the adress space used by parent process and child process is same, so single copy of variable pid should be there. Now i cant understand how this pid variable can have two values returned by vfork() i.e. zero for child and non zero for parent ? In case of fork() the adress space also gets copied and there are two copy of pid variable in each child and parent, so I can understand in this case two different copies can have different values returned

system call to map memory to a file descriptor (inverse mmap)?

眉间皱痕 提交于 2019-12-01 17:00:20
I want to be able to map memory to a file descriptor so I can use some existing functions that need a file descriptor. Here's essentially what I'm looking for: void do_operation1(int fd); char data[DATA_MAX] = { /* embedded binary data */ }; int fd = addr_to_fd(data, DATA_MAX); do_operation1(fd); /* ... operate on fd ... */ What system call, or calls, can I use to accomplish this? Enquimot You should Check out shm_open() . Some implementations have fmemopen() . (Then of course you have to call fileno() ). If yours doesn't, you can build it yourself with fork() and pipe() . Sure, just open(argv

Where is clone() method in sched.h on Mac OS X

风格不统一 提交于 2019-12-01 16:53:09
I can not find clone() in the sched.h header file. Where is it on Mac OS X? man 2 clone says: The clone() and sys_clone calls are Linux-specific and should not be used in programs intended to be portable. clone() is a Linux-specific call and doesn't exist in OSX. 来源: https://stackoverflow.com/questions/8320174/where-is-clone-method-in-sched-h-on-mac-os-x