posix.c 60 KB

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