You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
11KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>Stubs (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="Stubs (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="Stubs (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Syscalls.html#Syscalls" rel="up" title="Syscalls">
  16. <link href="Reentrant-Syscalls.html#Reentrant-Syscalls" rel="next" title="Reentrant Syscalls">
  17. <link href="Syscalls.html#Syscalls" rel="prev" title="Syscalls">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="Stubs"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="Reentrant-Syscalls.html#Reentrant-Syscalls" accesskey="n" rel="next">Reentrant Syscalls</a>, Up: <a href="Syscalls.html#Syscalls" accesskey="u" rel="up">Syscalls</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="Definitions-for-OS-interface"></a>
  54. <h3 class="section">13.1 Definitions for OS interface</h3>
  55. <a name="index-stubs"></a>
  56. <a name="index-subroutines-for-OS-interface"></a>
  57. <a name="index-OS-interface-subroutines"></a>
  58. <p>This is the complete set of system definitions (primarily subroutines)
  59. required; the examples shown implement the minimal functionality
  60. required to allow <code>libc</code> to link, and fail gracefully where OS
  61. services are not available.
  62. </p>
  63. <p>Graceful failure is permitted by returning an error code. A minor
  64. complication arises here: the C library must be compatible with
  65. development environments that supply fully functional versions of these
  66. subroutines. Such environments usually return error codes in a global
  67. <code>errno</code>. However, the Red Hat newlib C library provides a <em>macro</em>
  68. definition for <code>errno</code> in the header file <samp>errno.h</samp>, as part
  69. of its support for reentrant routines (see <a href="Reentrancy.html#Reentrancy">Reentrancy</a>).
  70. </p>
  71. <a name="index-errno-global-vs-macro"></a>
  72. <p>The bridge between these two interpretations of <code>errno</code> is
  73. straightforward: the C library routines with OS interface calls
  74. capture the <code>errno</code> values returned globally, and record them in
  75. the appropriate field of the reentrancy structure (so that you can query
  76. them using the <code>errno</code> macro from <samp>errno.h</samp>).
  77. </p>
  78. <p>This mechanism becomes visible when you write stub routines for OS
  79. interfaces. You must include <samp>errno.h</samp>, then disable the macro,
  80. like this:
  81. </p>
  82. <div class="example">
  83. <pre class="example">#include &lt;errno.h&gt;
  84. #undef errno
  85. extern int errno;
  86. </pre></div>
  87. <p>The examples in this chapter include this treatment of <code>errno</code>.
  88. </p>
  89. <dl compact="compact">
  90. <dt><code>_exit</code>
  91. <a name="index-_005fexit"></a>
  92. </dt>
  93. <dd><p>Exit a program without cleaning up files. If your system doesn&rsquo;t
  94. provide this, it is best to avoid linking with subroutines that require
  95. it (<code>exit</code>, <code>system</code>).
  96. </p>
  97. </dd>
  98. <dt><code>close</code>
  99. <a name="index-close"></a>
  100. </dt>
  101. <dd><p>Close a file. Minimal implementation:
  102. </p>
  103. <div class="example">
  104. <pre class="example">int close(int file) {
  105. return -1;
  106. }
  107. </pre></div>
  108. </dd>
  109. <dt><code>environ</code>
  110. <a name="index-environ-1"></a>
  111. </dt>
  112. <dd><p>A pointer to a list of environment variables and their values. For a
  113. minimal environment, this empty list is adequate:
  114. </p>
  115. <div class="example">
  116. <pre class="example">char *__env[1] = { 0 };
  117. char **environ = __env;
  118. </pre></div>
  119. </dd>
  120. <dt><code>execve</code>
  121. <a name="index-execve"></a>
  122. </dt>
  123. <dd><p>Transfer control to a new process. Minimal implementation (for a system
  124. without processes):
  125. </p>
  126. <div class="example">
  127. <pre class="example">#include &lt;errno.h&gt;
  128. #undef errno
  129. extern int errno;
  130. int execve(char *name, char **argv, char **env) {
  131. errno = ENOMEM;
  132. return -1;
  133. }
  134. </pre></div>
  135. </dd>
  136. <dt><code>fork</code>
  137. <a name="index-fork"></a>
  138. </dt>
  139. <dd><p>Create a new process. Minimal implementation (for a system without processes):
  140. </p>
  141. <div class="example">
  142. <pre class="example">#include &lt;errno.h&gt;
  143. #undef errno
  144. extern int errno;
  145. int fork(void) {
  146. errno = EAGAIN;
  147. return -1;
  148. }
  149. </pre></div>
  150. </dd>
  151. <dt><code>fstat</code>
  152. <a name="index-fstat"></a>
  153. </dt>
  154. <dd><p>Status of an open file. For consistency with other minimal
  155. implementations in these examples, all files are regarded as character
  156. special devices. The <samp>sys/stat.h</samp> header file required is
  157. distributed in the <samp>include</samp> subdirectory for this C library.
  158. </p>
  159. <div class="example">
  160. <pre class="example">#include &lt;sys/stat.h&gt;
  161. int fstat(int file, struct stat *st) {
  162. st-&gt;st_mode = S_IFCHR;
  163. return 0;
  164. }
  165. </pre></div>
  166. </dd>
  167. <dt><code>getpid</code>
  168. <a name="index-getpid"></a>
  169. </dt>
  170. <dd><p>Process-ID; this is sometimes used to generate strings unlikely to
  171. conflict with other processes. Minimal implementation, for a system
  172. without processes:
  173. </p>
  174. <div class="example">
  175. <pre class="example">int getpid(void) {
  176. return 1;
  177. }
  178. </pre></div>
  179. </dd>
  180. <dt><code>isatty</code>
  181. <a name="index-isatty"></a>
  182. </dt>
  183. <dd><p>Query whether output stream is a terminal. For consistency with the
  184. other minimal implementations, which only support output to
  185. <code>stdout</code>, this minimal implementation is suggested:
  186. </p>
  187. <div class="example">
  188. <pre class="example">int isatty(int file) {
  189. return 1;
  190. }
  191. </pre></div>
  192. </dd>
  193. <dt><code>kill</code>
  194. <a name="index-kill"></a>
  195. </dt>
  196. <dd><p>Send a signal. Minimal implementation:
  197. </p>
  198. <div class="example">
  199. <pre class="example">#include &lt;errno.h&gt;
  200. #undef errno
  201. extern int errno;
  202. int kill(int pid, int sig) {
  203. errno = EINVAL;
  204. return -1;
  205. }
  206. </pre></div>
  207. </dd>
  208. <dt><code>link</code>
  209. <a name="index-link"></a>
  210. </dt>
  211. <dd><p>Establish a new name for an existing file. Minimal implementation:
  212. </p>
  213. <div class="example">
  214. <pre class="example">#include &lt;errno.h&gt;
  215. #undef errno
  216. extern int errno;
  217. int link(char *old, char *new) {
  218. errno = EMLINK;
  219. return -1;
  220. }
  221. </pre></div>
  222. </dd>
  223. <dt><code>lseek</code>
  224. <a name="index-lseek"></a>
  225. </dt>
  226. <dd><p>Set position in a file. Minimal implementation:
  227. </p>
  228. <div class="example">
  229. <pre class="example">int lseek(int file, int ptr, int dir) {
  230. return 0;
  231. }
  232. </pre></div>
  233. </dd>
  234. <dt><code>open</code>
  235. <a name="index-open"></a>
  236. </dt>
  237. <dd><p>Open a file. Minimal implementation:
  238. </p>
  239. <div class="example">
  240. <pre class="example">int open(const char *name, int flags, int mode) {
  241. return -1;
  242. }
  243. </pre></div>
  244. </dd>
  245. <dt><code>read</code>
  246. <a name="index-read"></a>
  247. </dt>
  248. <dd><p>Read from a file. Minimal implementation:
  249. </p>
  250. <div class="example">
  251. <pre class="example">int read(int file, char *ptr, int len) {
  252. return 0;
  253. }
  254. </pre></div>
  255. </dd>
  256. <dt><code>sbrk</code>
  257. <a name="index-sbrk"></a>
  258. </dt>
  259. <dd><p>Increase program data space. As <code>malloc</code> and related functions
  260. depend on this, it is useful to have a working implementation. The
  261. following suffices for a standalone system; it exploits the symbol
  262. <code>_end</code> automatically defined by the GNU linker.
  263. </p>
  264. <div class="example">
  265. <pre class="example">caddr_t sbrk(int incr) {
  266. extern char _end; /* <span class="roman">Defined by the linker</span> */
  267. static char *heap_end;
  268. char *prev_heap_end;
  269. if (heap_end == 0) {
  270. heap_end = &amp;_end;
  271. }
  272. prev_heap_end = heap_end;
  273. if (heap_end + incr &gt; stack_ptr) {
  274. write (1, &quot;Heap and stack collision\n&quot;, 25);
  275. abort ();
  276. }
  277. heap_end += incr;
  278. return (caddr_t) prev_heap_end;
  279. }
  280. </pre></div>
  281. </dd>
  282. <dt><code>stat</code>
  283. <a name="index-stat"></a>
  284. </dt>
  285. <dd><p>Status of a file (by name). Minimal implementation:
  286. </p>
  287. <div class="example">
  288. <pre class="example">int stat(char *file, struct stat *st) {
  289. st-&gt;st_mode = S_IFCHR;
  290. return 0;
  291. }
  292. </pre></div>
  293. </dd>
  294. <dt><code>times</code>
  295. <a name="index-times"></a>
  296. </dt>
  297. <dd><p>Timing information for current process. Minimal implementation:
  298. </p>
  299. <div class="example">
  300. <pre class="example">int times(struct tms *buf) {
  301. return -1;
  302. }
  303. </pre></div>
  304. </dd>
  305. <dt><code>unlink</code>
  306. <a name="index-unlink"></a>
  307. </dt>
  308. <dd><p>Remove a file&rsquo;s directory entry. Minimal implementation:
  309. </p>
  310. <div class="example">
  311. <pre class="example">#include &lt;errno.h&gt;
  312. #undef errno
  313. extern int errno;
  314. int unlink(char *name) {
  315. errno = ENOENT;
  316. return -1;
  317. }
  318. </pre></div>
  319. </dd>
  320. <dt><code>wait</code>
  321. <a name="index-wait"></a>
  322. </dt>
  323. <dd><p>Wait for a child process. Minimal implementation:
  324. </p><div class="example">
  325. <pre class="example">#include &lt;errno.h&gt;
  326. #undef errno
  327. extern int errno;
  328. int wait(int *status) {
  329. errno = ECHILD;
  330. return -1;
  331. }
  332. </pre></div>
  333. </dd>
  334. <dt><code>write</code>
  335. <a name="index-write"></a>
  336. </dt>
  337. <dd><p>Write to a file. <samp>libc</samp> subroutines will use this
  338. system routine for output to all files, <em>including</em>
  339. <code>stdout</code>&mdash;so if you need to generate any output, for example to a
  340. serial port for debugging, you should make your minimal <code>write</code>
  341. capable of doing this. The following minimal implementation is an
  342. incomplete example; it relies on a <code>outbyte</code> subroutine (not
  343. shown; typically, you must write this in assembler from examples
  344. provided by your hardware manufacturer) to actually perform the output.
  345. </p>
  346. <div class="example">
  347. <pre class="example">int write(int file, char *ptr, int len) {
  348. int todo;
  349. for (todo = 0; todo &lt; len; todo++) {
  350. outbyte (*ptr++);
  351. }
  352. return len;
  353. }
  354. </pre></div>
  355. </dd>
  356. </dl>
  357. <hr>
  358. <div class="header">
  359. <p>
  360. Next: <a href="Reentrant-Syscalls.html#Reentrant-Syscalls" accesskey="n" rel="next">Reentrant Syscalls</a>, Up: <a href="Syscalls.html#Syscalls" accesskey="u" rel="up">Syscalls</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  361. </div>
  362. </body>
  363. </html>