posix.c 68 KB

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