ENOSPC vs ENOMEM when to use which?

326 views Asked by At

Linux/Unix defines plenty of relatively similar error codes. Are there any commonly followed conventions suggesting what types of errors should be bound to which error codes?

Let's say my function has some arbitrary container with fixed size (and can't be resized for arbitrary reasons not related to actual RAM usage). If code tries to push too many objects into containers should I rather return ENOMEM or ENOSPC? Is ENOSPC solely dedicated to persistent storage devices space or something like that?

1

There are 1 answers

1
Basile Starynkevitch On BEST ANSWER

Error codes are not returned by programs, but by functions. See syscalls(2) and errno(3) with intro(3)

Correct programs are using somehow exit(3) (implicitly called by crt0 code when main is returning an exit code) but see also signal(7) and execve(2). Use EXIT_SUCCESS and EXIT_FAILURE, but do also look inside /usr/include/sysexits.h

Read also Advanced Linux Programming and man pages.

Study for inspiration the source code of existing open source software, including GNU libc, GNU coreutils, GNU make.

For kernel modules, see also code from kernel.org and kernelnewbies.org