posix.c 71 KB

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