posix.c 76 KB

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