posix.c 69 KB

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