posix.c 60 KB

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