system-calls

Linux syscalls and errno

て烟熏妆下的殇ゞ 提交于 2019-12-05 04:18:28
Context: I am trying to write a small C program with inline asm that should run under Linux on an x86_64 system and being compiled with gcc in order to better understand how syscalls work under Linux. My question is: How are error numbers returned from a syscall (e.g. write) in this environment? I understand that when I use a library such as glibc, it takes care of saving the resulting error code in the global errno variable. But where is the error number stored when I call a syscall directly through inline assembler? Will it be stored inside a separate register, or will it be encoded in %rax

Programmatically check whether a linux kernel module exists or not at runtime

∥☆過路亽.° 提交于 2019-12-05 02:48:09
I am writing a C daemon, which depends on the existence of two kernel modules in order to do its job. The program does not directly use these (or any other) modules. It only needs them to exist. Therefore, I would like to programmatically check whether these modules are already loaded or not, in order to warn the user at runtime. Before I start to do things like parsing /proc/modules or lsmod output, does a utility function already exist somewhere? Something like is_module_loaded(const char* name) ; I am pretty sure this has been asked before. However, I think I am missing the correct terms to

Why do we need to call poll_wait in poll?

我只是一个虾纸丫 提交于 2019-12-05 02:33:03
In LDD3, i saw such codes static unsigned int scull_p_poll(struct file *filp, poll_table *wait) { struct scull_pipe *dev = filp->private_data; unsigned int mask = 0; /* * The buffer is circular; it is considered full * if "wp" is right behind "rp" and empty if the * two are equal. */ down(&dev->sem); poll_wait(filp, &dev->inq, wait); poll_wait(filp, &dev->outq, wait); if (dev->rp != dev->wp) mask |= POLLIN | POLLRDNORM; /* readable */ if (spacefree(dev)) mask |= POLLOUT | POLLWRNORM; /* writable */ up(&dev->sem); return mask; } But it says poll_wait won't wait and will return immediately. Then

“short read” from filesystem, when can it happen?

空扰寡人 提交于 2019-12-05 01:39:29
It is obvious that in general the read(2) system call can return less bytes than what was asked to be read. However, quite a few programs assume that when working with a local files, read(2) never returns less than what was asked (unless the file is shorter, of course). So, my question is: on Linux, in which cases can read(2) return less than what was requested if reading from an open file and EOF is not encountered and the amount being read is a few kilobytes at maximum? Some guesses: Can received signals interrupt a read like that, but not make it fail? Can different filesystems affect this

Difference in ABI between x86_64 Linux functions and syscalls

人盡茶涼 提交于 2019-12-05 01:28:04
The x86_64 SysV ABI 's function calling convention defines integer argument #4 to be passed in the rcx register. The Linux kernel syscall ABI, on the other hand, uses r10 for that same purpose. All other arguments are passed in the same registers for both functions and syscalls. This leads to some strange things. Check out, for example, the implementation of mmap in glibc for the x32 platform (for which the same discrepancy exists): 00432ce0 <__mmap>: 432ce0: 49 89 ca mov %rcx,%r10 432ce3: b8 09 00 00 40 mov $0x40000009,%eax 432ce8: 0f 05 syscall So all register are already in place, except we

List of and documentation for system calls for XNU kernel in OSX

社会主义新天地 提交于 2019-12-05 00:21:59
I'm trying to figure out how to get a list of and documentation for the system calls available in the XNU kernel in OSX. I've googled around quite a bit, but haven't been able to find anything of use. As I understand the calling conventions match BSD, is that correct? Thanks Yuji The "official" list is at Darwin page at Apple. Specifically, see the file syscalls.master in the XNU distribution. (If something you expect is missing, try a newer XNU version.) The BSD part of the system calls comes from BSD, but there're mach calls which follow quite different conventions. You'll definitely want to

Calling functions in an so file from Go

淺唱寂寞╮ 提交于 2019-12-04 23:48:22
问题 Is it possible to call a static object (.so) file from Go? I've been searchign Google and I keep hitting upon the claim that I can do lib, _ := syscall.LoadLibrary("...") But trying this gives an error undefined: syscall.LoadLibrary and searching through Godocs I cannot find reference to this function in the syscall package. Is it possible to load a library and call its functions? 回答1: On a POSIX platform, you could use cgo to call dlopen and friends: // #cgo LDFLAGS: -ldl // #include <dlfcn

compile errors using signal.h in Linux [duplicate]

给你一囗甜甜゛ 提交于 2019-12-04 23:15:49
This question already has an answer here: struct sigaction incomplete error 2 answers I'm writing a shell program that must handle signals. My relevant signal handling related code is as follows: #include <signal.h> ... #include <sys/types.h> ... void installSigactions( int, struct sigaction* ); void handler_function( int signal_id ); ... /*define signal table*/ struct sigaction signal_action; /*insert handler function*/ signal_action.sa_handler = handler_function; /*init the flags field*/ signal_action.sa_flags = 0; /*are no masked interrupts*/ sigemptyset( &signal_action.sa_mask ); /*install

Understanding `read, write` system calls in Unix

你。 提交于 2019-12-04 21:22:23
My Systems Programming project has us implementing a compression/decompression program to crunch down ASCII text files by removing the zero top bit and writing the output to a separate file, depending on whether the compression or decompression routine is working. To do this, the professor has required us to use the binary files and Unix system calls, which include open, close, read, write , etc. From my understanding of read and write, it reads the binary data by defined byte chunks. However, since this data is binary, I'm not sure how to parse it. This is a stripped down version of my code,

how can i know whether a linux syscall is thread safe?

跟風遠走 提交于 2019-12-04 20:27:23
some functions in linux mark "thread safe" by _r (e.g. gmtime_r ) but most of the syscalls are not be marked and also not mentioned in manpages. So my question is : How can i konw whether a linux syscall is thread safe? Thank you! I think you mean "library functions"; syscalls should, by virtue of operating on the thread's kernel-side data, be thread-safe. And the answer is: check the manual pages for the functions in question. The "_r" variants are provided specifically for functions which were non-reentrant, meaning that the extra parameters passed to them were statically declared and