posix.c 77 KB

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