exec 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. .TH EXEC 2
  2. .SH NAME
  3. exec, execl, execac, _privates, _nprivates, _tos \- execute a file
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .nf
  10. .B
  11. void* exec(char *name, char* argv[])
  12. .PP
  13. .B
  14. void* execl(char *name, ...)
  15. .PP
  16. .B
  17. void* execac(int core, char *name, char* argv[])
  18. .PP
  19. .B
  20. void **_privates;
  21. .PP
  22. .B
  23. int _nprivates;
  24. .PP
  25. .B
  26. #include <tos.h>
  27. .PP
  28. .ft L
  29. typedef struct Tos Tos;
  30. struct Tos {
  31. struct { ... } prof; /* profiling data */
  32. uvlong cyclefreq; /* cycle clock frequency */
  33. vlong kcycles; /* kernel cycles */
  34. vlong pcycles; /* process cycles (kernel + user) */
  35. ulong pid; /* process id */
  36. ulong clock; /* profiling clock */
  37. /* top of stack is here */
  38. };
  39. .PP
  40. .B
  41. extern Tos *_tos;
  42. .fi
  43. .SH DESCRIPTION
  44. .I Exec
  45. and
  46. .I execl
  47. overlay the calling process with the named file, then
  48. transfer to the entry point of the image of the file.
  49. .PP
  50. .I Name
  51. points to the name of the file
  52. to be executed; it must not be a directory, and the permissions
  53. must allow the current user to execute it
  54. (see
  55. .IR stat (2)).
  56. It should also be a valid binary image, as defined in the
  57. .IR a.out (6)
  58. for the current machine architecture,
  59. or a shell script
  60. (see
  61. .IR rc (1)).
  62. The first line of a
  63. shell script must begin with
  64. .L #!
  65. followed by the name of the program to interpret the file
  66. and any initial arguments to that program, for example
  67. .IP
  68. .EX
  69. #!/bin/rc
  70. ls | mc
  71. .EE
  72. .PP
  73. When a C program is executed,
  74. it is called as follows:
  75. .IP
  76. .EX
  77. void main(int argc, char *argv[])
  78. .EE
  79. .PP
  80. .I Argv
  81. is a copy of the array of argument pointers passed to
  82. .IR exec ;
  83. that array must end in a null pointer, and
  84. .I argc
  85. is the number of elements before the null pointer.
  86. By convention, the first argument should be the name of
  87. the program to be executed.
  88. .I Execl
  89. is like
  90. .I exec
  91. except that
  92. .I argv
  93. will be an array of the parameters that follow
  94. .I name
  95. in the call. The last argument to
  96. .I execl
  97. must be a null pointer.
  98. .PP
  99. .I Execac
  100. is like
  101. .I exec
  102. except that it moves the process to an AC. When
  103. .I core
  104. is zero, the process is allocated to a TC. When
  105. .I core
  106. is positive, the process is allocated to that particular core number
  107. (using an AC role). When
  108. .I core
  109. is less than zero, the kernel picks a suitable core (with the AC role)
  110. for the process.
  111. .PP
  112. For a file beginning
  113. .BR #! ,
  114. the arguments passed to the program
  115. .RB ( /bin/rc
  116. in the example above) will be the name of the file being
  117. executed, any arguments on the
  118. .B #!
  119. line, the name of the file again,
  120. and finally the second and subsequent arguments given to the original
  121. .I exec
  122. call.
  123. The result honors the two conventions of a program accepting as argument
  124. a file to be interpreted and
  125. .B argv[0]
  126. naming the file being
  127. executed.
  128. .PP
  129. Most attributes of the calling process are carried
  130. into the result; in particular,
  131. files remain open across
  132. .I exec
  133. (except those opened with
  134. .B OCEXEC
  135. OR'd
  136. into the open mode; see
  137. .IR open (2));
  138. and the working directory and environment
  139. (see
  140. .IR env (3))
  141. remain the same.
  142. However, a newly
  143. .I exec'ed
  144. process has no notification handler
  145. (see
  146. .IR notify (2)).
  147. .PP
  148. The global cell
  149. .B _privates
  150. points to an array of
  151. .B _nprivates
  152. elements of per-process private data.
  153. This storage is private for each process, even if the processes share data segments.
  154. .PP
  155. When the new program begins, the global pointer
  156. .B _tos
  157. is set to the address of a structure
  158. that holds information
  159. allowing accurate time keeping and clock reading in user space.
  160. These data are updated by the kernel during of the life of the process,
  161. including across
  162. .IR rfork s
  163. and
  164. .IR exec s.
  165. If there is a user-space accessible fast clock (a processor
  166. cycle counter),
  167. .B cyclefreq
  168. will be set to its frequency in Hz.
  169. .B Kcycles
  170. .RB ( pcycles )
  171. counts the number of cycles
  172. this process has spent in kernel mode
  173. (kernel and user mode).
  174. .B Pid
  175. is the current process's id.
  176. .B Clock
  177. is the user-profiling clock (see
  178. .IR prof (1)).
  179. Its time is measured in milliseconds but is updated at
  180. a system-dependent lower rate.
  181. This clock is typically used by the profiler but is available
  182. to all programs.
  183. .PP
  184. The above conventions apply to C programs; the raw system
  185. interface to the new image is as follows:
  186. the word pointed to by the stack pointer is
  187. .BR argc ;
  188. the words beyond that are the zeroth and subsequent elements
  189. of
  190. .BR argv ,
  191. followed by a terminating null pointer; and
  192. the return register (e.g.
  193. .B R0
  194. on the 68020) contains the address of the clock information.
  195. .SH SOURCE
  196. .B /sys/src/libc/9syscall
  197. .br
  198. .B /sys/src/libc/port/execl.c
  199. .SH SEE ALSO
  200. .IR prof (1),
  201. .IR intro (2),
  202. .IR stat (2)
  203. .SH DIAGNOSTICS
  204. If these functions fail, they return and set
  205. .IR errstr .
  206. There can be no return to the calling process from a successful
  207. .I exec
  208. or
  209. .IR execl ;
  210. the calling image is lost.
  211. .SH BUGS
  212. There is a large but finite limit on the size of an argment list,
  213. typically around 409,600 bytes.
  214. The kernel constant
  215. .B TSTKSIZ
  216. controls this.