posix.c 69 KB

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