posix.c 62 KB

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