system-calls

Will wait and waitpid block SIGCHLD and unblock it when they return in Linux?

自古美人都是妖i 提交于 2019-12-24 09:37:47
问题 Here is my code to examine this: void handler(int n) { printf("handler %d\n", n); int status; if (wait(&status) < 0) printf("%s\n", strerror(errno)); } int main() { struct sigaction sig; sigemptyset(&sig.sa_mask); sig.sa_handler = handler; sig.sa_flags = 0; sig.sa_restorer = NULL; struct sigaction sigold; sigaction(SIGCHLD, &sig, &sigold); pid_t pid; int status; printf("before fork\n"); if ((pid = fork()) == 0) { _exit(127); } else if (pid > 0) { printf("before waitpid\n"); if (waitpid(pid,

Alternatives to using stat() to get file type?

若如初见. 提交于 2019-12-24 08:55:58
问题 Are there any alternatives to stat (which is found on most Unix systems) which can determine the file type? The manpage says that a call to stat is expensive, and I need to call it quite often in my app. 回答1: The alternative is fstat() if you already have the file open (so you have a file descriptor for it). Or lstat() if you want to find out about symbolic links rather than the file the symlink points to. I think the man page is exaggerating the cost; it is not much worse than any other

getrlimit() returns wrong value?

走远了吗. 提交于 2019-12-24 03:35:10
问题 Can someone explain to me why the following program creates 7185 threads instead of 7455? void *thr_crt(void *arg) { sleep(64); return 0; } int main(void) { struct rlimit lim; int err; int i; pthread_t tid; if(getrlimit(RLIMIT_NPROC, &lim) < 0) perror("getrlimit error"), exit(1); i = 1; while(pthread_create(&tid, NULL, thr_crt, NULL) == 0) i++; printf("soft limit: %d\n", lim.rlim_cur); printf("hard limit: %d\n", lim.rlim_max); printf("threads %d\n", i-1); return 0; } output: soft limit: 7455

Does executable file of C++ program contain object code of system calls also

只谈情不闲聊 提交于 2019-12-24 03:29:58
问题 We use Linux system calls like fork() , pthread() , signal() and so on in C or C++ programs and compile the program to generate executable file (a.out). Now my doubt is whether the file a.out contain the object code of all linux system calls used, or whether the executable contain only the calls to system functions and the system call functions are linked during runtime? Suppose if I move my a.out file to some other Linux operating system which implements system calls in different syntax and

How to discover that a pendrive is in read-only mode?

萝らか妹 提交于 2019-12-24 03:06:22
问题 My question is the following: I have a software in C++ running in a embedded Linux system, the software has a feature to export some data to a pendrive, now comes my pitfall, some users tried to use a old pendrive in a key to change between read/write and read-only mode. Now, I need to know how to check if the device is in read-only mode the show some feedback to the user in my application. Is there a system call to check the read-only status before mount the device? 回答1: The usual way to

sys_readlink fails EFAULT - alternative

半城伤御伤魂 提交于 2019-12-24 01:27:35
问题 I have the filedescriptor and like to get the real path. Currently i call sys_readlink /proc/self/fd/<fd> which works sometimes, but often i get an error -14 (-EFAULT). Here some Code: fs = get_fs(); set_fs(KERNEL_DS); err = sys_readlink(path, buf, size-1); set_fs(fs); Is there an alternative (probably better) way to get the realpath from kernel? 回答1: Get it from the filep in the task struct, e.g. something like struct task_struct *task; struct files_struct *files; struct file *file; char buf

SYSCALL_INLINE in Android

非 Y 不嫁゛ 提交于 2019-12-24 00:45:14
问题 I need to use syscall internally in Android NDK to prevent hooking of the wrapper functions. In Linux there are macros like SYSCALL_INLINE which allows using syscall without wrapper function. Thus the macro embeds the syscall assembly code into the project directly. I could not find similar macro in Android NDK. Maybe I can write my own functions like this one; https://git.busybox.net/uClibc/tree/libc/sysdeps/linux/arm/syscall.c But I need to have arm, arm_64, x86 and x86_64 versions of the

Extracting system call name and arguments using ptrace

眉间皱痕 提交于 2019-12-24 00:10:03
问题 I am working on an assignment in which i have to implement strace like functionality using ptrace . So far, I have found out how to extract system call number and return value like this: //In parent process struct user_regs_struct regs; ptrace( PTRACE_GETREGS, child_pid, 0, &regs ); //child_pid is the pid of child process executing the required program //or system call passed as command line arguments syscall_num = regs.orig_rax; syscall_retval = regs.rax; But I haven't been able to find how

Handling EINTR (with goto?)

不想你离开。 提交于 2019-12-23 16:13:30
问题 Background: This is a follow-up question to this thread about handling EINTR for system calls in C++ (Linux/GCC). Regardless of whether or not I intend to profile my application, it seems like I should be handling system calls setting errno to EINTR as a special case. There are many, many, many opinions about the use of goto . My question: is a system call setting errno to EINTR a case where goto is considered nominal? If not, then how would you suggest converting the following code to handle

Are Unix/Linux system calls part of POSIX library functions?

╄→尐↘猪︶ㄣ 提交于 2019-12-23 15:58:17
问题 Are Unix/Linux system calls all or mostly in POSIX? Many Linux/Unix programming books say that POSIX library functions may be wrappers of OS system calls, or may be not. E.g. http://www.makelinux.net/books/lkd2/ch05lev1sec1, and https://www.safaribooksonline.com/library/view/understanding-the-linux/0596005652/ch10s01.html A part (called the Single UNIX Specification) of POSIX defines UNIX. Therefore I think POSIX defines the system calls of Unix (and of Linux). Then are Unix/Linux system