📄 sys.tex
字号:
@c -*- Texinfo -*-@node Syscalls@chapter System Calls@cindex linking the C libraryThe C subroutine library depends on a handful of subroutine calls foroperating system services. If you use the C library on a system thatcomplies with the POSIX.1 standard (also known as IEEE 1003.1), most ofthese subroutines are supplied with your operating system.If some of these subroutines are not provided with your system---inthe extreme case, if you are developing software for a ``bare board''system, without an OS---you will at least need to provide do-nothingstubs (or subroutines with minimal functionality) to allow yourprograms to link with the subroutines in @code{libc.a}.@menu* Stubs:: Definitions for OS interface* Reentrant Syscalls:: Reentrant covers for OS subroutines@end menu@node Stubs@section Definitions for OS interface@cindex stubs@cindex subroutines for OS interface@cindex OS interface subroutinesThis is the complete set of system definitions (primarily subroutines)required; the examples shown implement the minimal functionalityrequired to allow @code{libc} to link, and fail gracefully where OSservices are not available. Graceful failure is permitted by returning an error code. A minorcomplication arises here: the C library must be compatible withdevelopment environments that supply fully functional versions of thesesubroutines. Such environments usually return error codes in a global@code{errno}. However, the Cygnus C library provides a @emph{macro}definition for @code{errno} in the header file @file{errno.h}, as partof its support for reentrant routines (@pxref{Reentrancy,,Reentrancy}).@cindex @code{errno} global vs macroThe bridge between these two interpretations of @code{errno} isstraightforward: the C library routines with OS interface callscapture the @code{errno} values returned globally, and record them inthe appropriate field of the reentrancy structure (so that you can querythem using the @code{errno} macro from @file{errno.h}).This mechanism becomes visible when you write stub routines for OSinterfaces. You must include @file{errno.h}, then disable the macro,like this:@example#include <errno.h>#undef errnoextern int errno;@end example@noindentThe examples in this chapter include this treatment of @code{errno}.@ftable @code@item _exitExit a program without cleaning up files. If your system doesn'tprovide this, it is best to avoid linking with subroutines that requireit (@code{exit}, @code{system}).@item closeClose a file. Minimal implementation:@exampleint close(int file)@{ return -1;@}@end example@item environA pointer to a list of environment variables and their values. For aminimal environment, this empty list is adequate:@examplechar *__env[1] = @{ 0 @};char **environ = __env;@end example@item execveTransfer control to a new process. Minimal implementation (for a systemwithout processes):@example#include <errno.h>#undef errnoextern int errno;int execve(char *name, char **argv, char **env)@{ errno=ENOMEM; return -1;@}@end example@item forkCreate a new process. Minimal implementation (for a system without processes):@example#include <errno.h>#undef errnoextern int errno;int fork() @{ errno=EAGAIN; return -1;@}@end example@item fstatStatus of an open file. For consistency with other minimalimplementations in these examples, all files are regarded as characterspecial devices. The @file{sys/stat.h} header file required isdistributed in the @file{include} subdirectory for this C library.@example#include <sys/stat.h>int fstat(int file, struct stat *st) @{ st->st_mode = S_IFCHR; return 0;@}@end example@item getpidProcess-ID; this is sometimes used to generate strings unlikely toconflict with other processes. Minimal implementation, for a systemwithout processes:@exampleint getpid() @{ return 1;@}@end example@item isattyQuery whether output stream is a terminal. For consistency with theother minimal implementations, which only support output to@code{stdout}, this minimal implementation is suggested:@exampleint isatty(int file)@{ return 1;@}@end example@item killSend a signal. Minimal implementation:@example#include <errno.h>#undef errnoextern int errno;int kill(int pid, int sig)@{ errno=EINVAL; return(-1);@}@end example@item linkEstablish a new name for an existing file. Minimal implementation:@example#include <errno.h>#undef errnoextern int errno;int link(char *old, char *new)@{ errno=EMLINK; return -1;@}@end example@item lseekSet position in a file. Minimal implementation:@exampleint lseek(int file, int ptr, int dir)@{ return 0;@}@end example@item openOpen a file. Minimal implementation:@exampleint open(const char *name, int flags, int mode)@{ return -1;@}@end example@item readRead from a file. Minimal implementation:@exampleint read(int file, char *ptr, int len)@{ return 0;@}@end example@item sbrkIncrease program data space. As @code{malloc} and related functionsdepend on this, it is useful to have a working implementation. Thefollowing suffices for a standalone system; it exploits the symbol@code{end} automatically defined by the GNU linker.@example@groupcaddr_t sbrk(int incr)@{ extern char end; /* @r{Defined by the linker} */ static char *heap_end; char *prev_heap_end; if (heap_end == 0) @{ heap_end = &end; @} prev_heap_end = heap_end; if (heap_end + incr > stack_ptr) @{ _write (1, "Heap and stack collision\n", 25); abort (); @} heap_end += incr; return (caddr_t) prev_heap_end;@}@end group@end example@item statStatus of a file (by name). Minimal implementation:@exampleint stat(char *file, struct stat *st) @{ st->st_mode = S_IFCHR; return 0;@}@end example@item timesTiming information for current process. Minimal implementation:@exampleint times(struct tms *buf)@{ return -1;@}@end example@item unlinkRemove a file's directory entry. Minimal implementation:@example#include <errno.h>#undef errnoextern int errno;int unlink(char *name)@{ errno=ENOENT; return -1; @}@end example@item waitWait for a child process. Minimal implementation:@example#include <errno.h>#undef errnoextern int errno;int wait(int *status) @{ errno=ECHILD; return -1;@}@end example@item writeWrite a character to a file. @file{libc} subroutines will use thissystem routine for output to all files, @emph{including}@code{stdout}---so if you need to generate any output, for example to aserial port for debugging, you should make your minimal @code{write}capable of doing this. The following minimal implementation is anincomplete example; it relies on a @code{writechar} subroutine (notshown; typically, you must write this in assembler from examplesprovided by your hardware manufacturer) to actually perform the output.@example@groupint write(int file, char *ptr, int len)@{ int todo; for (todo = 0; todo < len; todo++) @{ writechar(*ptr++); @} return len;@}@end group@end example@end ftable@page@node Reentrant Syscalls@section Reentrant covers for OS subroutinesSince the system subroutines are used by other library routines thatrequire reentrancy, @file{libc.a} provides cover routines (for example,the reentrant version of @code{fork} is @code{_fork_r}). These coverroutines are consistent with the other reentrant subroutines in thislibrary, and achieve reentrancy by using a reserved global data block(@pxref{Reentrancy,,Reentrancy}).@c FIXME!!! The following ignored text specifies how this section ought@c to work; however, both standalone info and Emacs info mode fail when@c confronted with nodes beginning `_' as of 24may93. Restore when Info@c readers fixed!@ignore@menu* _open_r:: Reentrant version of open* _close_r:: Reentrant version of close* _lseek_r:: Reentrant version of lseek* _read_r:: Reentrant version of read* _write_r:: Reentrant version of write* _link_r:: Reentrant version of link* _unlink_r:: Reentrant version of unlink* _stat_r:: Reentrant version of stat* _fstat_r:: Reentrant version of fstat* _sbrk_r:: Reentrant version of sbrk* _fork_r:: Reentrant version of fork* _wait_r:: Reentrant version of wait@end menu@down@include reent/filer.def@include reent/execr.def@include reent/statr.def@include reent/fstatr.def@include reent/linkr.def@include reent/unlinkr.def@include reent/sbrkr.def@up@end ignore@ftable @code@item _open_rA reentrant version of @code{open}. It takes a pointerto the global data block, which holds @code{errno}.@exampleint _open_r(void *@var{reent}, const char *@var{file}, int @var{flags}, int @var{mode});@end example@ifset STDIO64@item _open64_rA reentrant version of @code{open64}. It takes a pointerto the global data block, which holds @code{errno}.@exampleint _open64_r(void *@var{reent}, const char *@var{file}, int @var{flags}, int @var{mode});@end example@end ifset@item _close_rA reentrant version of @code{close}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _close_r(void *@var{reent}, int @var{fd});@end example@item _lseek_rA reentrant version of @code{lseek}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleoff_t _lseek_r(void *@var{reent}, int @var{fd}, off_t @var{pos}, int @var{whence});@end example@ifset STDIO64@item _lseek64_rA reentrant version of @code{lseek64}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleoff_t _lseek64_r(void *@var{reent}, int @var{fd}, off_t @var{pos}, int @var{whence});@end example@end ifset@item _read_rA reentrant version of @code{read}. It takes a pointer to the globaldata block, which holds @code{errno}.@examplelong _read_r(void *@var{reent}, int @var{fd}, void *@var{buf}, size_t @var{cnt});@end example@item _write_rA reentrant version of @code{write}. It takes a pointer to the globaldata block, which holds @code{errno}.@examplelong _write_r(void *@var{reent}, int @var{fd}, const void *@var{buf}, size_t @var{cnt});@end example@item _fork_rA reentrant version of @code{fork}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _fork_r(void *@var{reent});@end example@item _wait_rA reentrant version of @code{wait}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _wait_r(void *@var{reent}, int *@var{status});@end example@item _stat_rA reentrant version of @code{stat}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _stat_r(void *@var{reent}, const char *@var{file}, struct stat *@var{pstat});@end example@item _fstat_rA reentrant version of @code{fstat}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _fstat_r(void *@var{reent}, int @var{fd}, struct stat *@var{pstat});@end example@ifset STDIO64@item _fstat64_rA reentrant version of @code{fstat64}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _fstat64_r(void *@var{reent}, int @var{fd}, struct stat *@var{pstat});@end example@end ifset@item _link_rA reentrant version of @code{link}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _link_r(void *@var{reent}, const char *@var{old}, const char *@var{new});@end example@item _unlink_rA reentrant version of @code{unlink}. It takes a pointer to the globaldata block, which holds @code{errno}.@exampleint _unlink_r(void *@var{reent}, const char *@var{file});@end example@item _sbrk_rA reentrant version of @code{sbrk}. It takes a pointer to the globaldata block, which holds @code{errno}.@examplechar *_sbrk_r(void *@var{reent}, size_t @var{incr});@end example@end ftable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -