system-calls

Sleeping for milliseconds on Windows, Linux, Solaris, HP-UX, IBM AIX, Vxworks, Wind River Linux?

↘锁芯ラ 提交于 2019-11-28 12:04:22
I have to write a C program which has to sleep for milliseconds, which has to run on various platforms like Windows, Linux, Solaris, HP-UX, IBM AIX, Vxworks, and Windriver Linux On Windows , the Sleep system call will work on milliseconds only. On Linux , sleep will work on seconds; usleep will perform on microseconds and it's available on Solaris also. In Vxworks , I hope I can implement using taskDelay and sysClkRateSet . How can I achieve this millisecond sleep on HP-UX, IBM AIX and Wind River Linux? Propably a wrapper using platform specific #define s will do: #if defined(WIN32) #include

Programmatically getting UID and GID from username in Unix?

落爺英雄遲暮 提交于 2019-11-28 10:54:16
I'm trying to use setuid() and setgid() to set the respective id's of a program to drop privileges down from root, but to use them I need to know the uid and gid of the user I want to change to. Is there a system call to do this? I don't want to hardcode it or parse from /etc/passwd . Also I'd like to do this programmatically rather than using: id -u USERNAME Any help would be greatly appreciated Have a look at the getpwnam() and getgrnam() functions. You want to use the getpw* family of system calls, generally in pwd.h . It's essentially a C-level interface to the information in /etc/passwd.

syscall from within GCC inline assembly [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-28 10:28:30
This question already has an answer here: How to invoke a system call via sysenter in inline assembly? 2 answers is it possible to write a single character using a syscall from within an inline assembly block? if so, how? it should look "something" like this: __asm__ __volatile__ ( " movl $1, %%edx \n\t" " movl $80, %%ecx \n\t" " movl $0, %%ebx \n\t" " movl $4, %%eax \n\t" " int $0x80 \n\t" ::: "%eax", "%ebx", "%ecx", "%edx" ); $80 is 'P' in ascii, but that returns nothing. any suggestions much appreciated! Something like char p = 'P'; int main() { __asm__ __volatile__ ( " movl $1, %%edx \n\t"

Linux kernel system call returns -1 instead of {-1, -256}

人盡茶涼 提交于 2019-11-28 09:16:22
问题 I'm a kernel newbie and am facing a weird issue. I have written a proof-of-concept calculator syscall and while it works fine for most computations, it is returning -1 when the SUBTRACTION result is between -1 to -256. If someone can throw some light on what could be happening, would appreciate it. Below is the syscall code. SYSCALL_DEFINE3(calc, int, a, int, b , char, op) { int res_int; switch(op) { case '+': res_int = a + b; break; case '-': res_int = a - b; break; case '*': res_int = a * b

How to pass arguments to processes created by fork()

守給你的承諾、 提交于 2019-11-28 09:08:53
I want to create copies of a process using fork() in C. I cant figure out how to pass arguments to the copies of my process. For example,I want to pass an integer to the process copies. Or I what to do, if I have a loop in which I call fork() and want to pass a unique value to processes (e.g. 0...N) for (int i = 0; i < 4; ++i) { fork(); // pass a unique value to new processes. } The nice part about fork() is that each process you spawn automatically gets a copy of everything the parent has, so for example, let's say we want to pass an int myvar to each of two child processes but I want each to

Accessing a system call directly from user program

ぐ巨炮叔叔 提交于 2019-11-28 08:42:05
On Ubuntu - kernel 2.6.32.2 How to call already existing system call from user code directly without help of any library? I read in books and on internet to solve this then written following code but still getting error. Please help Want to find out the process id of current process #include <stdio.h> #include<linux/unistd.h> // for __NR_getpid _syscall0(int, getpid) int main() { printf("Current Process ID : %d\n",getpid()); return 0; } Error While compilation : root@Omkant:~/os# gcc -Wall getpid.c -o getpid getpid.c:5:16: error: expected declaration specifiers or ‘...’ before ‘getpid’ getpid

x86_64 Assembly Linux System Call Confusion

早过忘川 提交于 2019-11-28 08:25:37
I am currently learning Assembly language on Linux. I have been using the book 'Programming From the Ground Up' and all the examples are 32-bit. My OS is 64-bit and I have been trying to do all the examples in 64-bit. I am having trouble however: .section .data .section .text .global _start _start: movq $60, %rax movq $2, %rbx int $0x80 This merely just calls the Linux exit System call or it should. Instead it causes a SEG FAULT and when I instead do this .section .data .section .text .global _start _start: movq $1, %rax movq $2, %rbx int $0x80 it works. Clearly the problem is the value I move

How does sched_setaffinity() work?

霸气de小男生 提交于 2019-11-28 07:00:05
I am trying to understand how the linux syscall sched_setaffinity() works. This is a follow-on from my question here . I have this guide , which explains how to use the syscall and has a pretty neat (working!) example. So I downloaded the Linux 2.6.27.19 kernel sources . I did a 'grep' for lines containing that syscall, and I got 91 results. Not promising. Ultimately, I'm trying to understand how the kernel is able to set the instruction pointer for a specific core (or processor.) I am familiar with how single-core-single-thread programs work. One might issue a 'jmp foo' instruction, and this

Call to operating system to open url?

混江龙づ霸主 提交于 2019-11-28 05:10:56
What can I use to call the OS to open a URL in whatever browser the user has as default? Not worried about cross-OS compatibility; if it works in linux thats enough for me! kobrien Here is how to open the user's default browser with a given url: import webbrowser webbrowser.open(url[, new=0[, autoraise=True]]) Here is the documentation about this functionality. It's part of Python's stdlibs: http://docs.python.org/library/webbrowser.html I have tested this successfully on Linux, Ubuntu 10.10. Personally I really wouldn't use the webbrowser module. It's a complicated mess of sniffing for

Memory access error sys_rt_sigaction (signal handler)

人走茶凉 提交于 2019-11-28 05:00:04
问题 Following this Interfacing Linux Signals article, i have been trying to use sys_rt_sigaction in amd64 , but always get memory access error when sending the signal. struct sigaction works when using C/C++ function sigaction . What is wrong in sys_rt_sigaction call? C/C++ with ASM code: #include<signal.h> #include<stdio.h> #include<time.h> void handler(int){printf("handler\n");} void restorer(){asm volatile("mov $15,%%rax\nsyscall":::"rax");} struct sigaction act{handler}; timespec ts{10,0};