posix.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451
  1. /* Copyright 1995-2014,2016-2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <sys/types.h>
  25. #include <uniconv.h>
  26. #include <unistd.h>
  27. #ifdef HAVE_SCHED_H
  28. # include <sched.h>
  29. #endif
  30. #ifdef TIME_WITH_SYS_TIME
  31. # include <sys/time.h>
  32. # include <time.h>
  33. #else
  34. # if HAVE_SYS_TIME_H
  35. # include <sys/time.h>
  36. # else
  37. # include <time.h>
  38. # endif
  39. #endif
  40. #ifdef LIBC_H_WITH_UNISTD_H
  41. # include <libc.h>
  42. #endif
  43. #ifdef HAVE_PWD_H
  44. # include <pwd.h>
  45. #endif
  46. #ifdef HAVE_IO_H
  47. # include <io.h>
  48. #endif
  49. #ifdef __MINGW32__
  50. # include "posix-w32.h"
  51. #endif
  52. #include "async.h"
  53. #include "bitvectors.h"
  54. #include "dynwind.h"
  55. #include "extensions.h"
  56. #include "feature.h"
  57. #include "finalizers.h"
  58. #include "fports.h"
  59. #include "gettext.h"
  60. #include "gsubr.h"
  61. #include "list.h"
  62. #include "modules.h"
  63. #include "numbers.h"
  64. #include "pairs.h"
  65. #include "scmsigs.h"
  66. #include "srfi-13.h"
  67. #include "srfi-14.h"
  68. #include "strings.h"
  69. #include "symbols.h"
  70. #include "syscalls.h"
  71. #include "threads.h"
  72. #include "values.h"
  73. #include "vectors.h"
  74. #include "posix.h"
  75. #if HAVE_SYS_WAIT_H
  76. # include <sys/wait.h>
  77. #endif
  78. #ifndef WEXITSTATUS
  79. # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
  80. #endif
  81. #ifndef WIFEXITED
  82. # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
  83. #endif
  84. #include <signal.h>
  85. #ifdef HAVE_GRP_H
  86. #include <grp.h>
  87. #endif
  88. #ifdef HAVE_SYS_UTSNAME_H
  89. #include <sys/utsname.h>
  90. #endif
  91. #ifdef HAVE_SETLOCALE
  92. #include <locale.h>
  93. #endif
  94. #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L)
  95. # define USE_GNU_LOCALE_API
  96. #endif
  97. #if (defined USE_GNU_LOCALE_API) && (defined HAVE_XLOCALE_H)
  98. # include <xlocale.h>
  99. #endif
  100. #ifdef HAVE_CRYPT_H
  101. # include <crypt.h>
  102. #endif
  103. #ifdef HAVE_NETDB_H
  104. #include <netdb.h> /* for MAXHOSTNAMELEN on Solaris */
  105. #endif
  106. #ifdef HAVE_SYS_PARAM_H
  107. #include <sys/param.h> /* for MAXHOSTNAMELEN */
  108. #endif
  109. #if HAVE_SYS_RESOURCE_H
  110. # include <sys/resource.h>
  111. #endif
  112. #include <sys/file.h> /* from Gnulib */
  113. /* Some Unix systems don't define these. CPP hair is dangerous, but
  114. this seems safe enough... */
  115. #ifndef R_OK
  116. #define R_OK 4
  117. #endif
  118. #ifndef W_OK
  119. #define W_OK 2
  120. #endif
  121. #ifndef X_OK
  122. #define X_OK 1
  123. #endif
  124. #ifndef F_OK
  125. #define F_OK 0
  126. #endif
  127. /* No prototype for this on Solaris 10. The man page says it's in
  128. <unistd.h> ... but it lies. */
  129. #if ! HAVE_DECL_SETHOSTNAME
  130. int sethostname (char *name, size_t namelen);
  131. #endif
  132. #if defined HAVE_GETLOGIN && !HAVE_DECL_GETLOGIN
  133. /* MinGW doesn't supply this decl; see
  134. http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00030.html for more
  135. details. */
  136. char *getlogin (void);
  137. #endif
  138. /* On NextStep, <utime.h> doesn't define struct utime, unless we
  139. #define _POSIX_SOURCE before #including it. I think this is less
  140. of a kludge than defining struct utimbuf ourselves. */
  141. #ifdef UTIMBUF_NEEDS_POSIX
  142. #define _POSIX_SOURCE
  143. #endif
  144. #ifdef HAVE_SYS_UTIME_H
  145. #include <sys/utime.h>
  146. #endif
  147. #ifdef HAVE_UTIME_H
  148. #include <utime.h>
  149. #endif
  150. /* Please don't add any more #includes or #defines here. The hack
  151. above means that _POSIX_SOURCE may be #defined, which will
  152. encourage header files to do strange things.
  153. FIXME: Maybe should undef _POSIX_SOURCE after it's done its job.
  154. FIXME: Probably should do all the includes first, then all the fallback
  155. declarations and defines, in case things are not in the header we
  156. imagine. */
  157. /* Two often used patterns
  158. */
  159. #define WITH_STRING(str,cstr,code) \
  160. do { \
  161. char *cstr = scm_to_locale_string (str); \
  162. code; \
  163. free (cstr); \
  164. } while (0)
  165. #define STRING_SYSCALL(str,cstr,code) \
  166. do { \
  167. int eno; \
  168. char *cstr = scm_to_locale_string (str); \
  169. SCM_SYSCALL (code); \
  170. eno = errno; free (cstr); errno = eno; \
  171. } while (0)
  172. SCM_SYMBOL (sym_read_pipe, "read pipe");
  173. SCM_SYMBOL (sym_write_pipe, "write pipe");
  174. SCM_DEFINE (scm_pipe, "pipe", 0, 0, 0,
  175. (),
  176. "Return a newly created pipe: a pair of ports which are linked\n"
  177. "together on the local machine. The @emph{car} is the input\n"
  178. "port and the @emph{cdr} is the output port. Data written (and\n"
  179. "flushed) to the output port can be read from the input port.\n"
  180. "Pipes are commonly used for communication with a newly forked\n"
  181. "child process. The need to flush the output port can be\n"
  182. "avoided by making it unbuffered using @code{setvbuf}.\n"
  183. "\n"
  184. "Writes occur atomically provided the size of the data in bytes\n"
  185. "is not greater than the value of @code{PIPE_BUF}. Note that\n"
  186. "the output port is likely to block if too much data (typically\n"
  187. "equal to @code{PIPE_BUF}) has been written but not yet read\n"
  188. "from the input port.")
  189. #define FUNC_NAME s_scm_pipe
  190. {
  191. int fd[2], rv;
  192. SCM p_rd, p_wt;
  193. rv = pipe (fd);
  194. if (rv)
  195. SCM_SYSERROR;
  196. p_rd = scm_i_fdes_to_port (fd[0], scm_mode_bits ("r"), sym_read_pipe,
  197. SCM_FPORT_OPTION_NOT_SEEKABLE);
  198. p_wt = scm_i_fdes_to_port (fd[1], scm_mode_bits ("w"), sym_write_pipe,
  199. SCM_FPORT_OPTION_NOT_SEEKABLE);
  200. return scm_cons (p_rd, p_wt);
  201. }
  202. #undef FUNC_NAME
  203. #ifdef HAVE_GETGROUPS
  204. SCM_DEFINE (scm_getgroups, "getgroups", 0, 0, 0,
  205. (),
  206. "Return a vector of integers representing the current\n"
  207. "supplementary group IDs.")
  208. #define FUNC_NAME s_scm_getgroups
  209. {
  210. SCM result;
  211. int ngroups;
  212. size_t size;
  213. GETGROUPS_T *groups;
  214. ngroups = getgroups (0, NULL);
  215. if (ngroups < 0)
  216. SCM_SYSERROR;
  217. else if (ngroups == 0)
  218. return scm_c_make_vector (0, SCM_BOOL_F);
  219. size = ngroups * sizeof (GETGROUPS_T);
  220. groups = scm_malloc (size);
  221. ngroups = getgroups (ngroups, groups);
  222. result = scm_c_make_vector (ngroups, SCM_BOOL_F);
  223. while (--ngroups >= 0)
  224. SCM_SIMPLE_VECTOR_SET (result, ngroups, scm_from_ulong (groups[ngroups]));
  225. free (groups);
  226. return result;
  227. }
  228. #undef FUNC_NAME
  229. #endif
  230. #ifdef HAVE_SETGROUPS
  231. SCM_DEFINE (scm_setgroups, "setgroups", 1, 0, 0,
  232. (SCM group_vec),
  233. "Set the current set of supplementary group IDs to the integers\n"
  234. "in the given vector @var{group_vec}. The return value is\n"
  235. "unspecified.\n"
  236. "\n"
  237. "Generally only the superuser can set the process group IDs.")
  238. #define FUNC_NAME s_scm_setgroups
  239. {
  240. size_t ngroups;
  241. size_t size;
  242. size_t i;
  243. int result;
  244. int save_errno;
  245. GETGROUPS_T *groups;
  246. SCM_VALIDATE_VECTOR (SCM_ARG1, group_vec);
  247. ngroups = SCM_SIMPLE_VECTOR_LENGTH (group_vec);
  248. /* validate before allocating, so we don't have to worry about leaks */
  249. for (i = 0; i < ngroups; i++)
  250. {
  251. unsigned long ulong_gid;
  252. GETGROUPS_T gid;
  253. SCM_VALIDATE_ULONG_COPY (1, SCM_SIMPLE_VECTOR_REF (group_vec, i),
  254. ulong_gid);
  255. gid = ulong_gid;
  256. if (gid != ulong_gid)
  257. SCM_OUT_OF_RANGE (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
  258. }
  259. size = ngroups * sizeof (GETGROUPS_T);
  260. if (size / sizeof (GETGROUPS_T) != ngroups)
  261. SCM_OUT_OF_RANGE (SCM_ARG1, scm_from_int (ngroups));
  262. groups = scm_malloc (size);
  263. for(i = 0; i < ngroups; i++)
  264. groups [i] = SCM_NUM2ULONG (1, SCM_SIMPLE_VECTOR_REF (group_vec, i));
  265. result = setgroups (ngroups, groups);
  266. save_errno = errno; /* don't let free() touch errno */
  267. free (groups);
  268. errno = save_errno;
  269. if (result < 0)
  270. SCM_SYSERROR;
  271. return SCM_UNSPECIFIED;
  272. }
  273. #undef FUNC_NAME
  274. #endif
  275. #ifdef HAVE_GETPWENT
  276. SCM_DEFINE (scm_getpwuid, "getpw", 0, 1, 0,
  277. (SCM user),
  278. "Look up an entry in the user database. @var{user} can be an\n"
  279. "integer, a string, or omitted, giving the behaviour of\n"
  280. "@code{getpwuid}, @code{getpwnam} or @code{getpwent}\n"
  281. "respectively.")
  282. #define FUNC_NAME s_scm_getpwuid
  283. {
  284. struct passwd *entry;
  285. SCM result = scm_c_make_vector (7, SCM_UNSPECIFIED);
  286. if (SCM_UNBNDP (user) || scm_is_false (user))
  287. {
  288. SCM_SYSCALL (entry = getpwent ());
  289. if (! entry)
  290. {
  291. return SCM_BOOL_F;
  292. }
  293. }
  294. else if (scm_is_integer (user))
  295. {
  296. entry = getpwuid (scm_to_int (user));
  297. }
  298. else
  299. {
  300. WITH_STRING (user, c_user,
  301. entry = getpwnam (c_user));
  302. }
  303. if (!entry)
  304. SCM_MISC_ERROR ("entry not found", SCM_EOL);
  305. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->pw_name));
  306. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->pw_passwd));
  307. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->pw_uid));
  308. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_ulong (entry->pw_gid));
  309. SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (entry->pw_gecos));
  310. if (!entry->pw_dir)
  311. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (""));
  312. else
  313. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (entry->pw_dir));
  314. if (!entry->pw_shell)
  315. SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (""));
  316. else
  317. SCM_SIMPLE_VECTOR_SET(result, 6, scm_from_locale_string (entry->pw_shell));
  318. return result;
  319. }
  320. #undef FUNC_NAME
  321. #endif /* HAVE_GETPWENT */
  322. #ifdef HAVE_SETPWENT
  323. SCM_DEFINE (scm_setpwent, "setpw", 0, 1, 0,
  324. (SCM arg),
  325. "If called with a true argument, initialize or reset the password data\n"
  326. "stream. Otherwise, close the stream. The @code{setpwent} and\n"
  327. "@code{endpwent} procedures are implemented on top of this.")
  328. #define FUNC_NAME s_scm_setpwent
  329. {
  330. if (SCM_UNBNDP (arg) || scm_is_false (arg))
  331. endpwent ();
  332. else
  333. setpwent ();
  334. return SCM_UNSPECIFIED;
  335. }
  336. #undef FUNC_NAME
  337. #endif
  338. #ifdef HAVE_GETGRENT
  339. /* Combines getgrgid and getgrnam. */
  340. SCM_DEFINE (scm_getgrgid, "getgr", 0, 1, 0,
  341. (SCM name),
  342. "Look up an entry in the group database. @var{name} can be an\n"
  343. "integer, a string, or omitted, giving the behaviour of\n"
  344. "@code{getgrgid}, @code{getgrnam} or @code{getgrent}\n"
  345. "respectively.")
  346. #define FUNC_NAME s_scm_getgrgid
  347. {
  348. struct group *entry;
  349. SCM result = scm_c_make_vector (4, SCM_UNSPECIFIED);
  350. if (SCM_UNBNDP (name) || scm_is_false (name))
  351. {
  352. SCM_SYSCALL (entry = getgrent ());
  353. if (! entry)
  354. {
  355. return SCM_BOOL_F;
  356. }
  357. }
  358. else if (scm_is_integer (name))
  359. SCM_SYSCALL (entry = getgrgid (scm_to_int (name)));
  360. else
  361. STRING_SYSCALL (name, c_name,
  362. entry = getgrnam (c_name));
  363. if (!entry)
  364. SCM_SYSERROR;
  365. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (entry->gr_name));
  366. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (entry->gr_passwd));
  367. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ulong (entry->gr_gid));
  368. SCM_SIMPLE_VECTOR_SET(result, 3, scm_makfromstrs (-1, entry->gr_mem));
  369. return result;
  370. }
  371. #undef FUNC_NAME
  372. SCM_DEFINE (scm_setgrent, "setgr", 0, 1, 0,
  373. (SCM arg),
  374. "If called with a true argument, initialize or reset the group data\n"
  375. "stream. Otherwise, close the stream. The @code{setgrent} and\n"
  376. "@code{endgrent} procedures are implemented on top of this.")
  377. #define FUNC_NAME s_scm_setgrent
  378. {
  379. if (SCM_UNBNDP (arg) || scm_is_false (arg))
  380. endgrent ();
  381. else
  382. setgrent ();
  383. return SCM_UNSPECIFIED;
  384. }
  385. #undef FUNC_NAME
  386. #endif /* HAVE_GETGRENT */
  387. #ifdef HAVE_GETRLIMIT
  388. #ifdef RLIMIT_AS
  389. SCM_SYMBOL (sym_as, "as");
  390. #endif
  391. #ifdef RLIMIT_CORE
  392. SCM_SYMBOL (sym_core, "core");
  393. #endif
  394. #ifdef RLIMIT_CPU
  395. SCM_SYMBOL (sym_cpu, "cpu");
  396. #endif
  397. #ifdef RLIMIT_DATA
  398. SCM_SYMBOL (sym_data, "data");
  399. #endif
  400. #ifdef RLIMIT_FSIZE
  401. SCM_SYMBOL (sym_fsize, "fsize");
  402. #endif
  403. #ifdef RLIMIT_MEMLOCK
  404. SCM_SYMBOL (sym_memlock, "memlock");
  405. #endif
  406. #ifdef RLIMIT_MSGQUEUE
  407. SCM_SYMBOL (sym_msgqueue, "msgqueue");
  408. #endif
  409. #ifdef RLIMIT_NICE
  410. SCM_SYMBOL (sym_nice, "nice");
  411. #endif
  412. #ifdef RLIMIT_NOFILE
  413. SCM_SYMBOL (sym_nofile, "nofile");
  414. #endif
  415. #ifdef RLIMIT_NPROC
  416. SCM_SYMBOL (sym_nproc, "nproc");
  417. #endif
  418. #ifdef RLIMIT_RSS
  419. SCM_SYMBOL (sym_rss, "rss");
  420. #endif
  421. #ifdef RLIMIT_RTPRIO
  422. SCM_SYMBOL (sym_rtprio, "rtprio");
  423. #endif
  424. #ifdef RLIMIT_RTPRIO
  425. SCM_SYMBOL (sym_rttime, "rttime");
  426. #endif
  427. #ifdef RLIMIT_SIGPENDING
  428. SCM_SYMBOL (sym_sigpending, "sigpending");
  429. #endif
  430. #ifdef RLIMIT_STACK
  431. SCM_SYMBOL (sym_stack, "stack");
  432. #endif
  433. static int
  434. scm_to_resource (SCM s, const char *func, int pos)
  435. {
  436. if (scm_is_number (s))
  437. return scm_to_int (s);
  438. SCM_ASSERT_TYPE (scm_is_symbol (s), s, pos, func, "symbol");
  439. #ifdef RLIMIT_AS
  440. if (scm_is_eq (s, sym_as))
  441. return RLIMIT_AS;
  442. #endif
  443. #ifdef RLIMIT_CORE
  444. if (scm_is_eq (s, sym_core))
  445. return RLIMIT_CORE;
  446. #endif
  447. #ifdef RLIMIT_CPU
  448. if (scm_is_eq (s, sym_cpu))
  449. return RLIMIT_CPU;
  450. #endif
  451. #ifdef RLIMIT_DATA
  452. if (scm_is_eq (s, sym_data))
  453. return RLIMIT_DATA;
  454. #endif
  455. #ifdef RLIMIT_FSIZE
  456. if (scm_is_eq (s, sym_fsize))
  457. return RLIMIT_FSIZE;
  458. #endif
  459. #ifdef RLIMIT_MEMLOCK
  460. if (scm_is_eq (s, sym_memlock))
  461. return RLIMIT_MEMLOCK;
  462. #endif
  463. #ifdef RLIMIT_MSGQUEUE
  464. if (scm_is_eq (s, sym_msgqueue))
  465. return RLIMIT_MSGQUEUE;
  466. #endif
  467. #ifdef RLIMIT_NICE
  468. if (scm_is_eq (s, sym_nice))
  469. return RLIMIT_NICE;
  470. #endif
  471. #ifdef RLIMIT_NOFILE
  472. if (scm_is_eq (s, sym_nofile))
  473. return RLIMIT_NOFILE;
  474. #endif
  475. #ifdef RLIMIT_NPROC
  476. if (scm_is_eq (s, sym_nproc))
  477. return RLIMIT_NPROC;
  478. #endif
  479. #ifdef RLIMIT_RSS
  480. if (scm_is_eq (s, sym_rss))
  481. return RLIMIT_RSS;
  482. #endif
  483. #ifdef RLIMIT_RTPRIO
  484. if (scm_is_eq (s, sym_rtprio))
  485. return RLIMIT_RTPRIO;
  486. #endif
  487. #ifdef RLIMIT_RTPRIO
  488. if (scm_is_eq (s, sym_rttime))
  489. return RLIMIT_RTPRIO;
  490. #endif
  491. #ifdef RLIMIT_SIGPENDING
  492. if (scm_is_eq (s, sym_sigpending))
  493. return RLIMIT_SIGPENDING;
  494. #endif
  495. #ifdef RLIMIT_STACK
  496. if (scm_is_eq (s, sym_stack))
  497. return RLIMIT_STACK;
  498. #endif
  499. scm_misc_error (func, "invalid rlimit resource ~A", scm_list_1 (s));
  500. return 0;
  501. }
  502. SCM_DEFINE (scm_getrlimit, "getrlimit", 1, 0, 0,
  503. (SCM resource),
  504. "Get a resource limit for this process. @var{resource} identifies the resource,\n"
  505. "either as an integer or as a symbol. For example, @code{(getrlimit 'stack)}\n"
  506. "gets the limits associated with @code{RLIMIT_STACK}.\n\n"
  507. "@code{getrlimit} returns two values, the soft and the hard limit. If no\n"
  508. "limit is set for the resource in question, the returned limit will be @code{#f}.")
  509. #define FUNC_NAME s_scm_getrlimit
  510. {
  511. int iresource;
  512. struct rlimit lim = { 0, 0 };
  513. iresource = scm_to_resource (resource, FUNC_NAME, 1);
  514. if (getrlimit (iresource, &lim) != 0)
  515. scm_syserror (FUNC_NAME);
  516. return scm_values_2 ((lim.rlim_cur == RLIM_INFINITY) ? SCM_BOOL_F
  517. : scm_from_long (lim.rlim_cur),
  518. (lim.rlim_max == RLIM_INFINITY) ? SCM_BOOL_F
  519. : scm_from_long (lim.rlim_max));
  520. }
  521. #undef FUNC_NAME
  522. #ifdef HAVE_SETRLIMIT
  523. SCM_DEFINE (scm_setrlimit, "setrlimit", 3, 0, 0,
  524. (SCM resource, SCM soft, SCM hard),
  525. "Set a resource limit for this process. @var{resource} identifies the resource,\n"
  526. "either as an integer or as a symbol. @var{soft} and @var{hard} should be integers,\n"
  527. "or @code{#f} to indicate no limit (i.e., @code{RLIM_INFINITY}).\n\n"
  528. "For example, @code{(setrlimit 'stack 150000 300000)} sets the @code{RLIMIT_STACK}\n"
  529. "limit to 150 kilobytes, with a hard limit of 300 kB.")
  530. #define FUNC_NAME s_scm_setrlimit
  531. {
  532. int iresource;
  533. struct rlimit lim = { 0, 0 };
  534. iresource = scm_to_resource (resource, FUNC_NAME, 1);
  535. lim.rlim_cur = scm_is_false (soft) ? RLIM_INFINITY : scm_to_long (soft);
  536. lim.rlim_max = scm_is_false (hard) ? RLIM_INFINITY : scm_to_long (hard);
  537. if (setrlimit (iresource, &lim) != 0)
  538. scm_syserror (FUNC_NAME);
  539. return SCM_UNSPECIFIED;
  540. }
  541. #undef FUNC_NAME
  542. #endif /* HAVE_SETRLIMIT */
  543. #endif /* HAVE_GETRLIMIT */
  544. #ifdef HAVE_KILL
  545. SCM_DEFINE (scm_kill, "kill", 2, 0, 0,
  546. (SCM pid, SCM sig),
  547. "Sends a signal to the specified process or group of processes.\n\n"
  548. "@var{pid} specifies the processes to which the signal is sent:\n\n"
  549. "@table @r\n"
  550. "@item @var{pid} greater than 0\n"
  551. "The process whose identifier is @var{pid}.\n"
  552. "@item @var{pid} equal to 0\n"
  553. "All processes in the current process group.\n"
  554. "@item @var{pid} less than -1\n"
  555. "The process group whose identifier is -@var{pid}\n"
  556. "@item @var{pid} equal to -1\n"
  557. "If the process is privileged, all processes except for some special\n"
  558. "system processes. Otherwise, all processes with the current effective\n"
  559. "user ID.\n"
  560. "@end table\n\n"
  561. "@var{sig} should be specified using a variable corresponding to\n"
  562. "the Unix symbolic name, e.g.,\n\n"
  563. "@defvar SIGHUP\n"
  564. "Hang-up signal.\n"
  565. "@end defvar\n\n"
  566. "@defvar SIGINT\n"
  567. "Interrupt signal.\n"
  568. "@end defvar")
  569. #define FUNC_NAME s_scm_kill
  570. {
  571. /* Signal values are interned in scm_init_posix(). */
  572. if (kill (scm_to_int (pid), scm_to_int (sig)) != 0)
  573. SCM_SYSERROR;
  574. return SCM_UNSPECIFIED;
  575. }
  576. #undef FUNC_NAME
  577. #endif
  578. #ifdef HAVE_WAITPID
  579. SCM_DEFINE (scm_waitpid, "waitpid", 1, 1, 0,
  580. (SCM pid, SCM options),
  581. "This procedure collects status information from a child process which\n"
  582. "has terminated or (optionally) stopped. Normally it will\n"
  583. "suspend the calling process until this can be done. If more than one\n"
  584. "child process is eligible then one will be chosen by the operating system.\n\n"
  585. "The value of @var{pid} determines the behaviour:\n\n"
  586. "@table @r\n"
  587. "@item @var{pid} greater than 0\n"
  588. "Request status information from the specified child process.\n"
  589. "@item @var{pid} equal to -1 or WAIT_ANY\n"
  590. "Request status information for any child process.\n"
  591. "@item @var{pid} equal to 0 or WAIT_MYPGRP\n"
  592. "Request status information for any child process in the current process\n"
  593. "group.\n"
  594. "@item @var{pid} less than -1\n"
  595. "Request status information for any child process whose process group ID\n"
  596. "is -@var{pid}.\n"
  597. "@end table\n\n"
  598. "The @var{options} argument, if supplied, should be the bitwise OR of the\n"
  599. "values of zero or more of the following variables:\n\n"
  600. "@defvar WNOHANG\n"
  601. "Return immediately even if there are no child processes to be collected.\n"
  602. "@end defvar\n\n"
  603. "@defvar WUNTRACED\n"
  604. "Report status information for stopped processes as well as terminated\n"
  605. "processes.\n"
  606. "@end defvar\n\n"
  607. "The return value is a pair containing:\n\n"
  608. "@enumerate\n"
  609. "@item\n"
  610. "The process ID of the child process, or 0 if @code{WNOHANG} was\n"
  611. "specified and no process was collected.\n"
  612. "@item\n"
  613. "The integer status value.\n"
  614. "@end enumerate")
  615. #define FUNC_NAME s_scm_waitpid
  616. {
  617. int i;
  618. int status;
  619. int ioptions;
  620. if (SCM_UNBNDP (options))
  621. ioptions = 0;
  622. else
  623. {
  624. /* Flags are interned in scm_init_posix. */
  625. ioptions = scm_to_int (options);
  626. }
  627. SCM_SYSCALL (i = waitpid (scm_to_int (pid), &status, ioptions));
  628. if (i == -1)
  629. SCM_SYSERROR;
  630. return scm_cons (scm_from_int (i), scm_from_int (status));
  631. }
  632. #undef FUNC_NAME
  633. #endif /* HAVE_WAITPID */
  634. #ifdef WIFEXITED
  635. SCM_DEFINE (scm_status_exit_val, "status:exit-val", 1, 0, 0,
  636. (SCM status),
  637. "Return the exit status value, as would be set if a process\n"
  638. "ended normally through a call to @code{exit} or @code{_exit},\n"
  639. "if any, otherwise @code{#f}.")
  640. #define FUNC_NAME s_scm_status_exit_val
  641. {
  642. int lstatus;
  643. /* On Ultrix, the WIF... macros assume their argument is an lvalue;
  644. go figure. */
  645. lstatus = scm_to_int (status);
  646. if (WIFEXITED (lstatus))
  647. return (scm_from_int (WEXITSTATUS (lstatus)));
  648. else
  649. return SCM_BOOL_F;
  650. }
  651. #undef FUNC_NAME
  652. #endif /* WIFEXITED */
  653. #ifdef WIFSIGNALED
  654. SCM_DEFINE (scm_status_term_sig, "status:term-sig", 1, 0, 0,
  655. (SCM status),
  656. "Return the signal number which terminated the process, if any,\n"
  657. "otherwise @code{#f}.")
  658. #define FUNC_NAME s_scm_status_term_sig
  659. {
  660. int lstatus;
  661. lstatus = scm_to_int (status);
  662. if (WIFSIGNALED (lstatus))
  663. return scm_from_int (WTERMSIG (lstatus));
  664. else
  665. return SCM_BOOL_F;
  666. }
  667. #undef FUNC_NAME
  668. #endif /* WIFSIGNALED */
  669. #ifdef WIFSTOPPED
  670. SCM_DEFINE (scm_status_stop_sig, "status:stop-sig", 1, 0, 0,
  671. (SCM status),
  672. "Return the signal number which stopped the process, if any,\n"
  673. "otherwise @code{#f}.")
  674. #define FUNC_NAME s_scm_status_stop_sig
  675. {
  676. int lstatus;
  677. lstatus = scm_to_int (status);
  678. if (WIFSTOPPED (lstatus))
  679. return scm_from_int (WSTOPSIG (lstatus));
  680. else
  681. return SCM_BOOL_F;
  682. }
  683. #undef FUNC_NAME
  684. #endif /* WIFSTOPPED */
  685. #ifdef HAVE_GETPPID
  686. SCM_DEFINE (scm_getppid, "getppid", 0, 0, 0,
  687. (),
  688. "Return an integer representing the process ID of the parent\n"
  689. "process.")
  690. #define FUNC_NAME s_scm_getppid
  691. {
  692. return scm_from_int (getppid ());
  693. }
  694. #undef FUNC_NAME
  695. #endif /* HAVE_GETPPID */
  696. #ifdef HAVE_GETUID
  697. SCM_DEFINE (scm_getuid, "getuid", 0, 0, 0,
  698. (),
  699. "Return an integer representing the current real user ID.")
  700. #define FUNC_NAME s_scm_getuid
  701. {
  702. return scm_from_int (getuid ());
  703. }
  704. #undef FUNC_NAME
  705. #endif /* HAVE_GETUID */
  706. #ifdef HAVE_GETGID
  707. SCM_DEFINE (scm_getgid, "getgid", 0, 0, 0,
  708. (),
  709. "Return an integer representing the current real group ID.")
  710. #define FUNC_NAME s_scm_getgid
  711. {
  712. return scm_from_int (getgid ());
  713. }
  714. #undef FUNC_NAME
  715. #endif /* HAVE_GETGID */
  716. #ifdef HAVE_GETUID
  717. SCM_DEFINE (scm_geteuid, "geteuid", 0, 0, 0,
  718. (),
  719. "Return an integer representing the current effective user ID.\n"
  720. "If the system does not support effective IDs, then the real ID\n"
  721. "is returned. @code{(provided? 'EIDs)} reports whether the\n"
  722. "system supports effective IDs.")
  723. #define FUNC_NAME s_scm_geteuid
  724. {
  725. #ifdef HAVE_GETEUID
  726. return scm_from_int (geteuid ());
  727. #else
  728. return scm_from_int (getuid ());
  729. #endif
  730. }
  731. #undef FUNC_NAME
  732. #endif /* HAVE_GETUID */
  733. #ifdef HAVE_GETGID
  734. SCM_DEFINE (scm_getegid, "getegid", 0, 0, 0,
  735. (),
  736. "Return an integer representing the current effective group ID.\n"
  737. "If the system does not support effective IDs, then the real ID\n"
  738. "is returned. @code{(provided? 'EIDs)} reports whether the\n"
  739. "system supports effective IDs.")
  740. #define FUNC_NAME s_scm_getegid
  741. {
  742. #ifdef HAVE_GETEUID
  743. return scm_from_int (getegid ());
  744. #else
  745. return scm_from_int (getgid ());
  746. #endif
  747. }
  748. #undef FUNC_NAME
  749. #endif /* HAVE_GETGID */
  750. #ifdef HAVE_SETUID
  751. SCM_DEFINE (scm_setuid, "setuid", 1, 0, 0,
  752. (SCM id),
  753. "Sets both the real and effective user IDs to the integer @var{id}, provided\n"
  754. "the process has appropriate privileges.\n"
  755. "The return value is unspecified.")
  756. #define FUNC_NAME s_scm_setuid
  757. {
  758. if (setuid (scm_to_int (id)) != 0)
  759. SCM_SYSERROR;
  760. return SCM_UNSPECIFIED;
  761. }
  762. #undef FUNC_NAME
  763. #endif /* HAVE_SETUID */
  764. #ifdef HAVE_SETGID
  765. SCM_DEFINE (scm_setgid, "setgid", 1, 0, 0,
  766. (SCM id),
  767. "Sets both the real and effective group IDs to the integer @var{id}, provided\n"
  768. "the process has appropriate privileges.\n"
  769. "The return value is unspecified.")
  770. #define FUNC_NAME s_scm_setgid
  771. {
  772. if (setgid (scm_to_int (id)) != 0)
  773. SCM_SYSERROR;
  774. return SCM_UNSPECIFIED;
  775. }
  776. #undef FUNC_NAME
  777. #endif /* HAVE_SETGID */
  778. #ifdef HAVE_SETUID
  779. SCM_DEFINE (scm_seteuid, "seteuid", 1, 0, 0,
  780. (SCM id),
  781. "Sets the effective user ID to the integer @var{id}, provided the process\n"
  782. "has appropriate privileges. If effective IDs are not supported, the\n"
  783. "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
  784. "system supports effective IDs.\n"
  785. "The return value is unspecified.")
  786. #define FUNC_NAME s_scm_seteuid
  787. {
  788. int rv;
  789. #ifdef HAVE_SETEUID
  790. rv = seteuid (scm_to_int (id));
  791. #else
  792. rv = setuid (scm_to_int (id));
  793. #endif
  794. if (rv != 0)
  795. SCM_SYSERROR;
  796. return SCM_UNSPECIFIED;
  797. }
  798. #undef FUNC_NAME
  799. #endif /* HAVE_SETUID */
  800. #ifdef HAVE_SETGID
  801. SCM_DEFINE (scm_setegid, "setegid", 1, 0, 0,
  802. (SCM id),
  803. "Sets the effective group ID to the integer @var{id}, provided the process\n"
  804. "has appropriate privileges. If effective IDs are not supported, the\n"
  805. "real ID is set instead -- @code{(provided? 'EIDs)} reports whether the\n"
  806. "system supports effective IDs.\n"
  807. "The return value is unspecified.")
  808. #define FUNC_NAME s_scm_setegid
  809. {
  810. int rv;
  811. #ifdef HAVE_SETEGID
  812. rv = setegid (scm_to_int (id));
  813. #else
  814. rv = setgid (scm_to_int (id));
  815. #endif
  816. if (rv != 0)
  817. SCM_SYSERROR;
  818. return SCM_UNSPECIFIED;
  819. }
  820. #undef FUNC_NAME
  821. #endif /* HAVE_SETGID */
  822. #ifdef HAVE_GETPGRP
  823. SCM_DEFINE (scm_getpgrp, "getpgrp", 0, 0, 0,
  824. (),
  825. "Return an integer representing the current process group ID.\n"
  826. "This is the POSIX definition, not BSD.")
  827. #define FUNC_NAME s_scm_getpgrp
  828. {
  829. int (*fn)();
  830. fn = (int (*) ()) getpgrp;
  831. return scm_from_int (fn (0));
  832. }
  833. #undef FUNC_NAME
  834. #endif /* HAVE_GETPGRP */
  835. #ifdef HAVE_SETPGID
  836. SCM_DEFINE (scm_setpgid, "setpgid", 2, 0, 0,
  837. (SCM pid, SCM pgid),
  838. "Move the process @var{pid} into the process group @var{pgid}. @var{pid} or\n"
  839. "@var{pgid} must be integers: they can be zero to indicate the ID of the\n"
  840. "current process.\n"
  841. "Fails on systems that do not support job control.\n"
  842. "The return value is unspecified.")
  843. #define FUNC_NAME s_scm_setpgid
  844. {
  845. /* FIXME(?): may be known as setpgrp. */
  846. if (setpgid (scm_to_int (pid), scm_to_int (pgid)) != 0)
  847. SCM_SYSERROR;
  848. return SCM_UNSPECIFIED;
  849. }
  850. #undef FUNC_NAME
  851. #endif /* HAVE_SETPGID */
  852. #ifdef HAVE_SETSID
  853. SCM_DEFINE (scm_setsid, "setsid", 0, 0, 0,
  854. (),
  855. "Creates a new session. The current process becomes the session leader\n"
  856. "and is put in a new process group. The process will be detached\n"
  857. "from its controlling terminal if it has one.\n"
  858. "The return value is an integer representing the new process group ID.")
  859. #define FUNC_NAME s_scm_setsid
  860. {
  861. pid_t sid = setsid ();
  862. if (sid == -1)
  863. SCM_SYSERROR;
  864. return SCM_UNSPECIFIED;
  865. }
  866. #undef FUNC_NAME
  867. #endif /* HAVE_SETSID */
  868. #ifdef HAVE_GETSID
  869. SCM_DEFINE (scm_getsid, "getsid", 1, 0, 0,
  870. (SCM pid),
  871. "Returns the session ID of process @var{pid}. (The session\n"
  872. "ID of a process is the process group ID of its session leader.)")
  873. #define FUNC_NAME s_scm_getsid
  874. {
  875. return scm_from_int (getsid (scm_to_int (pid)));
  876. }
  877. #undef FUNC_NAME
  878. #endif /* HAVE_GETSID */
  879. /* ttyname returns its result in a single static buffer, hence
  880. scm_i_misc_mutex for thread safety. In glibc 2.3.2 two threads
  881. continuously calling ttyname will otherwise get an overwrite quite
  882. easily.
  883. ttyname_r (when available) could be used instead of scm_i_misc_mutex, but
  884. there's probably little to be gained in either speed or parallelism. */
  885. #ifdef HAVE_TTYNAME
  886. SCM_DEFINE (scm_ttyname, "ttyname", 1, 0, 0,
  887. (SCM port),
  888. "Return a string with the name of the serial terminal device\n"
  889. "underlying @var{port}.")
  890. #define FUNC_NAME s_scm_ttyname
  891. {
  892. char *result;
  893. int fd, err;
  894. SCM ret = SCM_BOOL_F;
  895. port = SCM_COERCE_OUTPORT (port);
  896. SCM_VALIDATE_OPPORT (1, port);
  897. if (!SCM_FPORTP (port))
  898. return SCM_BOOL_F;
  899. fd = SCM_FPORT_FDES (port);
  900. scm_i_scm_pthread_mutex_lock (&scm_i_misc_mutex);
  901. SCM_SYSCALL (result = ttyname (fd));
  902. err = errno;
  903. if (result != NULL)
  904. result = strdup (result);
  905. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  906. if (!result)
  907. {
  908. errno = err;
  909. SCM_SYSERROR;
  910. }
  911. else
  912. ret = scm_take_locale_string (result);
  913. return ret;
  914. }
  915. #undef FUNC_NAME
  916. #endif /* HAVE_TTYNAME */
  917. /* For thread safety "buf" is used instead of NULL for the ctermid static
  918. buffer. Actually it's unlikely the controlling terminal will change
  919. during program execution, and indeed on glibc (2.3.2) it's always just
  920. "/dev/tty", but L_ctermid on the stack is easy and fast and guarantees
  921. safety everywhere. */
  922. #ifdef HAVE_CTERMID
  923. SCM_DEFINE (scm_ctermid, "ctermid", 0, 0, 0,
  924. (),
  925. "Return a string containing the file name of the controlling\n"
  926. "terminal for the current process.")
  927. #define FUNC_NAME s_scm_ctermid
  928. {
  929. char buf[L_ctermid];
  930. char *result = ctermid (buf);
  931. if (*result == '\0')
  932. SCM_SYSERROR;
  933. return scm_from_locale_string (result);
  934. }
  935. #undef FUNC_NAME
  936. #endif /* HAVE_CTERMID */
  937. #ifdef HAVE_TCGETPGRP
  938. SCM_DEFINE (scm_tcgetpgrp, "tcgetpgrp", 1, 0, 0,
  939. (SCM port),
  940. "Return the process group ID of the foreground process group\n"
  941. "associated with the terminal open on the file descriptor\n"
  942. "underlying @var{port}.\n"
  943. "\n"
  944. "If there is no foreground process group, the return value is a\n"
  945. "number greater than 1 that does not match the process group ID\n"
  946. "of any existing process group. This can happen if all of the\n"
  947. "processes in the job that was formerly the foreground job have\n"
  948. "terminated, and no other job has yet been moved into the\n"
  949. "foreground.")
  950. #define FUNC_NAME s_scm_tcgetpgrp
  951. {
  952. int fd;
  953. pid_t pgid;
  954. port = SCM_COERCE_OUTPORT (port);
  955. SCM_VALIDATE_OPFPORT (1, port);
  956. fd = SCM_FPORT_FDES (port);
  957. if ((pgid = tcgetpgrp (fd)) == -1)
  958. SCM_SYSERROR;
  959. return scm_from_int (pgid);
  960. }
  961. #undef FUNC_NAME
  962. #endif /* HAVE_TCGETPGRP */
  963. #ifdef HAVE_TCSETPGRP
  964. SCM_DEFINE (scm_tcsetpgrp, "tcsetpgrp", 2, 0, 0,
  965. (SCM port, SCM pgid),
  966. "Set the foreground process group ID for the terminal used by the file\n"
  967. "descriptor underlying @var{port} to the integer @var{pgid}.\n"
  968. "The calling process\n"
  969. "must be a member of the same session as @var{pgid} and must have the same\n"
  970. "controlling terminal. The return value is unspecified.")
  971. #define FUNC_NAME s_scm_tcsetpgrp
  972. {
  973. int fd;
  974. port = SCM_COERCE_OUTPORT (port);
  975. SCM_VALIDATE_OPFPORT (1, port);
  976. fd = SCM_FPORT_FDES (port);
  977. if (tcsetpgrp (fd, scm_to_int (pgid)) == -1)
  978. SCM_SYSERROR;
  979. return SCM_UNSPECIFIED;
  980. }
  981. #undef FUNC_NAME
  982. #endif /* HAVE_TCSETPGRP */
  983. SCM_DEFINE (scm_execl, "execl", 1, 0, 1,
  984. (SCM filename, SCM args),
  985. "Executes the file named by @var{filename} as a new process image.\n"
  986. "The remaining arguments are supplied to the process; from a C program\n"
  987. "they are accessible as the @code{argv} argument to @code{main}.\n"
  988. "Conventionally the first @var{arg} is the same as @var{filename}.\n"
  989. "All arguments must be strings.\n\n"
  990. "If @var{arg} is missing, @var{path} is executed with a null\n"
  991. "argument list, which may have system-dependent side-effects.\n\n"
  992. "This procedure is currently implemented using the @code{execv} system\n"
  993. "call, but we call it @code{execl} because of its Scheme calling interface.")
  994. #define FUNC_NAME s_scm_execl
  995. {
  996. char *exec_file;
  997. char **exec_argv;
  998. scm_dynwind_begin (0);
  999. exec_file = scm_to_locale_string (filename);
  1000. scm_dynwind_free (exec_file);
  1001. exec_argv = scm_i_allocate_string_pointers (args);
  1002. execv (exec_file, exec_argv);
  1003. SCM_SYSERROR;
  1004. /* not reached. */
  1005. scm_dynwind_end ();
  1006. return SCM_BOOL_F;
  1007. }
  1008. #undef FUNC_NAME
  1009. SCM_DEFINE (scm_execlp, "execlp", 1, 0, 1,
  1010. (SCM filename, SCM args),
  1011. "Similar to @code{execl}, however if\n"
  1012. "@var{filename} does not contain a slash\n"
  1013. "then the file to execute will be located by searching the\n"
  1014. "directories listed in the @code{PATH} environment variable.\n\n"
  1015. "This procedure is currently implemented using the @code{execvp} system\n"
  1016. "call, but we call it @code{execlp} because of its Scheme calling interface.")
  1017. #define FUNC_NAME s_scm_execlp
  1018. {
  1019. char *exec_file;
  1020. char **exec_argv;
  1021. scm_dynwind_begin (0);
  1022. exec_file = scm_to_locale_string (filename);
  1023. scm_dynwind_free (exec_file);
  1024. exec_argv = scm_i_allocate_string_pointers (args);
  1025. execvp (exec_file, exec_argv);
  1026. SCM_SYSERROR;
  1027. /* not reached. */
  1028. scm_dynwind_end ();
  1029. return SCM_BOOL_F;
  1030. }
  1031. #undef FUNC_NAME
  1032. /* OPTIMIZE-ME: scm_execle doesn't need malloced copies of the environment
  1033. list strings the way environ_list_to_c gives. */
  1034. SCM_DEFINE (scm_execle, "execle", 2, 0, 1,
  1035. (SCM filename, SCM env, SCM args),
  1036. "Similar to @code{execl}, but the environment of the new process is\n"
  1037. "specified by @var{env}, which must be a list of strings as returned by the\n"
  1038. "@code{environ} procedure.\n\n"
  1039. "This procedure is currently implemented using the @code{execve} system\n"
  1040. "call, but we call it @code{execle} because of its Scheme calling interface.")
  1041. #define FUNC_NAME s_scm_execle
  1042. {
  1043. char **exec_argv;
  1044. char **exec_env;
  1045. char *exec_file;
  1046. scm_dynwind_begin (0);
  1047. exec_file = scm_to_locale_string (filename);
  1048. scm_dynwind_free (exec_file);
  1049. exec_argv = scm_i_allocate_string_pointers (args);
  1050. exec_env = scm_i_allocate_string_pointers (env);
  1051. execve (exec_file, exec_argv, exec_env);
  1052. SCM_SYSERROR;
  1053. /* not reached. */
  1054. scm_dynwind_end ();
  1055. return SCM_BOOL_F;
  1056. }
  1057. #undef FUNC_NAME
  1058. #ifdef HAVE_FORK
  1059. SCM_DEFINE (scm_fork, "primitive-fork", 0, 0, 0,
  1060. (),
  1061. "Creates a new \"child\" process by duplicating the current \"parent\" process.\n"
  1062. "In the child the return value is 0. In the parent the return value is\n"
  1063. "the integer process ID of the child.\n\n"
  1064. "This procedure has been renamed from @code{fork} to avoid a naming conflict\n"
  1065. "with the scsh fork.")
  1066. #define FUNC_NAME s_scm_fork
  1067. {
  1068. int pid;
  1069. scm_i_finalizer_pre_fork ();
  1070. if (scm_ilength (scm_all_threads ()) != 1)
  1071. /* Other threads may be holding on to resources that Guile needs --
  1072. it is not safe to permit one thread to fork while others are
  1073. running.
  1074. In addition, POSIX clearly specifies that if a multi-threaded
  1075. program forks, the child must only call functions that are
  1076. async-signal-safe. We can't guarantee that in general. The best
  1077. we can do is to allow forking only very early, before any call to
  1078. sigaction spawns the signal-handling thread. */
  1079. scm_display
  1080. (scm_from_latin1_string
  1081. ("warning: call to primitive-fork while multiple threads are running;\n"
  1082. " further behavior unspecified. See \"Processes\" in the\n"
  1083. " manual, for more information.\n"),
  1084. scm_current_warning_port ());
  1085. pid = fork ();
  1086. if (pid == -1)
  1087. SCM_SYSERROR;
  1088. return scm_from_int (pid);
  1089. }
  1090. #undef FUNC_NAME
  1091. #endif /* HAVE_FORK */
  1092. #ifdef HAVE_FORK
  1093. #define HAVE_START_CHILD 1
  1094. /* Since Guile uses threads, we have to be very careful to avoid calling
  1095. functions that are not async-signal-safe in the child. That's why
  1096. this function is implemented in C. */
  1097. static pid_t
  1098. start_child (const char *exec_file, char **exec_argv,
  1099. int reading, int c2p[2], int writing, int p2c[2],
  1100. int in, int out, int err)
  1101. {
  1102. int pid;
  1103. int max_fd = 1024;
  1104. #if defined (HAVE_GETRLIMIT) && defined (RLIMIT_NOFILE)
  1105. {
  1106. struct rlimit lim = { 0, 0 };
  1107. if (getrlimit (RLIMIT_NOFILE, &lim) == 0)
  1108. max_fd = lim.rlim_cur;
  1109. }
  1110. #endif
  1111. pid = fork ();
  1112. if (pid != 0)
  1113. /* The parent, with either and error (pid == -1), or the PID of the
  1114. child. Return directly in either case. */
  1115. return pid;
  1116. /* The child. */
  1117. if (reading)
  1118. close (c2p[0]);
  1119. if (writing)
  1120. close (p2c[1]);
  1121. /* Close all file descriptors in ports inherited from the parent
  1122. except for in, out, and err. Heavy-handed, but robust. */
  1123. while (max_fd--)
  1124. if (max_fd != in && max_fd != out && max_fd != err)
  1125. close (max_fd);
  1126. /* Ignore errors on these open() calls. */
  1127. if (in == -1)
  1128. in = open ("/dev/null", O_RDONLY);
  1129. if (out == -1)
  1130. out = open ("/dev/null", O_WRONLY);
  1131. if (err == -1)
  1132. err = open ("/dev/null", O_WRONLY);
  1133. if (in > 0)
  1134. {
  1135. if (out == 0)
  1136. do out = dup (out); while (errno == EINTR);
  1137. if (err == 0)
  1138. do err = dup (err); while (errno == EINTR);
  1139. do dup2 (in, 0); while (errno == EINTR);
  1140. close (in);
  1141. }
  1142. if (out > 1)
  1143. {
  1144. if (err == 1)
  1145. do err = dup (err); while (errno == EINTR);
  1146. do dup2 (out, 1); while (errno == EINTR);
  1147. close (out);
  1148. }
  1149. if (err > 2)
  1150. {
  1151. do dup2 (err, 2); while (errno == EINTR);
  1152. close (err);
  1153. }
  1154. execvp (exec_file, exec_argv);
  1155. /* The exec failed! There is nothing sensible to do. */
  1156. if (err > 0)
  1157. {
  1158. char *msg = strerror (errno);
  1159. fprintf (fdopen (err, "a"), "In execvp of %s: %s\n",
  1160. exec_file, msg);
  1161. }
  1162. /* Use exit status 127, like shells in this case, as per POSIX
  1163. <http://pubs.opengroup.org/onlinepubs/007904875/utilities/xcu_chap02.html#tag_02_09_01_01>. */
  1164. _exit (127);
  1165. /* Not reached. */
  1166. return -1;
  1167. }
  1168. #endif
  1169. #ifdef HAVE_START_CHILD
  1170. static SCM
  1171. scm_open_process (SCM mode, SCM prog, SCM args)
  1172. #define FUNC_NAME "open-process"
  1173. {
  1174. long mode_bits;
  1175. int reading, writing;
  1176. int c2p[2]; /* Child to parent. */
  1177. int p2c[2]; /* Parent to child. */
  1178. int in = -1, out = -1, err = -1;
  1179. int pid;
  1180. char *exec_file;
  1181. char **exec_argv;
  1182. SCM read_port = SCM_BOOL_F, write_port = SCM_BOOL_F;
  1183. exec_file = scm_to_locale_string (prog);
  1184. exec_argv = scm_i_allocate_string_pointers (scm_cons (prog, args));
  1185. mode_bits = scm_i_mode_bits (mode);
  1186. reading = mode_bits & SCM_RDNG;
  1187. writing = mode_bits & SCM_WRTNG;
  1188. if (reading)
  1189. {
  1190. if (pipe (c2p))
  1191. {
  1192. int errno_save = errno;
  1193. free (exec_file);
  1194. errno = errno_save;
  1195. SCM_SYSERROR;
  1196. }
  1197. out = c2p[1];
  1198. }
  1199. if (writing)
  1200. {
  1201. if (pipe (p2c))
  1202. {
  1203. int errno_save = errno;
  1204. free (exec_file);
  1205. if (reading)
  1206. {
  1207. close (c2p[0]);
  1208. close (c2p[1]);
  1209. }
  1210. errno = errno_save;
  1211. SCM_SYSERROR;
  1212. }
  1213. in = p2c[0];
  1214. }
  1215. {
  1216. SCM port;
  1217. if (SCM_OPOUTFPORTP ((port = scm_current_error_port ())))
  1218. err = SCM_FPORT_FDES (port);
  1219. if (out == -1 && SCM_OPOUTFPORTP ((port = scm_current_output_port ())))
  1220. out = SCM_FPORT_FDES (port);
  1221. if (in == -1 && SCM_OPINFPORTP ((port = scm_current_input_port ())))
  1222. in = SCM_FPORT_FDES (port);
  1223. }
  1224. pid = start_child (exec_file, exec_argv, reading, c2p, writing, p2c,
  1225. in, out, err);
  1226. if (pid == -1)
  1227. {
  1228. int errno_save = errno;
  1229. free (exec_file);
  1230. if (reading)
  1231. {
  1232. close (c2p[0]);
  1233. close (c2p[1]);
  1234. }
  1235. if (writing)
  1236. {
  1237. close (p2c[0]);
  1238. close (p2c[1]);
  1239. }
  1240. errno = errno_save;
  1241. SCM_SYSERROR;
  1242. }
  1243. /* There is no sense in catching errors on close(). */
  1244. if (reading)
  1245. {
  1246. close (c2p[1]);
  1247. read_port = scm_i_fdes_to_port (c2p[0], scm_mode_bits ("r0"),
  1248. sym_read_pipe,
  1249. SCM_FPORT_OPTION_NOT_SEEKABLE);
  1250. }
  1251. if (writing)
  1252. {
  1253. close (p2c[0]);
  1254. write_port = scm_i_fdes_to_port (p2c[1], scm_mode_bits ("w0"),
  1255. sym_write_pipe,
  1256. SCM_FPORT_OPTION_NOT_SEEKABLE);
  1257. }
  1258. return scm_values_3 (read_port, write_port, scm_from_int (pid));
  1259. }
  1260. #undef FUNC_NAME
  1261. static void
  1262. restore_sigaction (SCM pair)
  1263. {
  1264. SCM sig, handler, flags;
  1265. sig = scm_car (pair);
  1266. handler = scm_cadr (pair);
  1267. flags = scm_cddr (pair);
  1268. scm_sigaction (sig, handler, flags);
  1269. }
  1270. static void
  1271. scm_dynwind_sigaction (int sig, SCM handler, SCM flags)
  1272. {
  1273. SCM old, scm_sig;
  1274. scm_sig = scm_from_int (sig);
  1275. old = scm_sigaction (scm_sig, handler, flags);
  1276. scm_dynwind_unwind_handler_with_scm (restore_sigaction,
  1277. scm_cons (scm_sig, old),
  1278. SCM_F_WIND_EXPLICITLY);
  1279. }
  1280. SCM_DEFINE (scm_system_star, "system*", 0, 0, 1,
  1281. (SCM args),
  1282. "Execute the command indicated by @var{args}. The first element must\n"
  1283. "be a string indicating the command to be executed, and the remaining\n"
  1284. "items must be strings representing each of the arguments to that\n"
  1285. "command.\n"
  1286. "\n"
  1287. "This function returns the exit status of the command as provided by\n"
  1288. "@code{waitpid}. This value can be handled with @code{status:exit-val}\n"
  1289. "and the related functions.\n"
  1290. "\n"
  1291. "@code{system*} is similar to @code{system}, but accepts only one\n"
  1292. "string per-argument, and performs no shell interpretation. The\n"
  1293. "command is executed using fork and execlp. Accordingly this function\n"
  1294. "may be safer than @code{system} in situations where shell\n"
  1295. "interpretation is not required.\n"
  1296. "\n"
  1297. "Example: (system* \"echo\" \"foo\" \"bar\")")
  1298. #define FUNC_NAME s_scm_system_star
  1299. {
  1300. SCM prog, res;
  1301. int pid, status, wait_result;
  1302. if (scm_is_null (args))
  1303. SCM_WRONG_NUM_ARGS ();
  1304. prog = scm_car (args);
  1305. args = scm_cdr (args);
  1306. scm_dynwind_begin (0);
  1307. /* Make sure the child can't kill us (as per normal system call). */
  1308. scm_dynwind_sigaction (SIGINT,
  1309. scm_from_uintptr_t ((uintptr_t) SIG_IGN),
  1310. SCM_UNDEFINED);
  1311. #ifdef SIGQUIT
  1312. scm_dynwind_sigaction (SIGQUIT,
  1313. scm_from_uintptr_t ((uintptr_t) SIG_IGN),
  1314. SCM_UNDEFINED);
  1315. #endif
  1316. res = scm_open_process (scm_nullstr, prog, args);
  1317. pid = scm_to_int (scm_c_value_ref (res, 2));
  1318. SCM_SYSCALL (wait_result = waitpid (pid, &status, 0));
  1319. if (wait_result == -1)
  1320. SCM_SYSERROR;
  1321. scm_dynwind_end ();
  1322. return scm_from_int (status);
  1323. }
  1324. #undef FUNC_NAME
  1325. #endif /* HAVE_START_CHILD */
  1326. #ifdef HAVE_UNAME
  1327. SCM_DEFINE (scm_uname, "uname", 0, 0, 0,
  1328. (),
  1329. "Return an object with some information about the computer\n"
  1330. "system the program is running on.")
  1331. #define FUNC_NAME s_scm_uname
  1332. {
  1333. struct utsname buf;
  1334. SCM result = scm_c_make_vector (5, SCM_UNSPECIFIED);
  1335. if (uname (&buf) < 0)
  1336. SCM_SYSERROR;
  1337. SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_locale_string (buf.sysname));
  1338. SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (buf.nodename));
  1339. SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_locale_string (buf.release));
  1340. SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_locale_string (buf.version));
  1341. SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_locale_string (buf.machine));
  1342. /*
  1343. a linux special?
  1344. SCM_SIMPLE_VECTOR_SET(result, 5, scm_from_locale_string (buf.domainname));
  1345. */
  1346. return result;
  1347. }
  1348. #undef FUNC_NAME
  1349. #endif /* HAVE_UNAME */
  1350. SCM_DEFINE (scm_environ, "environ", 0, 1, 0,
  1351. (SCM env),
  1352. "If @var{env} is omitted, return the current environment (in the\n"
  1353. "Unix sense) as a list of strings. Otherwise set the current\n"
  1354. "environment, which is also the default environment for child\n"
  1355. "processes, to the supplied list of strings. Each member of\n"
  1356. "@var{env} should be of the form @code{NAME=VALUE} and values of\n"
  1357. "@code{NAME} should not be duplicated. If @var{env} is supplied\n"
  1358. "then the return value is unspecified.")
  1359. #define FUNC_NAME s_scm_environ
  1360. {
  1361. if (SCM_UNBNDP (env))
  1362. return scm_makfromstrs (-1, environ);
  1363. else
  1364. {
  1365. environ = scm_i_allocate_string_pointers (env);
  1366. return SCM_UNSPECIFIED;
  1367. }
  1368. }
  1369. #undef FUNC_NAME
  1370. #ifdef L_tmpnam
  1371. SCM_DEFINE (scm_tmpnam, "tmpnam", 0, 0, 0,
  1372. (),
  1373. "Return a name in the file system that does not match any\n"
  1374. "existing file. However there is no guarantee that another\n"
  1375. "process will not create the file after @code{tmpnam} is called.\n"
  1376. "Care should be taken if opening the file, e.g., use the\n"
  1377. "@code{O_EXCL} open flag or use @code{mkstemp!} instead.")
  1378. #define FUNC_NAME s_scm_tmpnam
  1379. {
  1380. char name[L_tmpnam];
  1381. char *rv;
  1382. SCM_SYSCALL (rv = tmpnam (name));
  1383. if (rv == NULL)
  1384. /* not SCM_SYSERROR since errno probably not set. */
  1385. SCM_MISC_ERROR ("tmpnam failed", SCM_EOL);
  1386. return scm_from_locale_string (name);
  1387. }
  1388. #undef FUNC_NAME
  1389. #endif
  1390. SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0,
  1391. (void),
  1392. "Return an input/output port to a unique temporary file\n"
  1393. "named using the path prefix @code{P_tmpdir} defined in\n"
  1394. "@file{stdio.h}.\n"
  1395. "The file is automatically deleted when the port is closed\n"
  1396. "or the program terminates.")
  1397. #define FUNC_NAME s_scm_tmpfile
  1398. {
  1399. FILE *rv;
  1400. int fd;
  1401. if (! (rv = tmpfile ()))
  1402. SCM_SYSERROR;
  1403. #ifndef __MINGW32__
  1404. fd = dup (fileno (rv));
  1405. fclose (rv);
  1406. #else
  1407. fd = fileno (rv);
  1408. /* FIXME: leaking the file, it will never be closed! */
  1409. #endif
  1410. return scm_fdes_to_port (fd, "w+", SCM_BOOL_F);
  1411. }
  1412. #undef FUNC_NAME
  1413. SCM_DEFINE (scm_utime, "utime", 1, 5, 0,
  1414. (SCM pathname, SCM actime, SCM modtime, SCM actimens, SCM modtimens,
  1415. SCM flags),
  1416. "@code{utime} sets the access and modification times for the\n"
  1417. "file named by @var{pathname}. If @var{actime} or @var{modtime} is\n"
  1418. "not supplied, then the current time is used. @var{actime} and\n"
  1419. "@var{modtime} must be integer time values as returned by the\n"
  1420. "@code{current-time} procedure.\n\n"
  1421. "The optional @var{actimens} and @var{modtimens} are nanoseconds\n"
  1422. "to add @var{actime} and @var{modtime}. Nanosecond precision is\n"
  1423. "only supported on some combinations of file systems and operating\n"
  1424. "systems.\n"
  1425. "@lisp\n"
  1426. "(utime \"foo\" (- (current-time) 3600))\n"
  1427. "@end lisp\n"
  1428. "will set the access time to one hour in the past and the\n"
  1429. "modification time to the current time.")
  1430. #define FUNC_NAME s_scm_utime
  1431. {
  1432. int rv;
  1433. time_t atim_sec, mtim_sec;
  1434. long atim_nsec, mtim_nsec;
  1435. int f;
  1436. if (SCM_UNBNDP (actime))
  1437. {
  1438. #ifdef HAVE_UTIMENSAT
  1439. atim_sec = 0;
  1440. atim_nsec = UTIME_NOW;
  1441. #else
  1442. SCM_SYSCALL (time (&atim_sec));
  1443. atim_nsec = 0;
  1444. #endif
  1445. }
  1446. else
  1447. {
  1448. atim_sec = SCM_NUM2ULONG (2, actime);
  1449. if (SCM_UNBNDP (actimens))
  1450. atim_nsec = 0;
  1451. else
  1452. atim_nsec = SCM_NUM2LONG (4, actimens);
  1453. }
  1454. if (SCM_UNBNDP (modtime))
  1455. {
  1456. #ifdef HAVE_UTIMENSAT
  1457. mtim_sec = 0;
  1458. mtim_nsec = UTIME_NOW;
  1459. #else
  1460. SCM_SYSCALL (time (&mtim_sec));
  1461. mtim_nsec = 0;
  1462. #endif
  1463. }
  1464. else
  1465. {
  1466. mtim_sec = SCM_NUM2ULONG (3, modtime);
  1467. if (SCM_UNBNDP (modtimens))
  1468. mtim_nsec = 0;
  1469. else
  1470. mtim_nsec = SCM_NUM2LONG (5, modtimens);
  1471. }
  1472. if (SCM_UNBNDP (flags))
  1473. f = 0;
  1474. else
  1475. f = SCM_NUM2INT (6, flags);
  1476. #ifdef HAVE_UTIMENSAT
  1477. {
  1478. struct timespec times[2];
  1479. times[0].tv_sec = atim_sec;
  1480. times[0].tv_nsec = atim_nsec;
  1481. times[1].tv_sec = mtim_sec;
  1482. times[1].tv_nsec = mtim_nsec;
  1483. STRING_SYSCALL (pathname, c_pathname,
  1484. rv = utimensat (AT_FDCWD, c_pathname, times, f));
  1485. }
  1486. #else
  1487. {
  1488. struct utimbuf utm;
  1489. utm.actime = atim_sec;
  1490. utm.modtime = mtim_sec;
  1491. /* Silence warnings. */
  1492. (void) atim_nsec;
  1493. (void) mtim_nsec;
  1494. if (f != 0)
  1495. scm_out_of_range(FUNC_NAME, flags);
  1496. STRING_SYSCALL (pathname, c_pathname,
  1497. rv = utime (c_pathname, &utm));
  1498. }
  1499. #endif
  1500. if (rv != 0)
  1501. SCM_SYSERROR;
  1502. return SCM_UNSPECIFIED;
  1503. }
  1504. #undef FUNC_NAME
  1505. SCM_DEFINE (scm_getpid, "getpid", 0, 0, 0,
  1506. (),
  1507. "Return an integer representing the current process ID.")
  1508. #define FUNC_NAME s_scm_getpid
  1509. {
  1510. return scm_from_ulong (getpid ());
  1511. }
  1512. #undef FUNC_NAME
  1513. SCM_DEFINE (scm_putenv, "putenv", 1, 0, 0,
  1514. (SCM str),
  1515. "Modifies the environment of the current process, which is also\n"
  1516. "the default environment inherited by child processes. If\n"
  1517. "@var{str} is of the form @code{NAME=VALUE} then it will be\n"
  1518. "written directly into the environment, replacing any existing\n"
  1519. "environment string with name matching @code{NAME}. If\n"
  1520. "@var{str} does not contain an equal sign, then any existing\n"
  1521. "string with name matching @var{str} will be removed.\n"
  1522. "\n"
  1523. "The return value is unspecified.")
  1524. #define FUNC_NAME s_scm_putenv
  1525. {
  1526. int rv;
  1527. char *c_str = scm_to_locale_string (str);
  1528. /* Leave C_STR in the environment. */
  1529. /* Gnulib's `putenv' module honors the semantics described above. */
  1530. rv = putenv (c_str);
  1531. if (rv < 0)
  1532. SCM_SYSERROR;
  1533. return SCM_UNSPECIFIED;
  1534. }
  1535. #undef FUNC_NAME
  1536. /* This mutex is used to serialize invocations of `setlocale ()' on non-GNU
  1537. systems (i.e., systems where a reentrant locale API is not available). It
  1538. is also acquired before calls to `nl_langinfo ()'. See `i18n.c' for
  1539. details. */
  1540. scm_i_pthread_mutex_t scm_i_locale_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
  1541. #ifdef HAVE_SETLOCALE
  1542. SCM_DEFINE (scm_setlocale, "setlocale", 1, 1, 0,
  1543. (SCM category, SCM locale),
  1544. "If @var{locale} is omitted, return the current value of the\n"
  1545. "specified locale category as a system-dependent string.\n"
  1546. "@var{category} should be specified using the values\n"
  1547. "@code{LC_COLLATE}, @code{LC_ALL} etc.\n"
  1548. "\n"
  1549. "Otherwise the specified locale category is set to the string\n"
  1550. "@var{locale} and the new value is returned as a\n"
  1551. "system-dependent string. If @var{locale} is an empty string,\n"
  1552. "the locale will be set using environment variables.\n"
  1553. "\n"
  1554. "When the locale is changed, the character encoding of the new\n"
  1555. "locale (UTF-8, ISO-8859-1, etc.) is used for the current\n"
  1556. "input, output, and error ports\n")
  1557. #define FUNC_NAME s_scm_setlocale
  1558. {
  1559. int c_category;
  1560. char *clocale;
  1561. char *rv;
  1562. const char *enc;
  1563. scm_dynwind_begin (0);
  1564. if (SCM_UNBNDP (locale))
  1565. {
  1566. clocale = NULL;
  1567. }
  1568. else
  1569. {
  1570. clocale = scm_to_locale_string (locale);
  1571. scm_dynwind_free (clocale);
  1572. }
  1573. c_category = scm_i_to_lc_category (category, 1);
  1574. scm_i_pthread_mutex_lock (&scm_i_locale_mutex);
  1575. rv = setlocale (c_category, clocale);
  1576. scm_i_pthread_mutex_unlock (&scm_i_locale_mutex);
  1577. if (rv == NULL)
  1578. {
  1579. /* POSIX and C99 don't say anything about setlocale setting errno, so
  1580. force a sensible value here. glibc leaves ENOENT, which would be
  1581. fine, but it's not a documented feature. */
  1582. errno = EINVAL;
  1583. SCM_SYSERROR;
  1584. }
  1585. enc = locale_charset ();
  1586. /* Set the default encoding for new ports. */
  1587. scm_i_set_default_port_encoding (enc);
  1588. /* Set the encoding for the stdio ports. */
  1589. scm_i_set_port_encoding_x (scm_current_input_port (), enc);
  1590. scm_i_set_port_encoding_x (scm_current_output_port (), enc);
  1591. scm_i_set_port_encoding_x (scm_current_error_port (), enc);
  1592. scm_dynwind_end ();
  1593. return scm_from_locale_string (rv);
  1594. }
  1595. #undef FUNC_NAME
  1596. #endif /* HAVE_SETLOCALE */
  1597. #ifdef HAVE_MKNOD
  1598. SCM_DEFINE (scm_mknod, "mknod", 4, 0, 0,
  1599. (SCM path, SCM type, SCM perms, SCM dev),
  1600. "Creates a new special file, such as a file corresponding to a device.\n"
  1601. "@var{path} specifies the name of the file. @var{type} should\n"
  1602. "be one of the following symbols:\n"
  1603. "regular, directory, symlink, block-special, char-special,\n"
  1604. "fifo, or socket. @var{perms} (an integer) specifies the file permissions.\n"
  1605. "@var{dev} (an integer) specifies which device the special file refers\n"
  1606. "to. Its exact interpretation depends on the kind of special file\n"
  1607. "being created.\n\n"
  1608. "E.g.,\n"
  1609. "@lisp\n"
  1610. "(mknod \"/dev/fd0\" 'block-special #o660 (+ (* 2 256) 2))\n"
  1611. "@end lisp\n\n"
  1612. "The return value is unspecified.")
  1613. #define FUNC_NAME s_scm_mknod
  1614. {
  1615. int val;
  1616. const char *p;
  1617. int ctype = 0;
  1618. SCM_VALIDATE_STRING (1, path);
  1619. SCM_VALIDATE_SYMBOL (2, type);
  1620. p = scm_i_symbol_chars (type);
  1621. if (strcmp (p, "regular") == 0)
  1622. ctype = S_IFREG;
  1623. else if (strcmp (p, "directory") == 0)
  1624. ctype = S_IFDIR;
  1625. #ifdef S_IFLNK
  1626. /* systems without symlinks probably don't have S_IFLNK defined */
  1627. else if (strcmp (p, "symlink") == 0)
  1628. ctype = S_IFLNK;
  1629. #endif
  1630. else if (strcmp (p, "block-special") == 0)
  1631. ctype = S_IFBLK;
  1632. else if (strcmp (p, "char-special") == 0)
  1633. ctype = S_IFCHR;
  1634. else if (strcmp (p, "fifo") == 0)
  1635. ctype = S_IFIFO;
  1636. #ifdef S_IFSOCK
  1637. else if (strcmp (p, "socket") == 0)
  1638. ctype = S_IFSOCK;
  1639. #endif
  1640. else
  1641. SCM_OUT_OF_RANGE (2, type);
  1642. STRING_SYSCALL (path, c_path,
  1643. val = mknod (c_path,
  1644. ctype | scm_to_int (perms),
  1645. scm_to_int (dev)));
  1646. if (val != 0)
  1647. SCM_SYSERROR;
  1648. return SCM_UNSPECIFIED;
  1649. }
  1650. #undef FUNC_NAME
  1651. #endif /* HAVE_MKNOD */
  1652. #ifdef HAVE_NICE
  1653. SCM_DEFINE (scm_nice, "nice", 1, 0, 0,
  1654. (SCM incr),
  1655. "Increment the priority of the current process by @var{incr}. A higher\n"
  1656. "priority value means that the process runs less often.\n"
  1657. "The return value is unspecified.")
  1658. #define FUNC_NAME s_scm_nice
  1659. {
  1660. /* nice() returns "prio-NZERO" on success or -1 on error, but -1 can arise
  1661. from "prio-NZERO", so an error must be detected from errno changed */
  1662. errno = 0;
  1663. nice (scm_to_int (incr));
  1664. if (errno != 0)
  1665. SCM_SYSERROR;
  1666. return SCM_UNSPECIFIED;
  1667. }
  1668. #undef FUNC_NAME
  1669. #endif /* HAVE_NICE */
  1670. #ifdef HAVE_SYNC
  1671. SCM_DEFINE (scm_sync, "sync", 0, 0, 0,
  1672. (),
  1673. "Flush the operating system disk buffers.\n"
  1674. "The return value is unspecified.")
  1675. #define FUNC_NAME s_scm_sync
  1676. {
  1677. sync();
  1678. return SCM_UNSPECIFIED;
  1679. }
  1680. #undef FUNC_NAME
  1681. #endif /* HAVE_SYNC */
  1682. /* crypt() returns a pointer to a static buffer, so we use scm_i_misc_mutex
  1683. to avoid another thread overwriting it. A test program running crypt
  1684. continuously in two threads can be quickly seen tripping this problem.
  1685. crypt() is pretty slow normally, so a mutex shouldn't add much overhead.
  1686. glibc has a thread-safe crypt_r, but (in version 2.3.2) it runs a lot
  1687. slower (about 5x) than plain crypt if you pass an uninitialized data
  1688. block each time. Presumably there's some one-time setups. The best way
  1689. to use crypt_r for parallel execution in multiple threads would probably
  1690. be to maintain a little pool of initialized crypt_data structures, take
  1691. one and use it, then return it to the pool. That pool could be garbage
  1692. collected so it didn't add permanently to memory use if only a few crypt
  1693. calls are made. But we expect crypt will be used rarely, and even more
  1694. rarely will there be any desire for lots of parallel execution on
  1695. multiple cpus. So for now we don't bother with anything fancy, just
  1696. ensure it works. */
  1697. #ifdef HAVE_CRYPT
  1698. SCM_DEFINE (scm_crypt, "crypt", 2, 0, 0,
  1699. (SCM key, SCM salt),
  1700. "Encrypt @var{key} using @var{salt} as the salt value to the\n"
  1701. "crypt(3) library call.")
  1702. #define FUNC_NAME s_scm_crypt
  1703. {
  1704. int err;
  1705. SCM ret;
  1706. char *c_key, *c_salt, *c_ret;
  1707. scm_dynwind_begin (0);
  1708. c_key = scm_to_locale_string (key);
  1709. scm_dynwind_free (c_key);
  1710. c_salt = scm_to_locale_string (salt);
  1711. scm_dynwind_free (c_salt);
  1712. /* Take the lock because 'crypt' uses a static buffer. */
  1713. scm_i_dynwind_pthread_mutex_lock (&scm_i_misc_mutex);
  1714. /* The Linux crypt(3) man page says crypt will return NULL and set errno
  1715. on error. (Eg. ENOSYS if legal restrictions mean it cannot be
  1716. implemented). */
  1717. c_ret = crypt (c_key, c_salt);
  1718. if (c_ret == NULL)
  1719. {
  1720. /* Note: Do not throw until we've released 'scm_i_misc_mutex'
  1721. since this would cause a deadlock down the path. */
  1722. err = errno;
  1723. ret = SCM_BOOL_F;
  1724. }
  1725. else
  1726. {
  1727. err = 0;
  1728. ret = scm_from_locale_string (c_ret);
  1729. }
  1730. scm_dynwind_end ();
  1731. if (scm_is_false (ret))
  1732. {
  1733. errno = err;
  1734. SCM_SYSERROR;
  1735. }
  1736. return ret;
  1737. }
  1738. #undef FUNC_NAME
  1739. #endif /* HAVE_CRYPT */
  1740. #if HAVE_CHROOT
  1741. SCM_DEFINE (scm_chroot, "chroot", 1, 0, 0,
  1742. (SCM path),
  1743. "Change the root directory to that specified in @var{path}.\n"
  1744. "This directory will be used for path names beginning with\n"
  1745. "@file{/}. The root directory is inherited by all children\n"
  1746. "of the current process. Only the superuser may change the\n"
  1747. "root directory.")
  1748. #define FUNC_NAME s_scm_chroot
  1749. {
  1750. int rv;
  1751. WITH_STRING (path, c_path,
  1752. rv = chroot (c_path));
  1753. if (rv == -1)
  1754. SCM_SYSERROR;
  1755. return SCM_UNSPECIFIED;
  1756. }
  1757. #undef FUNC_NAME
  1758. #endif /* HAVE_CHROOT */
  1759. SCM_DEFINE (scm_getlogin, "getlogin", 0, 0, 0,
  1760. (void),
  1761. "Return a string containing the name of the user logged in on\n"
  1762. "the controlling terminal of the process, or @code{#f} if this\n"
  1763. "information cannot be obtained.")
  1764. #define FUNC_NAME s_scm_getlogin
  1765. {
  1766. char * p;
  1767. p = getlogin ();
  1768. if (!p || !*p)
  1769. return SCM_BOOL_F;
  1770. return scm_from_locale_string (p);
  1771. }
  1772. #undef FUNC_NAME
  1773. #if HAVE_GETPRIORITY
  1774. SCM_DEFINE (scm_getpriority, "getpriority", 2, 0, 0,
  1775. (SCM which, SCM who),
  1776. "Return the scheduling priority of the process, process group\n"
  1777. "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
  1778. "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
  1779. "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
  1780. "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
  1781. "process group identifier for @code{PRIO_PGRP}, and a user\n"
  1782. "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
  1783. "denotes the current process, process group, or user. Return\n"
  1784. "the highest priority (lowest numerical value) of any of the\n"
  1785. "specified processes.")
  1786. #define FUNC_NAME s_scm_getpriority
  1787. {
  1788. int cwhich, cwho, ret;
  1789. cwhich = scm_to_int (which);
  1790. cwho = scm_to_int (who);
  1791. /* We have to clear errno and examine it later, because -1 is a
  1792. legal return value for getpriority(). */
  1793. errno = 0;
  1794. ret = getpriority (cwhich, cwho);
  1795. if (errno != 0)
  1796. SCM_SYSERROR;
  1797. return scm_from_int (ret);
  1798. }
  1799. #undef FUNC_NAME
  1800. #endif /* HAVE_GETPRIORITY */
  1801. #if HAVE_SETPRIORITY
  1802. SCM_DEFINE (scm_setpriority, "setpriority", 3, 0, 0,
  1803. (SCM which, SCM who, SCM prio),
  1804. "Set the scheduling priority of the process, process group\n"
  1805. "or user, as indicated by @var{which} and @var{who}. @var{which}\n"
  1806. "is one of the variables @code{PRIO_PROCESS}, @code{PRIO_PGRP}\n"
  1807. "or @code{PRIO_USER}, and @var{who} is interpreted relative to\n"
  1808. "@var{which} (a process identifier for @code{PRIO_PROCESS},\n"
  1809. "process group identifier for @code{PRIO_PGRP}, and a user\n"
  1810. "identifier for @code{PRIO_USER}. A zero value of @var{who}\n"
  1811. "denotes the current process, process group, or user.\n"
  1812. "@var{prio} is a value in the range -20 and 20, the default\n"
  1813. "priority is 0; lower priorities cause more favorable\n"
  1814. "scheduling. Sets the priority of all of the specified\n"
  1815. "processes. Only the super-user may lower priorities.\n"
  1816. "The return value is not specified.")
  1817. #define FUNC_NAME s_scm_setpriority
  1818. {
  1819. int cwhich, cwho, cprio;
  1820. cwhich = scm_to_int (which);
  1821. cwho = scm_to_int (who);
  1822. cprio = scm_to_int (prio);
  1823. if (setpriority (cwhich, cwho, cprio) == -1)
  1824. SCM_SYSERROR;
  1825. return SCM_UNSPECIFIED;
  1826. }
  1827. #undef FUNC_NAME
  1828. #endif /* HAVE_SETPRIORITY */
  1829. #ifdef HAVE_SCHED_GETAFFINITY
  1830. static SCM
  1831. cpu_set_to_bitvector (const cpu_set_t *cs)
  1832. {
  1833. SCM bv;
  1834. size_t cpu;
  1835. bv = scm_c_make_bitvector (CPU_SETSIZE, SCM_BOOL_F);
  1836. for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
  1837. {
  1838. if (CPU_ISSET (cpu, cs))
  1839. /* XXX: This is inefficient but avoids code duplication. */
  1840. scm_c_bitvector_set_x (bv, cpu, SCM_BOOL_T);
  1841. }
  1842. return bv;
  1843. }
  1844. SCM_DEFINE (scm_getaffinity, "getaffinity", 1, 0, 0,
  1845. (SCM pid),
  1846. "Return a bitvector representing the CPU affinity mask for\n"
  1847. "process @var{pid}. Each CPU the process has affinity with\n"
  1848. "has its corresponding bit set in the returned bitvector.\n"
  1849. "The number of bits set is a good estimate of how many CPUs\n"
  1850. "Guile can use without stepping on other processes' toes.")
  1851. #define FUNC_NAME s_scm_getaffinity
  1852. {
  1853. int err;
  1854. cpu_set_t cs;
  1855. CPU_ZERO (&cs);
  1856. err = sched_getaffinity (scm_to_int (pid), sizeof (cs), &cs);
  1857. if (err)
  1858. SCM_SYSERROR;
  1859. return cpu_set_to_bitvector (&cs);
  1860. }
  1861. #undef FUNC_NAME
  1862. #endif /* HAVE_SCHED_GETAFFINITY */
  1863. #ifdef HAVE_SCHED_SETAFFINITY
  1864. SCM_DEFINE (scm_setaffinity, "setaffinity", 2, 0, 0,
  1865. (SCM pid, SCM mask),
  1866. "Install the CPU affinity mask @var{mask}, a bitvector, for\n"
  1867. "the process or thread with ID @var{pid}. The return value\n"
  1868. "is unspecified.")
  1869. #define FUNC_NAME s_scm_setaffinity
  1870. {
  1871. cpu_set_t cs;
  1872. scm_t_array_handle handle;
  1873. const uint32_t *c_mask;
  1874. size_t len, off, cpu;
  1875. ssize_t inc;
  1876. int err;
  1877. c_mask = scm_bitvector_elements (mask, &handle, &off, &len, &inc);
  1878. CPU_ZERO (&cs);
  1879. for (cpu = 0; cpu < len; cpu++)
  1880. {
  1881. size_t idx;
  1882. idx = cpu * inc + off;
  1883. if (c_mask[idx / 32] & (1UL << (idx % 32)))
  1884. CPU_SET (cpu, &cs);
  1885. }
  1886. err = sched_setaffinity (scm_to_int (pid), sizeof (cs), &cs);
  1887. if (err)
  1888. SCM_SYSERROR;
  1889. return SCM_UNSPECIFIED;
  1890. }
  1891. #undef FUNC_NAME
  1892. #endif /* HAVE_SCHED_SETAFFINITY */
  1893. #if HAVE_GETPASS
  1894. SCM_DEFINE (scm_getpass, "getpass", 1, 0, 0,
  1895. (SCM prompt),
  1896. "Display @var{prompt} to the standard error output and read\n"
  1897. "a password from @file{/dev/tty}. If this file is not\n"
  1898. "accessible, it reads from standard input. The password may be\n"
  1899. "up to 127 characters in length. Additional characters and the\n"
  1900. "terminating newline character are discarded. While reading\n"
  1901. "the password, echoing and the generation of signals by special\n"
  1902. "characters is disabled.")
  1903. #define FUNC_NAME s_scm_getpass
  1904. {
  1905. char * p;
  1906. SCM passwd;
  1907. SCM_VALIDATE_STRING (1, prompt);
  1908. WITH_STRING (prompt, c_prompt,
  1909. p = getpass(c_prompt));
  1910. passwd = scm_from_locale_string (p);
  1911. /* Clear out the password in the static buffer. */
  1912. memset (p, 0, strlen (p));
  1913. return passwd;
  1914. }
  1915. #undef FUNC_NAME
  1916. #endif /* HAVE_GETPASS */
  1917. SCM_DEFINE (scm_flock, "flock", 2, 0, 0,
  1918. (SCM file, SCM operation),
  1919. "Apply or remove an advisory lock on an open file.\n"
  1920. "@var{operation} specifies the action to be done:\n"
  1921. "\n"
  1922. "@defvar LOCK_SH\n"
  1923. "Shared lock. More than one process may hold a shared lock\n"
  1924. "for a given file at a given time.\n"
  1925. "@end defvar\n"
  1926. "@defvar LOCK_EX\n"
  1927. "Exclusive lock. Only one process may hold an exclusive lock\n"
  1928. "for a given file at a given time.\n"
  1929. "@end defvar\n"
  1930. "@defvar LOCK_UN\n"
  1931. "Unlock the file.\n"
  1932. "@end defvar\n"
  1933. "@defvar LOCK_NB\n"
  1934. "Don't block when locking. This is combined with one of the\n"
  1935. "other operations using @code{logior}. If @code{flock} would\n"
  1936. "block an @code{EWOULDBLOCK} error is thrown.\n"
  1937. "@end defvar\n"
  1938. "\n"
  1939. "The return value is not specified. @var{file} may be an open\n"
  1940. "file descriptor or an open file descriptor port.\n"
  1941. "\n"
  1942. "Note that @code{flock} does not lock files across NFS.")
  1943. #define FUNC_NAME s_scm_flock
  1944. {
  1945. int fdes;
  1946. if (scm_is_integer (file))
  1947. fdes = scm_to_int (file);
  1948. else
  1949. {
  1950. SCM_VALIDATE_OPFPORT (2, file);
  1951. fdes = SCM_FPORT_FDES (file);
  1952. }
  1953. if (flock (fdes, scm_to_int (operation)) == -1)
  1954. SCM_SYSERROR;
  1955. return SCM_UNSPECIFIED;
  1956. }
  1957. #undef FUNC_NAME
  1958. #if HAVE_SETHOSTNAME
  1959. SCM_DEFINE (scm_sethostname, "sethostname", 1, 0, 0,
  1960. (SCM name),
  1961. "Set the host name of the current processor to @var{name}. May\n"
  1962. "only be used by the superuser. The return value is not\n"
  1963. "specified.")
  1964. #define FUNC_NAME s_scm_sethostname
  1965. {
  1966. int rv;
  1967. WITH_STRING (name, c_name,
  1968. rv = sethostname (c_name, strlen(c_name)));
  1969. if (rv == -1)
  1970. SCM_SYSERROR;
  1971. return SCM_UNSPECIFIED;
  1972. }
  1973. #undef FUNC_NAME
  1974. #endif /* HAVE_SETHOSTNAME */
  1975. #if HAVE_GETHOSTNAME
  1976. SCM_DEFINE (scm_gethostname, "gethostname", 0, 0, 0,
  1977. (void),
  1978. "Return the host name of the current processor.")
  1979. #define FUNC_NAME s_scm_gethostname
  1980. {
  1981. #ifdef MAXHOSTNAMELEN
  1982. /* Various systems define MAXHOSTNAMELEN (including Solaris in fact).
  1983. * On GNU/Linux this doesn't include the terminating '\0', hence "+ 1". */
  1984. const int len = MAXHOSTNAMELEN + 1;
  1985. char *const p = scm_malloc (len);
  1986. const int res = gethostname (p, len);
  1987. scm_dynwind_begin (0);
  1988. scm_dynwind_unwind_handler (free, p, 0);
  1989. #else
  1990. /* Default 256 is for Solaris, under Linux ENAMETOOLONG is returned if not
  1991. * large enough. SUSv2 specifies 255 maximum too, apparently. */
  1992. int len = 256;
  1993. int res;
  1994. char *p;
  1995. # if HAVE_SYSCONF && defined (_SC_HOST_NAME_MAX)
  1996. /* POSIX specifies the HOST_NAME_MAX system parameter for the max size,
  1997. * which may reflect a particular kernel configuration.
  1998. * Must watch out for this existing but giving -1, as happens for instance
  1999. * in gnu/linux glibc 2.3.2. */
  2000. {
  2001. const long int n = sysconf (_SC_HOST_NAME_MAX);
  2002. if (n != -1L)
  2003. len = n;
  2004. }
  2005. # endif
  2006. p = scm_malloc (len);
  2007. scm_dynwind_begin (0);
  2008. scm_dynwind_unwind_handler (free, p, 0);
  2009. res = gethostname (p, len);
  2010. while (res == -1 && errno == ENAMETOOLONG)
  2011. {
  2012. len *= 2;
  2013. /* scm_realloc may throw an exception. */
  2014. p = scm_realloc (p, len);
  2015. res = gethostname (p, len);
  2016. }
  2017. #endif
  2018. if (res == -1)
  2019. {
  2020. const int save_errno = errno;
  2021. /* No guile exceptions can occur before we have freed p's memory. */
  2022. scm_dynwind_end ();
  2023. free (p);
  2024. errno = save_errno;
  2025. SCM_SYSERROR;
  2026. }
  2027. else
  2028. {
  2029. /* scm_from_locale_string may throw an exception. */
  2030. const SCM name = scm_from_locale_string (p);
  2031. /* No guile exceptions can occur before we have freed p's memory. */
  2032. scm_dynwind_end ();
  2033. free (p);
  2034. return name;
  2035. }
  2036. }
  2037. #undef FUNC_NAME
  2038. #endif /* HAVE_GETHOSTNAME */
  2039. #ifdef HAVE_START_CHILD
  2040. static void
  2041. scm_init_popen (void)
  2042. {
  2043. scm_c_define_gsubr ("open-process", 2, 0, 1, scm_open_process);
  2044. }
  2045. #endif /* HAVE_START_CHILD */
  2046. void
  2047. scm_init_posix ()
  2048. {
  2049. scm_add_feature ("posix");
  2050. #ifdef EXIT_SUCCESS
  2051. scm_c_define ("EXIT_SUCCESS", scm_from_int (EXIT_SUCCESS));
  2052. #endif
  2053. #ifdef EXIT_FAILURE
  2054. scm_c_define ("EXIT_FAILURE", scm_from_int (EXIT_FAILURE));
  2055. #endif
  2056. #ifdef HAVE_GETEUID
  2057. scm_add_feature ("EIDs");
  2058. #endif
  2059. #ifdef WAIT_ANY
  2060. scm_c_define ("WAIT_ANY", scm_from_int (WAIT_ANY));
  2061. #endif
  2062. #ifdef WAIT_MYPGRP
  2063. scm_c_define ("WAIT_MYPGRP", scm_from_int (WAIT_MYPGRP));
  2064. #endif
  2065. #ifdef WNOHANG
  2066. scm_c_define ("WNOHANG", scm_from_int (WNOHANG));
  2067. #endif
  2068. #ifdef WUNTRACED
  2069. scm_c_define ("WUNTRACED", scm_from_int (WUNTRACED));
  2070. #endif
  2071. #ifdef LC_COLLATE
  2072. scm_c_define ("LC_COLLATE", scm_from_int (LC_COLLATE));
  2073. #endif
  2074. #ifdef LC_CTYPE
  2075. scm_c_define ("LC_CTYPE", scm_from_int (LC_CTYPE));
  2076. #endif
  2077. #ifdef LC_MONETARY
  2078. scm_c_define ("LC_MONETARY", scm_from_int (LC_MONETARY));
  2079. #endif
  2080. #ifdef LC_NUMERIC
  2081. scm_c_define ("LC_NUMERIC", scm_from_int (LC_NUMERIC));
  2082. #endif
  2083. #ifdef LC_TIME
  2084. scm_c_define ("LC_TIME", scm_from_int (LC_TIME));
  2085. #endif
  2086. #ifdef LC_MESSAGES
  2087. scm_c_define ("LC_MESSAGES", scm_from_int (LC_MESSAGES));
  2088. #endif
  2089. #ifdef LC_ALL
  2090. scm_c_define ("LC_ALL", scm_from_int (LC_ALL));
  2091. #endif
  2092. #ifdef LC_PAPER
  2093. scm_c_define ("LC_PAPER", scm_from_int (LC_PAPER));
  2094. #endif
  2095. #ifdef LC_NAME
  2096. scm_c_define ("LC_NAME", scm_from_int (LC_NAME));
  2097. #endif
  2098. #ifdef LC_ADDRESS
  2099. scm_c_define ("LC_ADDRESS", scm_from_int (LC_ADDRESS));
  2100. #endif
  2101. #ifdef LC_TELEPHONE
  2102. scm_c_define ("LC_TELEPHONE", scm_from_int (LC_TELEPHONE));
  2103. #endif
  2104. #ifdef LC_MEASUREMENT
  2105. scm_c_define ("LC_MEASUREMENT", scm_from_int (LC_MEASUREMENT));
  2106. #endif
  2107. #ifdef LC_IDENTIFICATION
  2108. scm_c_define ("LC_IDENTIFICATION", scm_from_int (LC_IDENTIFICATION));
  2109. #endif
  2110. #ifdef PIPE_BUF
  2111. scm_c_define ("PIPE_BUF", scm_from_long (PIPE_BUF));
  2112. #endif
  2113. #ifdef PRIO_PROCESS
  2114. scm_c_define ("PRIO_PROCESS", scm_from_int (PRIO_PROCESS));
  2115. #endif
  2116. #ifdef PRIO_PGRP
  2117. scm_c_define ("PRIO_PGRP", scm_from_int (PRIO_PGRP));
  2118. #endif
  2119. #ifdef PRIO_USER
  2120. scm_c_define ("PRIO_USER", scm_from_int (PRIO_USER));
  2121. #endif
  2122. #ifdef LOCK_SH
  2123. scm_c_define ("LOCK_SH", scm_from_int (LOCK_SH));
  2124. #endif
  2125. #ifdef LOCK_EX
  2126. scm_c_define ("LOCK_EX", scm_from_int (LOCK_EX));
  2127. #endif
  2128. #ifdef LOCK_UN
  2129. scm_c_define ("LOCK_UN", scm_from_int (LOCK_UN));
  2130. #endif
  2131. #ifdef LOCK_NB
  2132. scm_c_define ("LOCK_NB", scm_from_int (LOCK_NB));
  2133. #endif
  2134. #include "cpp-SIG.c"
  2135. #include "posix.x"
  2136. #ifdef HAVE_FORK
  2137. scm_add_feature ("fork");
  2138. #endif /* HAVE_FORK */
  2139. #ifdef HAVE_START_CHILD
  2140. scm_add_feature ("popen");
  2141. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  2142. "scm_init_popen",
  2143. (scm_t_extension_init_func) scm_init_popen,
  2144. NULL);
  2145. #endif /* HAVE_START_CHILD */
  2146. }