I study programming under Linux, in particular system calls :)

Hours-long search for a normal reference for system calls ended, in fact, with three links:

Everything else is about the same, only with a different design. Well, or some textbook about Unix in general (albeit in Russian).

So, in practice, you will have to use all 3 directories ...

  1. In the first one, find the necessary function and read its description (not bad, that everything is in Russian).
  2. In the second, find the function number by name.
  3. The third is to use when there is not enough information from the 1st (there is more detailed information and the number of functions is much more ... and even there are source codes).

Who has, throw, pliz, a reference to a convenient, complete, structured directory, which will indicate ( important! ) The function numbers and the values ​​of the constants (such as EBADF , etc., because it is necessary for the assembler). That the standard input descriptor = 0, and the output = 1 I already understood, but it seems to me, there are still a lot of similar pieces there, which again I’ll have to look for in example on the Internet, which is not very convenient ... more examples of use will be, it will be generally super (although not critical).


In particular, I have such a question : brk and sbrk are different functions. And the function number ( eax ) seems to be the same ... How can this be? What's the catch? Or does the system determine the value of the transmitted parameter (of the type> 0x8000000, which means brk , otherwise sbrk )?

  • Well, let's say I found this h-file (albeit, on a different path, well, ok). We look at the write function: man7.org/linux/man-pages/man2/write.2.html In the description a bunch of constants: RLIMIT_FSIZE, EAGAIN, EBADF, etc. In unistd.h they are not. Where to find them? Search all the files in the entire include folder? The manual of the names of the header files in which they are registered is not ... Some kind of nonsense ... In Windows, at least, it’s clear where to look, a list of h-files is listed in MSDN. ps ... And to whom I answered? Message already rubbed ... - Jin X
  • one
    Generally speaking, to use definitions for EAGAIN, EBADF, etc., you need to include the header <errno.h> . If you want to find out the real value of the constant, you can look at one of the files listed below. The macro RLIMIT_FSIZE should be defined somewhere inside <unistd.h> - mymedia
  • one
    man-s and sources (grep first tool). For example, I like this implementation for Linux git.musl-libc.org/cgit/musl/tree - avp
  • one
    And from books I would advise spbk-spo.com/Professional/matematika_i_informatika/… and ozon.ru/context/detail/id/17925734 by the same author (link to the text did not google at once) - avp
  • @avp git grep too soon. - 0andriy

1 answer 1

Often, the most complete documentation is source code. For example,

  • The system call numbers can be found in the /usr/include/asm/unistd.h file. For each architecture, the numbers are different, so this header includes others depending on certain macros. In particular, i386 and amd64 architectures have a slightly different list of numbers and calling conventions .
  • The macro values ​​for errno are scattered across different files. But /usr/include/errno.h includes them all. In glibc, these are mainly:
    • /usr/include/asm-generic/errno.h
    • /usr/include/x86_64-linux-gnu/bits/errno.h
  • Judging by the same glibc source, the sbrk call does n’t exist as such - it is a superstructure over brk . See the misc/sbrk.c in the glibc source package. (You can get it with apt source glibc ). By the way, the source code for the glibc library's brk function is in the sysdeps/unix/sysv/linux/ia64/brk.S for the 64-bit PC architecture; it's just an assembly language shell for a real system call.

Generally speaking, it seems to me more rational not to write hard constants in the assembly code, but simply to include macros from <errno.h> or <unistd.h> . GNU Assembler supports the front-end preprocessor, so you can simply include these headers with the #include directive.

  • 2
    My opinion: all this is useful only for learning, but not for writing real code in production. - mymedia
  • Well thank you. And where in general can you read about what files you need to connect to use a particular call and constants used? Here in MSDN everything is clear: the operation of each function, all header files, etc. are described in detail. And here? - Jin X
  • @JinX, guides from the second section. Type man 2 имя_вызова . Which header to connect is specified in the Syntax section (“Synopsis”). In principle, one <unistd.h> enough. It can even be included in the assembly listings for gas . - mymedia
  • And I still did not understand how to call sbrk ? Just like brk , by the same number or what? The file /usr/include/misc/sbrk.c could not be found either in the installed Ubuntu or here: github.com/torvalds/linux/tree/master/include/misc . How, in fact, brk.S (I even searched for these files over the whole disk). - Jin X
  • 2
    And it seems to me that it will be more convenient for you to get all the glibc sources from the official site of the project - mymedia