fixed bash/dash/sh issue (Ubuntu)
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / sys.tex
blob362732ace85dbf1b8f4749aefc18eda593765ca0
1 @c -*- Texinfo -*-
2 @node Syscalls
3 @chapter System Calls
5 @cindex linking the C library
6 The C subroutine library depends on a handful of subroutine calls for
7 operating system services. If you use the C library on a system that
8 complies with the POSIX.1 standard (also known as IEEE 1003.1), most of
9 these subroutines are supplied with your operating system.
11 If some of these subroutines are not provided with your system---in
12 the extreme case, if you are developing software for a ``bare board''
13 system, without an OS---you will at least need to provide do-nothing
14 stubs (or subroutines with minimal functionality) to allow your
15 programs to link with the subroutines in @code{libc.a}.
17 @menu
18 * Stubs:: Definitions for OS interface
19 * Reentrant Syscalls:: Reentrant covers for OS subroutines
20 @end menu
22 @node Stubs
23 @section Definitions for OS interface
24 @cindex stubs
26 @cindex subroutines for OS interface
27 @cindex OS interface subroutines
28 This is the complete set of system definitions (primarily subroutines)
29 required; the examples shown implement the minimal functionality
30 required to allow @code{libc} to link, and fail gracefully where OS
31 services are not available.
33 Graceful failure is permitted by returning an error code. A minor
34 complication arises here: the C library must be compatible with
35 development environments that supply fully functional versions of these
36 subroutines. Such environments usually return error codes in a global
37 @code{errno}. However, the Cygnus C library provides a @emph{macro}
38 definition for @code{errno} in the header file @file{errno.h}, as part
39 of its support for reentrant routines (@pxref{Reentrancy,,Reentrancy}).
41 @cindex @code{errno} global vs macro
42 The bridge between these two interpretations of @code{errno} is
43 straightforward: the C library routines with OS interface calls
44 capture the @code{errno} values returned globally, and record them in
45 the appropriate field of the reentrancy structure (so that you can query
46 them using the @code{errno} macro from @file{errno.h}).
48 This mechanism becomes visible when you write stub routines for OS
49 interfaces. You must include @file{errno.h}, then disable the macro,
50 like this:
52 @example
53 #include <errno.h>
54 #undef errno
55 extern int errno;
56 @end example
58 @noindent
59 The examples in this chapter include this treatment of @code{errno}.
61 @ftable @code
62 @item _exit
63 Exit a program without cleaning up files. If your system doesn't
64 provide this, it is best to avoid linking with subroutines that require
65 it (@code{exit}, @code{system}).
67 @item close
68 Close a file. Minimal implementation:
70 @example
71 int close(int file)@{
72 return -1;
74 @end example
76 @item environ
77 A pointer to a list of environment variables and their values. For a
78 minimal environment, this empty list is adequate:
80 @example
81 char *__env[1] = @{ 0 @};
82 char **environ = __env;
83 @end example
85 @item execve
86 Transfer control to a new process. Minimal implementation (for a system
87 without processes):
89 @example
90 #include <errno.h>
91 #undef errno
92 extern int errno;
93 int execve(char *name, char **argv, char **env)@{
94 errno=ENOMEM;
95 return -1;
97 @end example
99 @item fork
100 Create a new process. Minimal implementation (for a system without processes):
102 @example
103 #include <errno.h>
104 #undef errno
105 extern int errno;
106 int fork() @{
107 errno=EAGAIN;
108 return -1;
110 @end example
112 @item fstat
113 Status of an open file. For consistency with other minimal
114 implementations in these examples, all files are regarded as character
115 special devices. The @file{sys/stat.h} header file required is
116 distributed in the @file{include} subdirectory for this C library.
118 @example
119 #include <sys/stat.h>
120 int fstat(int file, struct stat *st) @{
121 st->st_mode = S_IFCHR;
122 return 0;
124 @end example
126 @item getpid
127 Process-ID; this is sometimes used to generate strings unlikely to
128 conflict with other processes. Minimal implementation, for a system
129 without processes:
131 @example
132 int getpid() @{
133 return 1;
135 @end example
137 @item isatty
138 Query whether output stream is a terminal. For consistency with the
139 other minimal implementations, which only support output to
140 @code{stdout}, this minimal implementation is suggested:
142 @example
143 int isatty(int file)@{
144 return 1;
146 @end example
148 @item kill
149 Send a signal. Minimal implementation:
151 @example
152 #include <errno.h>
153 #undef errno
154 extern int errno;
155 int kill(int pid, int sig)@{
156 errno=EINVAL;
157 return(-1);
159 @end example
161 @item link
162 Establish a new name for an existing file. Minimal implementation:
164 @example
165 #include <errno.h>
166 #undef errno
167 extern int errno;
168 int link(char *old, char *new)@{
169 errno=EMLINK;
170 return -1;
172 @end example
174 @item lseek
175 Set position in a file. Minimal implementation:
177 @example
178 int lseek(int file, int ptr, int dir)@{
179 return 0;
181 @end example
183 @item open
184 Open a file. Minimal implementation:
186 @example
187 int open(const char *name, int flags, int mode)@{
188 return -1;
190 @end example
192 @item read
193 Read from a file. Minimal implementation:
195 @example
196 int read(int file, char *ptr, int len)@{
197 return 0;
199 @end example
201 @item sbrk
202 Increase program data space. As @code{malloc} and related functions
203 depend on this, it is useful to have a working implementation. The
204 following suffices for a standalone system; it exploits the symbol
205 @code{end} automatically defined by the GNU linker.
207 @example
208 @group
209 caddr_t sbrk(int incr)@{
210 extern char end; /* @r{Defined by the linker} */
211 static char *heap_end;
212 char *prev_heap_end;
214 if (heap_end == 0) @{
215 heap_end = &end;
217 prev_heap_end = heap_end;
218 if (heap_end + incr > stack_ptr)
220 _write (1, "Heap and stack collision\n", 25);
221 abort ();
224 heap_end += incr;
225 return (caddr_t) prev_heap_end;
227 @end group
228 @end example
230 @item stat
231 Status of a file (by name). Minimal implementation:
233 @example
234 int stat(char *file, struct stat *st) @{
235 st->st_mode = S_IFCHR;
236 return 0;
238 @end example
240 @item times
241 Timing information for current process. Minimal implementation:
243 @example
244 int times(struct tms *buf)@{
245 return -1;
247 @end example
249 @item unlink
250 Remove a file's directory entry. Minimal implementation:
252 @example
253 #include <errno.h>
254 #undef errno
255 extern int errno;
256 int unlink(char *name)@{
257 errno=ENOENT;
258 return -1;
260 @end example
262 @item wait
263 Wait for a child process. Minimal implementation:
264 @example
265 #include <errno.h>
266 #undef errno
267 extern int errno;
268 int wait(int *status) @{
269 errno=ECHILD;
270 return -1;
272 @end example
274 @item write
275 Write a character to a file. @file{libc} subroutines will use this
276 system routine for output to all files, @emph{including}
277 @code{stdout}---so if you need to generate any output, for example to a
278 serial port for debugging, you should make your minimal @code{write}
279 capable of doing this. The following minimal implementation is an
280 incomplete example; it relies on a @code{writechar} subroutine (not
281 shown; typically, you must write this in assembler from examples
282 provided by your hardware manufacturer) to actually perform the output.
284 @example
285 @group
286 int write(int file, char *ptr, int len)@{
287 int todo;
289 for (todo = 0; todo < len; todo++) @{
290 writechar(*ptr++);
292 return len;
294 @end group
295 @end example
297 @end ftable
299 @page
300 @node Reentrant Syscalls
301 @section Reentrant covers for OS subroutines
303 Since the system subroutines are used by other library routines that
304 require reentrancy, @file{libc.a} provides cover routines (for example,
305 the reentrant version of @code{fork} is @code{_fork_r}). These cover
306 routines are consistent with the other reentrant subroutines in this
307 library, and achieve reentrancy by using a reserved global data block
308 (@pxref{Reentrancy,,Reentrancy}).
310 @c FIXME!!! The following ignored text specifies how this section ought
311 @c to work; however, both standalone info and Emacs info mode fail when
312 @c confronted with nodes beginning `_' as of 24may93. Restore when Info
313 @c readers fixed!
314 @ignore
315 @menu
316 * _open_r:: Reentrant version of open
317 * _close_r:: Reentrant version of close
318 * _lseek_r:: Reentrant version of lseek
319 * _read_r:: Reentrant version of read
320 * _write_r:: Reentrant version of write
321 * _link_r:: Reentrant version of link
322 * _unlink_r:: Reentrant version of unlink
323 * _stat_r:: Reentrant version of stat
324 * _fstat_r:: Reentrant version of fstat
325 * _sbrk_r:: Reentrant version of sbrk
326 * _fork_r:: Reentrant version of fork
327 * _wait_r:: Reentrant version of wait
328 @end menu
330 @down
331 @include reent/filer.def
332 @include reent/execr.def
333 @include reent/statr.def
334 @include reent/fstatr.def
335 @include reent/linkr.def
336 @include reent/unlinkr.def
337 @include reent/sbrkr.def
339 @end ignore
341 @ftable @code
342 @item _open_r
343 A reentrant version of @code{open}. It takes a pointer
344 to the global data block, which holds @code{errno}.
346 @example
347 int _open_r(void *@var{reent},
348 const char *@var{file}, int @var{flags}, int @var{mode});
349 @end example
351 @ifset STDIO64
352 @item _open64_r
353 A reentrant version of @code{open64}. It takes a pointer
354 to the global data block, which holds @code{errno}.
356 @example
357 int _open64_r(void *@var{reent},
358 const char *@var{file}, int @var{flags}, int @var{mode});
359 @end example
360 @end ifset
362 @item _close_r
363 A reentrant version of @code{close}. It takes a pointer to the global
364 data block, which holds @code{errno}.
366 @example
367 int _close_r(void *@var{reent}, int @var{fd});
368 @end example
370 @item _lseek_r
371 A reentrant version of @code{lseek}. It takes a pointer to the global
372 data block, which holds @code{errno}.
374 @example
375 off_t _lseek_r(void *@var{reent},
376 int @var{fd}, off_t @var{pos}, int @var{whence});
377 @end example
379 @ifset STDIO64
380 @item _lseek64_r
381 A reentrant version of @code{lseek64}. It takes a pointer to the global
382 data block, which holds @code{errno}.
384 @example
385 off_t _lseek64_r(void *@var{reent},
386 int @var{fd}, off_t @var{pos}, int @var{whence});
387 @end example
388 @end ifset
390 @item _read_r
391 A reentrant version of @code{read}. It takes a pointer to the global
392 data block, which holds @code{errno}.
394 @example
395 long _read_r(void *@var{reent},
396 int @var{fd}, void *@var{buf}, size_t @var{cnt});
397 @end example
399 @item _write_r
400 A reentrant version of @code{write}. It takes a pointer to the global
401 data block, which holds @code{errno}.
403 @example
404 long _write_r(void *@var{reent},
405 int @var{fd}, const void *@var{buf}, size_t @var{cnt});
406 @end example
408 @item _fork_r
409 A reentrant version of @code{fork}. It takes a pointer to the global
410 data block, which holds @code{errno}.
412 @example
413 int _fork_r(void *@var{reent});
414 @end example
416 @item _wait_r
417 A reentrant version of @code{wait}. It takes a pointer to the global
418 data block, which holds @code{errno}.
420 @example
421 int _wait_r(void *@var{reent}, int *@var{status});
422 @end example
424 @item _stat_r
425 A reentrant version of @code{stat}. It takes a pointer to the global
426 data block, which holds @code{errno}.
428 @example
429 int _stat_r(void *@var{reent},
430 const char *@var{file}, struct stat *@var{pstat});
431 @end example
433 @item _fstat_r
434 A reentrant version of @code{fstat}. It takes a pointer to the global
435 data block, which holds @code{errno}.
437 @example
438 int _fstat_r(void *@var{reent},
439 int @var{fd}, struct stat *@var{pstat});
440 @end example
442 @ifset STDIO64
443 @item _fstat64_r
444 A reentrant version of @code{fstat64}. It takes a pointer to the global
445 data block, which holds @code{errno}.
447 @example
448 int _fstat64_r(void *@var{reent},
449 int @var{fd}, struct stat *@var{pstat});
450 @end example
451 @end ifset
453 @item _link_r
454 A reentrant version of @code{link}. It takes a pointer to the global
455 data block, which holds @code{errno}.
457 @example
458 int _link_r(void *@var{reent},
459 const char *@var{old}, const char *@var{new});
460 @end example
462 @item _unlink_r
463 A reentrant version of @code{unlink}. It takes a pointer to the global
464 data block, which holds @code{errno}.
466 @example
467 int _unlink_r(void *@var{reent}, const char *@var{file});
468 @end example
470 @item _sbrk_r
471 A reentrant version of @code{sbrk}. It takes a pointer to the global
472 data block, which holds @code{errno}.
474 @example
475 char *_sbrk_r(void *@var{reent}, size_t @var{incr});
476 @end example
477 @end ftable