posix.c 68 KB

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