posix.c 60 KB

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