posix.c 69 KB

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