PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/erts/emulator/sys/unix/sys.c

https://github.com/dudefrommangalore/otp
C | 3345 lines | 2515 code | 418 blank | 412 comment | 438 complexity | c8ca265027e88bddb30c7c593bca9afd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * %CopyrightBegin%
  3. *
  4. * Copyright Ericsson AB 1996-2010. All Rights Reserved.
  5. *
  6. * The contents of this file are subject to the Erlang Public License,
  7. * Version 1.1, (the "License"); you may not use this file except in
  8. * compliance with the License. You should have received a copy of the
  9. * Erlang Public License along with this software. If not, it can be
  10. * retrieved online at http://www.erlang.org/.
  11. *
  12. * Software distributed under the License is distributed on an "AS IS"
  13. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. * the License for the specific language governing rights and limitations
  15. * under the License.
  16. *
  17. * %CopyrightEnd%
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include "config.h"
  21. #endif
  22. #ifdef ISC32
  23. #define _POSIX_SOURCE
  24. #define _XOPEN_SOURCE
  25. #endif
  26. #include <sys/times.h> /* ! */
  27. #include <time.h>
  28. #include <signal.h>
  29. #include <sys/wait.h>
  30. #include <sys/uio.h>
  31. #include <termios.h>
  32. #include <ctype.h>
  33. #include <sys/utsname.h>
  34. #ifdef ISC32
  35. #include <sys/bsdtypes.h>
  36. #endif
  37. #include <termios.h>
  38. #ifdef HAVE_FCNTL_H
  39. #include <fcntl.h>
  40. #endif
  41. #ifdef HAVE_SYS_IOCTL_H
  42. #include <sys/ioctl.h>
  43. #endif
  44. #define NEED_CHILD_SETUP_DEFINES
  45. #define ERTS_WANT_BREAK_HANDLING
  46. #define ERTS_WANT_GOT_SIGUSR1
  47. #define WANT_NONBLOCKING /* must define this to pull in defs from sys.h */
  48. #include "sys.h"
  49. #ifdef USE_THREADS
  50. #include "erl_threads.h"
  51. #endif
  52. #include "erl_mseg.h"
  53. extern char **environ;
  54. static erts_smp_rwmtx_t environ_rwmtx;
  55. #define MAX_VSIZE 16 /* Max number of entries allowed in an I/O
  56. * vector sock_sendv().
  57. */
  58. /*
  59. * Don't need global.h, but bif_table.h (included by bif.h),
  60. * won't compile otherwise
  61. */
  62. #include "global.h"
  63. #include "bif.h"
  64. #include "erl_sys_driver.h"
  65. #include "erl_check_io.h"
  66. #ifndef DISABLE_VFORK
  67. #define DISABLE_VFORK 0
  68. #endif
  69. #ifdef USE_THREADS
  70. # ifdef ENABLE_CHILD_WAITER_THREAD
  71. # define CHLDWTHR ENABLE_CHILD_WAITER_THREAD
  72. # else
  73. # define CHLDWTHR 0
  74. # endif
  75. #else
  76. # define CHLDWTHR 0
  77. #endif
  78. /*
  79. * [OTP-3906]
  80. * Solaris signal management gets confused when threads are used and a
  81. * lot of child processes dies. The confusion results in that SIGCHLD
  82. * signals aren't delivered to the emulator which in turn results in
  83. * a lot of defunct processes in the system.
  84. *
  85. * The problem seems to appear when a signal is frequently
  86. * blocked/unblocked at the same time as the signal is frequently
  87. * propagated. The child waiter thread is a workaround for this problem.
  88. * The SIGCHLD signal is always blocked (in all threads), and the child
  89. * waiter thread fetches the signal by a call to sigwait(). See
  90. * child_waiter().
  91. */
  92. typedef struct ErtsSysReportExit_ ErtsSysReportExit;
  93. struct ErtsSysReportExit_ {
  94. ErtsSysReportExit *next;
  95. Eterm port;
  96. int pid;
  97. int ifd;
  98. int ofd;
  99. #if CHLDWTHR && !defined(ERTS_SMP)
  100. int status;
  101. #endif
  102. };
  103. static ErtsSysReportExit *report_exit_list;
  104. #if CHLDWTHR && !defined(ERTS_SMP)
  105. static ErtsSysReportExit *report_exit_transit_list;
  106. #endif
  107. extern int check_async_ready(void);
  108. extern int driver_interrupt(int, int);
  109. /*EXTERN_FUNCTION(void, increment_time, (int));*/
  110. /*EXTERN_FUNCTION(int, next_time, (_VOID_));*/
  111. extern void do_break(void);
  112. extern void erl_sys_args(int*, char**);
  113. /* The following two defs should probably be moved somewhere else */
  114. extern void erts_sys_init_float(void);
  115. extern void erl_crash_dump(char* file, int line, char* fmt, ...);
  116. #define DIR_SEPARATOR_CHAR '/'
  117. #if defined(DEBUG)
  118. #define ERL_BUILD_TYPE_MARKER ".debug"
  119. #elif defined(PURIFY)
  120. #define ERL_BUILD_TYPE_MARKER ".purify"
  121. #elif defined(QUANTIFY)
  122. #define ERL_BUILD_TYPE_MARKER ".quantify"
  123. #elif defined(PURECOV)
  124. #define ERL_BUILD_TYPE_MARKER ".purecov"
  125. #elif defined(VALGRIND)
  126. #define ERL_BUILD_TYPE_MARKER ".valgrind"
  127. #else /* opt */
  128. #define ERL_BUILD_TYPE_MARKER
  129. #endif
  130. #define CHILD_SETUP_PROG_NAME "child_setup" ERL_BUILD_TYPE_MARKER
  131. #if !DISABLE_VFORK
  132. static char *child_setup_prog;
  133. #endif
  134. #ifdef DEBUG
  135. static int debug_log = 0;
  136. #endif
  137. #ifdef ERTS_SMP
  138. erts_smp_atomic_t erts_got_sigusr1;
  139. #define ERTS_SET_GOT_SIGUSR1 \
  140. erts_smp_atomic_set(&erts_got_sigusr1, 1)
  141. #define ERTS_UNSET_GOT_SIGUSR1 \
  142. erts_smp_atomic_set(&erts_got_sigusr1, 0)
  143. static erts_smp_atomic_t have_prepared_crash_dump;
  144. #define ERTS_PREPARED_CRASH_DUMP \
  145. ((int) erts_smp_atomic_xchg(&have_prepared_crash_dump, 1))
  146. #else
  147. volatile int erts_got_sigusr1;
  148. #define ERTS_SET_GOT_SIGUSR1 (erts_got_sigusr1 = 1)
  149. #define ERTS_UNSET_GOT_SIGUSR1 (erts_got_sigusr1 = 0)
  150. static volatile int have_prepared_crash_dump;
  151. #define ERTS_PREPARED_CRASH_DUMP \
  152. (have_prepared_crash_dump++)
  153. #endif
  154. static erts_smp_atomic_t sys_misc_mem_sz;
  155. #if defined(ERTS_SMP)
  156. static void smp_sig_notify(char c);
  157. static int sig_notify_fds[2] = {-1, -1};
  158. #elif defined(USE_THREADS)
  159. static int async_fd[2];
  160. #endif
  161. #if CHLDWTHR || defined(ERTS_SMP)
  162. erts_mtx_t chld_stat_mtx;
  163. #endif
  164. #if CHLDWTHR
  165. static erts_tid_t child_waiter_tid;
  166. /* chld_stat_mtx is used to protect against concurrent accesses
  167. of the driver_data fields pid, alive, and status. */
  168. erts_cnd_t chld_stat_cnd;
  169. static long children_alive;
  170. #define CHLD_STAT_LOCK erts_mtx_lock(&chld_stat_mtx)
  171. #define CHLD_STAT_UNLOCK erts_mtx_unlock(&chld_stat_mtx)
  172. #define CHLD_STAT_WAIT erts_cnd_wait(&chld_stat_cnd, &chld_stat_mtx)
  173. #define CHLD_STAT_SIGNAL erts_cnd_signal(&chld_stat_cnd)
  174. #elif defined(ERTS_SMP) /* ------------------------------------------------- */
  175. #define CHLD_STAT_LOCK erts_mtx_lock(&chld_stat_mtx)
  176. #define CHLD_STAT_UNLOCK erts_mtx_unlock(&chld_stat_mtx)
  177. #else /* ------------------------------------------------------------------- */
  178. #define CHLD_STAT_LOCK
  179. #define CHLD_STAT_UNLOCK
  180. static volatile int children_died;
  181. #endif
  182. static struct fd_data {
  183. char pbuf[4]; /* hold partial packet bytes */
  184. int psz; /* size of pbuf */
  185. char *buf;
  186. char *cpos;
  187. int sz;
  188. int remain; /* for input on fd */
  189. } *fd_data; /* indexed by fd */
  190. /* static FUNCTION(int, write_fill, (int, char*, int)); unused? */
  191. static FUNCTION(void, note_child_death, (int, int));
  192. #if CHLDWTHR
  193. static FUNCTION(void *, child_waiter, (void *));
  194. #endif
  195. /********************* General functions ****************************/
  196. /* This is used by both the drivers and general I/O, must be set early */
  197. static int max_files = -1;
  198. /*
  199. * a few variables used by the break handler
  200. */
  201. #ifdef ERTS_SMP
  202. erts_smp_atomic_t erts_break_requested;
  203. #define ERTS_SET_BREAK_REQUESTED \
  204. erts_smp_atomic_set(&erts_break_requested, (long) 1)
  205. #define ERTS_UNSET_BREAK_REQUESTED \
  206. erts_smp_atomic_set(&erts_break_requested, (long) 0)
  207. #else
  208. volatile int erts_break_requested = 0;
  209. #define ERTS_SET_BREAK_REQUESTED (erts_break_requested = 1)
  210. #define ERTS_UNSET_BREAK_REQUESTED (erts_break_requested = 0)
  211. #endif
  212. /* set early so the break handler has access to initial mode */
  213. static struct termios initial_tty_mode;
  214. static int replace_intr = 0;
  215. /* assume yes initially, ttsl_init will clear it */
  216. int using_oldshell = 1;
  217. #ifdef ERTS_ENABLE_KERNEL_POLL
  218. int erts_use_kernel_poll = 0;
  219. struct {
  220. int (*select)(ErlDrvPort, ErlDrvEvent, int, int);
  221. int (*event)(ErlDrvPort, ErlDrvEvent, ErlDrvEventData);
  222. void (*check_io_interrupt)(int);
  223. void (*check_io_interrupt_tmd)(int, long);
  224. void (*check_io)(int);
  225. Uint (*size)(void);
  226. Eterm (*info)(void *);
  227. int (*check_io_debug)(void);
  228. } io_func = {0};
  229. int
  230. driver_select(ErlDrvPort port, ErlDrvEvent event, int mode, int on)
  231. {
  232. return (*io_func.select)(port, event, mode, on);
  233. }
  234. int
  235. driver_event(ErlDrvPort port, ErlDrvEvent event, ErlDrvEventData event_data)
  236. {
  237. return (*io_func.event)(port, event, event_data);
  238. }
  239. Eterm erts_check_io_info(void *p)
  240. {
  241. return (*io_func.info)(p);
  242. }
  243. int
  244. erts_check_io_debug(void)
  245. {
  246. return (*io_func.check_io_debug)();
  247. }
  248. static void
  249. init_check_io(void)
  250. {
  251. if (erts_use_kernel_poll) {
  252. io_func.select = driver_select_kp;
  253. io_func.event = driver_event_kp;
  254. io_func.check_io_interrupt = erts_check_io_interrupt_kp;
  255. io_func.check_io_interrupt_tmd = erts_check_io_interrupt_timed_kp;
  256. io_func.check_io = erts_check_io_kp;
  257. io_func.size = erts_check_io_size_kp;
  258. io_func.info = erts_check_io_info_kp;
  259. io_func.check_io_debug = erts_check_io_debug_kp;
  260. erts_init_check_io_kp();
  261. max_files = erts_check_io_max_files_kp();
  262. }
  263. else {
  264. io_func.select = driver_select_nkp;
  265. io_func.event = driver_event_nkp;
  266. io_func.check_io_interrupt = erts_check_io_interrupt_nkp;
  267. io_func.check_io_interrupt_tmd = erts_check_io_interrupt_timed_nkp;
  268. io_func.check_io = erts_check_io_nkp;
  269. io_func.size = erts_check_io_size_nkp;
  270. io_func.info = erts_check_io_info_nkp;
  271. io_func.check_io_debug = erts_check_io_debug_nkp;
  272. erts_init_check_io_nkp();
  273. max_files = erts_check_io_max_files_nkp();
  274. }
  275. }
  276. #define ERTS_CHK_IO_INTR (*io_func.check_io_interrupt)
  277. #define ERTS_CHK_IO_INTR_TMD (*io_func.check_io_interrupt_tmd)
  278. #define ERTS_CHK_IO (*io_func.check_io)
  279. #define ERTS_CHK_IO_SZ (*io_func.size)
  280. #else /* !ERTS_ENABLE_KERNEL_POLL */
  281. static void
  282. init_check_io(void)
  283. {
  284. erts_init_check_io();
  285. max_files = erts_check_io_max_files();
  286. }
  287. #define ERTS_CHK_IO_INTR erts_check_io_interrupt
  288. #define ERTS_CHK_IO_INTR_TMD erts_check_io_interrupt_timed
  289. #define ERTS_CHK_IO erts_check_io
  290. #define ERTS_CHK_IO_SZ erts_check_io_size
  291. #endif
  292. #ifdef ERTS_SMP
  293. void
  294. erts_sys_schedule_interrupt(int set)
  295. {
  296. ERTS_CHK_IO_INTR(set);
  297. }
  298. void
  299. erts_sys_schedule_interrupt_timed(int set, long msec)
  300. {
  301. ERTS_CHK_IO_INTR_TMD(set, msec);
  302. }
  303. #endif
  304. Uint
  305. erts_sys_misc_mem_sz(void)
  306. {
  307. Uint res = ERTS_CHK_IO_SZ();
  308. res += erts_smp_atomic_read(&sys_misc_mem_sz);
  309. return res;
  310. }
  311. /*
  312. * reset the terminal to the original settings on exit
  313. */
  314. void sys_tty_reset(void)
  315. {
  316. if (using_oldshell && !replace_intr) {
  317. SET_BLOCKING(0);
  318. }
  319. else if (isatty(0)) {
  320. tcsetattr(0,TCSANOW,&initial_tty_mode);
  321. }
  322. }
  323. #ifdef __tile__
  324. /* Direct malloc to spread memory around the caches of multiple tiles. */
  325. #include <malloc.h>
  326. MALLOC_USE_HASH(1);
  327. #endif
  328. #ifdef USE_THREADS
  329. static void *ethr_internal_alloc(size_t size)
  330. {
  331. return erts_alloc_fnf(ERTS_ALC_T_ETHR_INTERNAL, (Uint) size);
  332. }
  333. static void *ethr_internal_realloc(void *ptr, size_t size)
  334. {
  335. return erts_realloc_fnf(ERTS_ALC_T_ETHR_INTERNAL, ptr, (Uint) size);
  336. }
  337. static void ethr_internal_free(void *ptr)
  338. {
  339. erts_free(ERTS_ALC_T_ETHR_INTERNAL, ptr);
  340. }
  341. #ifdef ERTS_THR_HAVE_SIG_FUNCS
  342. /*
  343. * Child thread inherits parents signal mask at creation. In order to
  344. * guarantee that the main thread will receive all SIGINT, SIGCHLD, and
  345. * SIGUSR1 signals sent to the process, we block these signals in the
  346. * parent thread when creating a new thread.
  347. */
  348. static sigset_t thr_create_sigmask;
  349. #endif /* #ifdef ERTS_THR_HAVE_SIG_FUNCS */
  350. typedef struct {
  351. #ifdef ERTS_THR_HAVE_SIG_FUNCS
  352. sigset_t saved_sigmask;
  353. #endif
  354. int unbind_child;
  355. } erts_thr_create_data_t;
  356. /*
  357. * thr_create_prepare() is called in parent thread before thread creation.
  358. * Returned value is passed as argument to thr_create_cleanup().
  359. */
  360. static void *
  361. thr_create_prepare(void)
  362. {
  363. erts_thr_create_data_t *tcdp;
  364. ErtsSchedulerData *esdp;
  365. tcdp = erts_alloc(ERTS_ALC_T_TMP, sizeof(erts_thr_create_data_t));
  366. #ifdef ERTS_THR_HAVE_SIG_FUNCS
  367. erts_thr_sigmask(SIG_BLOCK, &thr_create_sigmask, &tcdp->saved_sigmask);
  368. #endif
  369. esdp = erts_get_scheduler_data();
  370. tcdp->unbind_child = esdp && erts_is_scheduler_bound(esdp);
  371. return (void *) tcdp;
  372. }
  373. /* thr_create_cleanup() is called in parent thread after thread creation. */
  374. static void
  375. thr_create_cleanup(void *vtcdp)
  376. {
  377. erts_thr_create_data_t *tcdp = (erts_thr_create_data_t *) vtcdp;
  378. #ifdef ERTS_THR_HAVE_SIG_FUNCS
  379. /* Restore signalmask... */
  380. erts_thr_sigmask(SIG_SETMASK, &tcdp->saved_sigmask, NULL);
  381. #endif
  382. erts_free(ERTS_ALC_T_TMP, tcdp);
  383. }
  384. static void
  385. thr_create_prepare_child(void *vtcdp)
  386. {
  387. erts_thr_create_data_t *tcdp = (erts_thr_create_data_t *) vtcdp;
  388. #ifndef NO_FPE_SIGNALS
  389. /*
  390. * We do not want fp exeptions in other threads than the
  391. * scheduler threads. We enable fpe explicitly in the scheduler
  392. * threads after this.
  393. */
  394. erts_thread_disable_fpe();
  395. #endif
  396. if (tcdp->unbind_child) {
  397. erts_smp_rwmtx_rlock(&erts_cpu_bind_rwmtx);
  398. erts_unbind_from_cpu(erts_cpuinfo);
  399. erts_smp_rwmtx_runlock(&erts_cpu_bind_rwmtx);
  400. }
  401. }
  402. #endif /* #ifdef USE_THREADS */
  403. void
  404. erts_sys_pre_init(void)
  405. {
  406. erts_printf_add_cr_to_stdout = 1;
  407. erts_printf_add_cr_to_stderr = 1;
  408. #ifdef USE_THREADS
  409. {
  410. erts_thr_init_data_t eid = ERTS_THR_INIT_DATA_DEF_INITER;
  411. eid.alloc = ethr_internal_alloc;
  412. eid.realloc = ethr_internal_realloc;
  413. eid.free = ethr_internal_free;
  414. eid.thread_create_child_func = thr_create_prepare_child;
  415. /* Before creation in parent */
  416. eid.thread_create_prepare_func = thr_create_prepare;
  417. /* After creation in parent */
  418. eid.thread_create_parent_func = thr_create_cleanup,
  419. #ifdef ERTS_THR_HAVE_SIG_FUNCS
  420. sigemptyset(&thr_create_sigmask);
  421. sigaddset(&thr_create_sigmask, SIGINT); /* block interrupt */
  422. sigaddset(&thr_create_sigmask, SIGCHLD); /* block child signals */
  423. sigaddset(&thr_create_sigmask, SIGUSR1); /* block user defined signal */
  424. #endif
  425. erts_thr_init(&eid);
  426. report_exit_list = NULL;
  427. #ifdef ERTS_ENABLE_LOCK_COUNT
  428. erts_lcnt_init();
  429. #endif
  430. #if CHLDWTHR || defined(ERTS_SMP)
  431. erts_mtx_init(&chld_stat_mtx, "child_status");
  432. #endif
  433. #if CHLDWTHR
  434. #ifndef ERTS_SMP
  435. report_exit_transit_list = NULL;
  436. #endif
  437. erts_cnd_init(&chld_stat_cnd);
  438. children_alive = 0;
  439. #endif
  440. }
  441. #ifdef ERTS_SMP
  442. erts_smp_atomic_init(&erts_break_requested, 0);
  443. erts_smp_atomic_init(&erts_got_sigusr1, 0);
  444. erts_smp_atomic_init(&have_prepared_crash_dump, 0);
  445. #else
  446. erts_break_requested = 0;
  447. erts_got_sigusr1 = 0;
  448. have_prepared_crash_dump = 0;
  449. #endif
  450. #if !CHLDWTHR && !defined(ERTS_SMP)
  451. children_died = 0;
  452. #endif
  453. #endif /* USE_THREADS */
  454. erts_smp_atomic_init(&sys_misc_mem_sz, 0);
  455. erts_smp_rwmtx_init(&environ_rwmtx, "environ");
  456. }
  457. void
  458. erl_sys_init(void)
  459. {
  460. #if !DISABLE_VFORK
  461. int res;
  462. char bindir[MAXPATHLEN];
  463. size_t bindirsz = sizeof(bindir);
  464. Uint csp_path_sz;
  465. res = erts_sys_getenv("BINDIR", bindir, &bindirsz);
  466. if (res != 0) {
  467. if (res < 0)
  468. erl_exit(-1,
  469. "Environment variable BINDIR is not set\n");
  470. if (res > 0)
  471. erl_exit(-1,
  472. "Value of environment variable BINDIR is too large\n");
  473. }
  474. if (bindir[0] != DIR_SEPARATOR_CHAR)
  475. erl_exit(-1,
  476. "Environment variable BINDIR does not contain an"
  477. " absolute path\n");
  478. csp_path_sz = (strlen(bindir)
  479. + 1 /* DIR_SEPARATOR_CHAR */
  480. + sizeof(CHILD_SETUP_PROG_NAME)
  481. + 1);
  482. child_setup_prog = erts_alloc(ERTS_ALC_T_CS_PROG_PATH, csp_path_sz);
  483. erts_smp_atomic_add(&sys_misc_mem_sz, csp_path_sz);
  484. sprintf(child_setup_prog,
  485. "%s%c%s",
  486. bindir,
  487. DIR_SEPARATOR_CHAR,
  488. CHILD_SETUP_PROG_NAME);
  489. #endif
  490. #ifdef USE_SETLINEBUF
  491. setlinebuf(stdout);
  492. #else
  493. setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  494. #endif
  495. erts_sys_init_float();
  496. /* we save this so the break handler can set and reset it properly */
  497. /* also so that we can reset on exit (break handler or not) */
  498. if (isatty(0)) {
  499. tcgetattr(0,&initial_tty_mode);
  500. }
  501. tzset(); /* Required at least for NetBSD with localtime_r() */
  502. }
  503. /* signal handling */
  504. #ifdef SIG_SIGSET /* Old SysV */
  505. RETSIGTYPE (*sys_sigset(sig, func))()
  506. int sig;
  507. RETSIGTYPE (*func)();
  508. {
  509. return(sigset(sig, func));
  510. }
  511. void sys_sigblock(int sig)
  512. {
  513. sighold(sig);
  514. }
  515. void sys_sigrelease(int sig)
  516. {
  517. sigrelse(sig);
  518. }
  519. #else /* !SIG_SIGSET */
  520. #ifdef SIG_SIGNAL /* Old BSD */
  521. RETSIGTYPE (*sys_sigset(sig, func))(int, int)
  522. int sig;
  523. RETSIGTYPE (*func)();
  524. {
  525. return(signal(sig, func));
  526. }
  527. sys_sigblock(int sig)
  528. {
  529. sigblock(sig);
  530. }
  531. sys_sigrelease(int sig)
  532. {
  533. sigsetmask(sigblock(0) & ~sigmask(sig));
  534. }
  535. #else /* !SIG_SIGNAL */ /* The True Way - POSIX!:-) */
  536. RETSIGTYPE (*sys_sigset(int sig, RETSIGTYPE (*func)(int)))(int)
  537. {
  538. struct sigaction act, oact;
  539. sigemptyset(&act.sa_mask);
  540. act.sa_flags = 0;
  541. act.sa_handler = func;
  542. sigaction(sig, &act, &oact);
  543. return(oact.sa_handler);
  544. }
  545. #ifdef USE_THREADS
  546. #undef sigprocmask
  547. #define sigprocmask erts_thr_sigmask
  548. #endif
  549. void sys_sigblock(int sig)
  550. {
  551. sigset_t mask;
  552. sigemptyset(&mask);
  553. sigaddset(&mask, sig);
  554. sigprocmask(SIG_BLOCK, &mask, (sigset_t *)NULL);
  555. }
  556. void sys_sigrelease(int sig)
  557. {
  558. sigset_t mask;
  559. sigemptyset(&mask);
  560. sigaddset(&mask, sig);
  561. sigprocmask(SIG_UNBLOCK, &mask, (sigset_t *)NULL);
  562. }
  563. #endif /* !SIG_SIGNAL */
  564. #endif /* !SIG_SIGSET */
  565. #if (0) /* not used? -- gordon */
  566. static void (*break_func)();
  567. static RETSIGTYPE break_handler(int sig)
  568. {
  569. #ifdef QNX
  570. /* Turn off SIGCHLD during break processing */
  571. sys_sigblock(SIGCHLD);
  572. #endif
  573. (*break_func)();
  574. #ifdef QNX
  575. sys_sigrelease(SIGCHLD);
  576. #endif
  577. }
  578. #endif /* 0 */
  579. static ERTS_INLINE void
  580. prepare_crash_dump(void)
  581. {
  582. int i, max;
  583. char env[21]; /* enough to hold any 64-bit integer */
  584. size_t envsz;
  585. if (ERTS_PREPARED_CRASH_DUMP)
  586. return; /* We have already been called */
  587. /* Make sure we unregister at epmd (unknown fd) and get at least
  588. one free filedescriptor (for erl_crash.dump) */
  589. max = max_files;
  590. if (max < 1024)
  591. max = 1024;
  592. for (i = 3; i < max; i++) {
  593. #if defined(ERTS_SMP)
  594. /* We don't want to close the signal notification pipe... */
  595. if (i == sig_notify_fds[0] || i == sig_notify_fds[1])
  596. continue;
  597. #elif defined(USE_THREADS)
  598. /* We don't want to close the async notification pipe... */
  599. if (i == async_fd[0] || i == async_fd[1])
  600. continue;
  601. #endif
  602. close(i);
  603. }
  604. envsz = sizeof(env);
  605. i = erts_sys_getenv("ERL_CRASH_DUMP_NICE", env, &envsz);
  606. if (i >= 0) {
  607. int nice_val;
  608. nice_val = i != 0 ? 0 : atoi(env);
  609. if (nice_val > 39) {
  610. nice_val = 39;
  611. }
  612. erts_silence_warn_unused_result(nice(nice_val));
  613. }
  614. envsz = sizeof(env);
  615. i = erts_sys_getenv("ERL_CRASH_DUMP_SECONDS", env, &envsz);
  616. if (i >= 0) {
  617. unsigned sec;
  618. sec = (unsigned) i != 0 ? 0 : atoi(env);
  619. alarm(sec);
  620. }
  621. }
  622. void
  623. erts_sys_prepare_crash_dump(void)
  624. {
  625. prepare_crash_dump();
  626. }
  627. static ERTS_INLINE void
  628. break_requested(void)
  629. {
  630. /*
  631. * just set a flag - checked for and handled by
  632. * scheduler threads erts_check_io() (not signal handler).
  633. */
  634. #ifdef DEBUG
  635. fprintf(stderr,"break!\n");
  636. #endif
  637. if (ERTS_BREAK_REQUESTED)
  638. erl_exit(ERTS_INTR_EXIT, "");
  639. ERTS_SET_BREAK_REQUESTED;
  640. ERTS_CHK_IO_INTR(1); /* Make sure we don't sleep in poll */
  641. }
  642. /* set up signal handlers for break and quit */
  643. #if (defined(SIG_SIGSET) || defined(SIG_SIGNAL))
  644. static RETSIGTYPE request_break(void)
  645. #else
  646. static RETSIGTYPE request_break(int signum)
  647. #endif
  648. {
  649. #ifdef ERTS_SMP
  650. smp_sig_notify('I');
  651. #else
  652. break_requested();
  653. #endif
  654. }
  655. static ERTS_INLINE void
  656. sigusr1_exit(void)
  657. {
  658. /* We do this at interrupt level, since the main reason for
  659. wanting to generate a crash dump in this way is that the emulator
  660. is hung somewhere, so it won't be able to poll any flag we set here.
  661. */
  662. ERTS_SET_GOT_SIGUSR1;
  663. prepare_crash_dump();
  664. erl_exit(1, "Received SIGUSR1\n");
  665. }
  666. #ifdef ETHR_UNUSABLE_SIGUSRX
  667. #warning "Unusable SIGUSR1 & SIGUSR2. Disabling use of these signals"
  668. #endif
  669. #ifndef ETHR_UNUSABLE_SIGUSRX
  670. #if (defined(SIG_SIGSET) || defined(SIG_SIGNAL))
  671. static RETSIGTYPE user_signal1(void)
  672. #else
  673. static RETSIGTYPE user_signal1(int signum)
  674. #endif
  675. {
  676. #ifdef ERTS_SMP
  677. smp_sig_notify('1');
  678. #else
  679. sigusr1_exit();
  680. #endif
  681. }
  682. #ifdef QUANTIFY
  683. #if (defined(SIG_SIGSET) || defined(SIG_SIGNAL))
  684. static RETSIGTYPE user_signal2(void)
  685. #else
  686. static RETSIGTYPE user_signal2(int signum)
  687. #endif
  688. {
  689. #ifdef ERTS_SMP
  690. smp_sig_notify('2');
  691. #else
  692. quantify_save_data();
  693. #endif
  694. }
  695. #endif
  696. #endif /* #ifndef ETHR_UNUSABLE_SIGUSRX */
  697. static void
  698. quit_requested(void)
  699. {
  700. erl_exit(ERTS_INTR_EXIT, "");
  701. }
  702. #if (defined(SIG_SIGSET) || defined(SIG_SIGNAL))
  703. static RETSIGTYPE do_quit(void)
  704. #else
  705. static RETSIGTYPE do_quit(int signum)
  706. #endif
  707. {
  708. #ifdef ERTS_SMP
  709. smp_sig_notify('Q');
  710. #else
  711. quit_requested();
  712. #endif
  713. }
  714. /* Disable break */
  715. void erts_set_ignore_break(void) {
  716. sys_sigset(SIGINT, SIG_IGN);
  717. sys_sigset(SIGQUIT, SIG_IGN);
  718. sys_sigset(SIGTSTP, SIG_IGN);
  719. }
  720. /* Don't use ctrl-c for break handler but let it be
  721. used by the shell instead (see user_drv.erl) */
  722. void erts_replace_intr(void) {
  723. struct termios mode;
  724. if (isatty(0)) {
  725. tcgetattr(0, &mode);
  726. /* here's an example of how to replace ctrl-c with ctrl-u */
  727. /* mode.c_cc[VKILL] = 0;
  728. mode.c_cc[VINTR] = CKILL; */
  729. mode.c_cc[VINTR] = 0; /* disable ctrl-c */
  730. tcsetattr(0, TCSANOW, &mode);
  731. replace_intr = 1;
  732. }
  733. }
  734. void init_break_handler(void)
  735. {
  736. sys_sigset(SIGINT, request_break);
  737. #ifndef ETHR_UNUSABLE_SIGUSRX
  738. sys_sigset(SIGUSR1, user_signal1);
  739. #ifdef QUANTIFY
  740. sys_sigset(SIGUSR2, user_signal2);
  741. #endif
  742. #endif /* #ifndef ETHR_UNUSABLE_SIGUSRX */
  743. sys_sigset(SIGQUIT, do_quit);
  744. }
  745. int sys_max_files(void)
  746. {
  747. return(max_files);
  748. }
  749. static void block_signals(void)
  750. {
  751. #if !CHLDWTHR
  752. sys_sigblock(SIGCHLD);
  753. #endif
  754. #ifndef ERTS_SMP
  755. sys_sigblock(SIGINT);
  756. #ifndef ETHR_UNUSABLE_SIGUSRX
  757. sys_sigblock(SIGUSR1);
  758. #endif
  759. #endif
  760. }
  761. static void unblock_signals(void)
  762. {
  763. /* Update erl_child_setup.c if changed */
  764. #if !CHLDWTHR
  765. sys_sigrelease(SIGCHLD);
  766. #endif
  767. #ifndef ERTS_SMP
  768. sys_sigrelease(SIGINT);
  769. #ifndef ETHR_UNUSABLE_SIGUSRX
  770. sys_sigrelease(SIGUSR1);
  771. #endif /* #ifndef ETHR_UNUSABLE_SIGUSRX */
  772. #endif
  773. }
  774. /************************** Time stuff **************************/
  775. #ifdef HAVE_GETHRTIME
  776. #ifdef GETHRTIME_WITH_CLOCK_GETTIME
  777. SysHrTime sys_gethrtime(void)
  778. {
  779. struct timespec ts;
  780. long long result;
  781. if (clock_gettime(CLOCK_MONOTONIC,&ts) != 0) {
  782. erl_exit(1,"Fatal, could not get clock_monotonic value!, "
  783. "errno = %d\n", errno);
  784. }
  785. result = ((long long) ts.tv_sec) * 1000000000LL +
  786. ((long long) ts.tv_nsec);
  787. return (SysHrTime) result;
  788. }
  789. #endif
  790. #endif
  791. /************************** OS info *******************************/
  792. /* Used by erlang:info/1. */
  793. /* (This code was formerly in drv.XXX/XXX_os_drv.c) */
  794. char os_type[] = "unix";
  795. static int
  796. get_number(char **str_ptr)
  797. {
  798. char* s = *str_ptr; /* Pointer to beginning of string. */
  799. char* dot; /* Pointer to dot in string or NULL. */
  800. if (!isdigit((int) *s))
  801. return 0;
  802. if ((dot = strchr(s, '.')) == NULL) {
  803. *str_ptr = s+strlen(s);
  804. return atoi(s);
  805. } else {
  806. *dot = '\0';
  807. *str_ptr = dot+1;
  808. return atoi(s);
  809. }
  810. }
  811. void
  812. os_flavor(char* namebuf, /* Where to return the name. */
  813. unsigned size) /* Size of name buffer. */
  814. {
  815. static int called = 0;
  816. static struct utsname uts; /* Information about the system. */
  817. if (!called) {
  818. char* s;
  819. (void) uname(&uts);
  820. called = 1;
  821. for (s = uts.sysname; *s; s++) {
  822. if (isupper((int) *s)) {
  823. *s = tolower((int) *s);
  824. }
  825. }
  826. }
  827. strcpy(namebuf, uts.sysname);
  828. }
  829. void
  830. os_version(pMajor, pMinor, pBuild)
  831. int* pMajor; /* Pointer to major version. */
  832. int* pMinor; /* Pointer to minor version. */
  833. int* pBuild; /* Pointer to build number. */
  834. {
  835. struct utsname uts; /* Information about the system. */
  836. char* release; /* Pointer to the release string:
  837. * X.Y or X.Y.Z.
  838. */
  839. (void) uname(&uts);
  840. release = uts.release;
  841. *pMajor = get_number(&release);
  842. *pMinor = get_number(&release);
  843. *pBuild = get_number(&release);
  844. }
  845. void init_getenv_state(GETENV_STATE *state)
  846. {
  847. erts_smp_rwmtx_rlock(&environ_rwmtx);
  848. *state = NULL;
  849. }
  850. char *getenv_string(GETENV_STATE *state0)
  851. {
  852. char **state = (char **) *state0;
  853. char *cp;
  854. ERTS_SMP_LC_ASSERT(erts_smp_lc_rwmtx_is_rlocked(&environ_rwmtx));
  855. if (state == NULL)
  856. state = environ;
  857. cp = *state++;
  858. *state0 = (GETENV_STATE) state;
  859. return cp;
  860. }
  861. void fini_getenv_state(GETENV_STATE *state)
  862. {
  863. *state = NULL;
  864. erts_smp_rwmtx_runlock(&environ_rwmtx);
  865. }
  866. /************************** Port I/O *******************************/
  867. /* I. Common stuff */
  868. /*
  869. * Decreasing the size of it below 16384 is not allowed.
  870. */
  871. /* II. The spawn/fd/vanilla drivers */
  872. #define ERTS_SYS_READ_BUF_SZ (64*1024)
  873. /* This data is shared by these drivers - initialized by spawn_init() */
  874. static struct driver_data {
  875. int port_num, ofd, packet_bytes;
  876. ErtsSysReportExit *report_exit;
  877. int pid;
  878. int alive;
  879. int status;
  880. } *driver_data; /* indexed by fd */
  881. /* Driver interfaces */
  882. static ErlDrvData spawn_start(ErlDrvPort, char*, SysDriverOpts*);
  883. static ErlDrvData fd_start(ErlDrvPort, char*, SysDriverOpts*);
  884. static int fd_control(ErlDrvData, unsigned int, char *, int, char **, int);
  885. static ErlDrvData vanilla_start(ErlDrvPort, char*, SysDriverOpts*);
  886. static int spawn_init(void);
  887. static void fd_stop(ErlDrvData);
  888. static void stop(ErlDrvData);
  889. static void ready_input(ErlDrvData, ErlDrvEvent);
  890. static void ready_output(ErlDrvData, ErlDrvEvent);
  891. static void output(ErlDrvData, char*, int);
  892. static void outputv(ErlDrvData, ErlIOVec*);
  893. static void stop_select(ErlDrvEvent, void*);
  894. struct erl_drv_entry spawn_driver_entry = {
  895. spawn_init,
  896. spawn_start,
  897. stop,
  898. output,
  899. ready_input,
  900. ready_output,
  901. "spawn",
  902. NULL,
  903. NULL,
  904. NULL,
  905. NULL,
  906. NULL,
  907. NULL,
  908. NULL,
  909. NULL,
  910. NULL,
  911. ERL_DRV_EXTENDED_MARKER,
  912. ERL_DRV_EXTENDED_MAJOR_VERSION,
  913. ERL_DRV_EXTENDED_MINOR_VERSION,
  914. ERL_DRV_FLAG_USE_PORT_LOCKING,
  915. NULL, NULL,
  916. stop_select
  917. };
  918. struct erl_drv_entry fd_driver_entry = {
  919. NULL,
  920. fd_start,
  921. fd_stop,
  922. output,
  923. ready_input,
  924. ready_output,
  925. "fd",
  926. NULL,
  927. NULL,
  928. fd_control,
  929. NULL,
  930. outputv,
  931. NULL, /* ready_async */
  932. NULL, /* flush */
  933. NULL, /* call */
  934. NULL, /* event */
  935. ERL_DRV_EXTENDED_MARKER,
  936. ERL_DRV_EXTENDED_MAJOR_VERSION,
  937. ERL_DRV_EXTENDED_MINOR_VERSION,
  938. 0, /* ERL_DRV_FLAGs */
  939. NULL, /* handle2 */
  940. NULL, /* process_exit */
  941. stop_select
  942. };
  943. struct erl_drv_entry vanilla_driver_entry = {
  944. NULL,
  945. vanilla_start,
  946. stop,
  947. output,
  948. ready_input,
  949. ready_output,
  950. "vanilla",
  951. NULL,
  952. NULL,
  953. NULL,
  954. NULL,
  955. NULL,
  956. NULL,
  957. NULL, /* flush */
  958. NULL, /* call */
  959. NULL, /* event */
  960. ERL_DRV_EXTENDED_MARKER,
  961. ERL_DRV_EXTENDED_MAJOR_VERSION,
  962. ERL_DRV_EXTENDED_MINOR_VERSION,
  963. 0, /* ERL_DRV_FLAGs */
  964. NULL, /* handle2 */
  965. NULL, /* process_exit */
  966. stop_select
  967. };
  968. #if defined(USE_THREADS) && !defined(ERTS_SMP)
  969. static int async_drv_init(void);
  970. static ErlDrvData async_drv_start(ErlDrvPort, char*, SysDriverOpts*);
  971. static void async_drv_stop(ErlDrvData);
  972. static void async_drv_input(ErlDrvData, ErlDrvEvent);
  973. /* INTERNAL use only */
  974. struct erl_drv_entry async_driver_entry = {
  975. async_drv_init,
  976. async_drv_start,
  977. async_drv_stop,
  978. NULL,
  979. async_drv_input,
  980. NULL,
  981. "async",
  982. NULL,
  983. NULL,
  984. NULL,
  985. NULL,
  986. NULL,
  987. NULL
  988. };
  989. #endif
  990. /* Handle SIGCHLD signals. */
  991. #if (defined(SIG_SIGSET) || defined(SIG_SIGNAL))
  992. static RETSIGTYPE onchld(void)
  993. #else
  994. static RETSIGTYPE onchld(int signum)
  995. #endif
  996. {
  997. #if CHLDWTHR
  998. ASSERT(0); /* We should *never* catch a SIGCHLD signal */
  999. #elif defined(ERTS_SMP)
  1000. smp_sig_notify('C');
  1001. #else
  1002. children_died = 1;
  1003. ERTS_CHK_IO_INTR(1); /* Make sure we don't sleep in poll */
  1004. #endif
  1005. }
  1006. static int set_driver_data(int port_num,
  1007. int ifd,
  1008. int ofd,
  1009. int packet_bytes,
  1010. int read_write,
  1011. int exit_status,
  1012. int pid)
  1013. {
  1014. ErtsSysReportExit *report_exit;
  1015. if (!exit_status)
  1016. report_exit = NULL;
  1017. else {
  1018. report_exit = erts_alloc(ERTS_ALC_T_PRT_REP_EXIT,
  1019. sizeof(ErtsSysReportExit));
  1020. report_exit->next = report_exit_list;
  1021. report_exit->port = erts_port[port_num].id;
  1022. report_exit->pid = pid;
  1023. report_exit->ifd = read_write & DO_READ ? ifd : -1;
  1024. report_exit->ofd = read_write & DO_WRITE ? ofd : -1;
  1025. #if CHLDWTHR && !defined(ERTS_SMP)
  1026. report_exit->status = 0;
  1027. #endif
  1028. report_exit_list = report_exit;
  1029. }
  1030. if (read_write & DO_READ) {
  1031. driver_data[ifd].packet_bytes = packet_bytes;
  1032. driver_data[ifd].port_num = port_num;
  1033. driver_data[ifd].report_exit = report_exit;
  1034. driver_data[ifd].pid = pid;
  1035. driver_data[ifd].alive = 1;
  1036. driver_data[ifd].status = 0;
  1037. if (read_write & DO_WRITE) {
  1038. driver_data[ifd].ofd = ofd;
  1039. if (ifd != ofd)
  1040. driver_data[ofd] = driver_data[ifd]; /* structure copy */
  1041. } else { /* DO_READ only */
  1042. driver_data[ifd].ofd = -1;
  1043. }
  1044. (void) driver_select(port_num, ifd, (ERL_DRV_READ|ERL_DRV_USE), 1);
  1045. return(ifd);
  1046. } else { /* DO_WRITE only */
  1047. driver_data[ofd].packet_bytes = packet_bytes;
  1048. driver_data[ofd].port_num = port_num;
  1049. driver_data[ofd].report_exit = report_exit;
  1050. driver_data[ofd].ofd = ofd;
  1051. driver_data[ofd].pid = pid;
  1052. driver_data[ofd].alive = 1;
  1053. driver_data[ofd].status = 0;
  1054. return(ofd);
  1055. }
  1056. }
  1057. static int spawn_init()
  1058. {
  1059. int i;
  1060. #if CHLDWTHR
  1061. erts_thr_opts_t thr_opts = ERTS_THR_OPTS_DEFAULT_INITER;
  1062. thr_opts.detached = 0;
  1063. thr_opts.suggested_stack_size = 0; /* Smallest possible */
  1064. #endif
  1065. sys_sigset(SIGPIPE, SIG_IGN); /* Ignore - we'll handle the write failure */
  1066. driver_data = (struct driver_data *)
  1067. erts_alloc(ERTS_ALC_T_DRV_TAB, max_files * sizeof(struct driver_data));
  1068. erts_smp_atomic_add(&sys_misc_mem_sz,
  1069. max_files * sizeof(struct driver_data));
  1070. for (i = 0; i < max_files; i++)
  1071. driver_data[i].pid = -1;
  1072. #if CHLDWTHR
  1073. sys_sigblock(SIGCHLD);
  1074. #endif
  1075. sys_sigset(SIGCHLD, onchld); /* Reap children */
  1076. #if CHLDWTHR
  1077. erts_thr_create(&child_waiter_tid, child_waiter, NULL, &thr_opts);
  1078. #endif
  1079. return 1;
  1080. }
  1081. static void close_pipes(int ifd[2], int ofd[2], int read_write)
  1082. {
  1083. if (read_write & DO_READ) {
  1084. (void) close(ifd[0]);
  1085. (void) close(ifd[1]);
  1086. }
  1087. if (read_write & DO_WRITE) {
  1088. (void) close(ofd[0]);
  1089. (void) close(ofd[1]);
  1090. }
  1091. }
  1092. static void init_fd_data(int fd, int prt)
  1093. {
  1094. fd_data[fd].buf = NULL;
  1095. fd_data[fd].cpos = NULL;
  1096. fd_data[fd].remain = 0;
  1097. fd_data[fd].sz = 0;
  1098. fd_data[fd].psz = 0;
  1099. }
  1100. static char **build_unix_environment(char *block)
  1101. {
  1102. int i;
  1103. int j;
  1104. int len;
  1105. char *cp;
  1106. char **cpp;
  1107. char** old_env;
  1108. ERTS_SMP_LC_ASSERT(erts_smp_lc_rwmtx_is_rlocked(&environ_rwmtx));
  1109. cp = block;
  1110. len = 0;
  1111. while (*cp != '\0') {
  1112. cp += strlen(cp) + 1;
  1113. len++;
  1114. }
  1115. old_env = environ;
  1116. while (*old_env++ != NULL) {
  1117. len++;
  1118. }
  1119. cpp = (char **) erts_alloc_fnf(ERTS_ALC_T_ENVIRONMENT,
  1120. sizeof(char *) * (len+1));
  1121. if (cpp == NULL) {
  1122. return NULL;
  1123. }
  1124. cp = block;
  1125. len = 0;
  1126. while (*cp != '\0') {
  1127. cpp[len] = cp;
  1128. cp += strlen(cp) + 1;
  1129. len++;
  1130. }
  1131. i = len;
  1132. for (old_env = environ; *old_env; old_env++) {
  1133. char* old = *old_env;
  1134. for (j = 0; j < len; j++) {
  1135. char *s, *t;
  1136. s = cpp[j];
  1137. t = old;
  1138. while (*s == *t && *s != '=') {
  1139. s++, t++;
  1140. }
  1141. if (*s == '=' && *t == '=') {
  1142. break;
  1143. }
  1144. }
  1145. if (j == len) { /* New version not found */
  1146. cpp[len++] = old;
  1147. }
  1148. }
  1149. for (j = 0; j < i; j++) {
  1150. if (cpp[j][strlen(cpp[j])-1] == '=') {
  1151. cpp[j] = cpp[--len];
  1152. }
  1153. }
  1154. cpp[len] = NULL;
  1155. return cpp;
  1156. }
  1157. /*
  1158. [arndt] In most Unix systems, including Solaris 2.5, 'fork' allocates memory
  1159. in swap space for the child of a 'fork', whereas 'vfork' does not do this.
  1160. The natural call to use here is therefore 'vfork'. Due to a bug in
  1161. 'vfork' in Solaris 2.5 (apparently fixed in 2.6), using 'vfork'
  1162. can be dangerous in what seems to be these circumstances:
  1163. If the child code under a vfork sets the signal action to SIG_DFL
  1164. (or SIG_IGN)
  1165. for any signal which was previously set to a signal handler, the
  1166. state of the parent is clobbered, so that the later arrival of
  1167. such a signal yields a sigsegv in the parent. If the signal was
  1168. not set to a signal handler, but ignored, all seems to work.
  1169. If you change the forking code below, beware of this.
  1170. */
  1171. static ErlDrvData spawn_start(ErlDrvPort port_num, char* name, SysDriverOpts* opts)
  1172. {
  1173. #define CMD_LINE_PREFIX_STR "exec "
  1174. #define CMD_LINE_PREFIX_STR_SZ (sizeof(CMD_LINE_PREFIX_STR) - 1)
  1175. int ifd[2], ofd[2], len, pid, i;
  1176. char **volatile new_environ; /* volatile since a vfork() then cannot
  1177. cause 'new_environ' to be clobbered
  1178. in the parent process. */
  1179. int saved_errno;
  1180. long res;
  1181. char *cmd_line;
  1182. #ifndef QNX
  1183. int unbind;
  1184. #endif
  1185. #if !DISABLE_VFORK
  1186. int no_vfork;
  1187. size_t no_vfork_sz = sizeof(no_vfork);
  1188. no_vfork = (erts_sys_getenv("ERL_NO_VFORK",
  1189. (char *) &no_vfork,
  1190. &no_vfork_sz) >= 0);
  1191. #endif
  1192. switch (opts->read_write) {
  1193. case DO_READ:
  1194. if (pipe(ifd) < 0)
  1195. return ERL_DRV_ERROR_ERRNO;
  1196. if (ifd[0] >= max_files) {
  1197. close_pipes(ifd, ofd, opts->read_write);
  1198. errno = EMFILE;
  1199. return ERL_DRV_ERROR_ERRNO;
  1200. }
  1201. ofd[1] = -1; /* keep purify happy */
  1202. break;
  1203. case DO_WRITE:
  1204. if (pipe(ofd) < 0) return ERL_DRV_ERROR_ERRNO;
  1205. if (ofd[1] >= max_files) {
  1206. close_pipes(ifd, ofd, opts->read_write);
  1207. errno = EMFILE;
  1208. return ERL_DRV_ERROR_ERRNO;
  1209. }
  1210. ifd[0] = -1; /* keep purify happy */
  1211. break;
  1212. case DO_READ|DO_WRITE:
  1213. if (pipe(ifd) < 0) return ERL_DRV_ERROR_ERRNO;
  1214. errno = EMFILE; /* default for next two conditions */
  1215. if (ifd[0] >= max_files || pipe(ofd) < 0) {
  1216. close_pipes(ifd, ofd, DO_READ);
  1217. return ERL_DRV_ERROR_ERRNO;
  1218. }
  1219. if (ofd[1] >= max_files) {
  1220. close_pipes(ifd, ofd, opts->read_write);
  1221. errno = EMFILE;
  1222. return ERL_DRV_ERROR_ERRNO;
  1223. }
  1224. break;
  1225. default:
  1226. ASSERT(0);
  1227. return ERL_DRV_ERROR_GENERAL;
  1228. }
  1229. if (opts->spawn_type == ERTS_SPAWN_EXECUTABLE) {
  1230. /* started with spawn_executable, not with spawn */
  1231. len = strlen(name);
  1232. cmd_line = (char *) erts_alloc_fnf(ERTS_ALC_T_TMP, len + 1);
  1233. if (!cmd_line) {
  1234. close_pipes(ifd, ofd, opts->read_write);
  1235. errno = ENOMEM;
  1236. return ERL_DRV_ERROR_ERRNO;
  1237. }
  1238. memcpy((void *) cmd_line,(void *) name, len);
  1239. cmd_line[len] = '\0';
  1240. if (access(cmd_line,X_OK) != 0) {
  1241. int save_errno = errno;
  1242. erts_free(ERTS_ALC_T_TMP, cmd_line);
  1243. errno = save_errno;
  1244. return ERL_DRV_ERROR_ERRNO;
  1245. }
  1246. } else {
  1247. /* make the string suitable for giving to "sh" */
  1248. len = strlen(name);
  1249. cmd_line = (char *) erts_alloc_fnf(ERTS_ALC_T_TMP,
  1250. CMD_LINE_PREFIX_STR_SZ + len + 1);
  1251. if (!cmd_line) {
  1252. close_pipes(ifd, ofd, opts->read_write);
  1253. errno = ENOMEM;
  1254. return ERL_DRV_ERROR_ERRNO;
  1255. }
  1256. memcpy((void *) cmd_line,
  1257. (void *) CMD_LINE_PREFIX_STR,
  1258. CMD_LINE_PREFIX_STR_SZ);
  1259. memcpy((void *) (cmd_line + CMD_LINE_PREFIX_STR_SZ), (void *) name, len);
  1260. cmd_line[CMD_LINE_PREFIX_STR_SZ + len] = '\0';
  1261. }
  1262. erts_smp_rwmtx_rlock(&environ_rwmtx);
  1263. if (opts->envir == NULL) {
  1264. new_environ = environ;
  1265. } else if ((new_environ = build_unix_environment(opts->envir)) == NULL) {
  1266. erts_smp_rwmtx_runlock(&environ_rwmtx);
  1267. erts_free(ERTS_ALC_T_TMP, (void *) cmd_line);
  1268. errno = ENOMEM;
  1269. return ERL_DRV_ERROR_ERRNO;
  1270. }
  1271. #ifndef QNX
  1272. /* Block child from SIGINT and SIGUSR1. Must be before fork()
  1273. to be safe. */
  1274. block_signals();
  1275. CHLD_STAT_LOCK;
  1276. unbind = erts_is_scheduler_bound(NULL);
  1277. if (unbind)
  1278. erts_smp_rwmtx_rlock(&erts_cpu_bind_rwmtx);
  1279. #if !DISABLE_VFORK
  1280. /* See fork/vfork discussion before this function. */
  1281. if (no_vfork) {
  1282. #endif
  1283. DEBUGF(("Using fork\n"));
  1284. pid = fork();
  1285. if (pid == 0) {
  1286. /* The child! Setup child... */
  1287. if (unbind && erts_unbind_from_cpu(erts_cpuinfo) != 0)
  1288. goto child_error;
  1289. /* OBSERVE!
  1290. * Keep child setup after vfork() (implemented below and in
  1291. * erl_child_setup.c) up to date if changes are made here.
  1292. */
  1293. if (opts->use_stdio) {
  1294. if (opts->read_write & DO_READ) {
  1295. /* stdout for process */
  1296. if (dup2(ifd[1], 1) < 0)
  1297. goto child_error;
  1298. if(opts->redir_stderr)
  1299. /* stderr for process */
  1300. if (dup2(ifd[1], 2) < 0)
  1301. goto child_error;
  1302. }
  1303. if (opts->read_write & DO_WRITE)
  1304. /* stdin for process */
  1305. if (dup2(ofd[0], 0) < 0)
  1306. goto child_error;
  1307. }
  1308. else { /* XXX will fail if ofd[0] == 4 (unlikely..) */
  1309. if (opts->read_write & DO_READ)
  1310. if (dup2(ifd[1], 4) < 0)
  1311. goto child_error;
  1312. if (opts->read_write & DO_WRITE)
  1313. if (dup2(ofd[0], 3) < 0)
  1314. goto child_error;
  1315. }
  1316. for (i = opts->use_stdio ? 3 : 5; i < max_files; i++)
  1317. (void) close(i);
  1318. if (opts->wd && chdir(opts->wd) < 0)
  1319. goto child_error;
  1320. #if defined(USE_SETPGRP_NOARGS) /* SysV */
  1321. (void) setpgrp();
  1322. #elif defined(USE_SETPGRP) /* BSD */
  1323. (void) setpgrp(0, getpid());
  1324. #else /* POSIX */
  1325. (void) setsid();
  1326. #endif
  1327. unblock_signals();
  1328. if (opts->spawn_type == ERTS_SPAWN_EXECUTABLE) {
  1329. if (opts->argv == NULL) {
  1330. execle(cmd_line,cmd_line,(char *) NULL, new_environ);
  1331. } else {
  1332. if (opts->argv[0] == erts_default_arg0) {
  1333. opts->argv[0] = cmd_line;
  1334. }
  1335. execve(cmd_line, opts->argv, new_environ);
  1336. if (opts->argv[0] == cmd_line) {
  1337. opts->argv[0] = erts_default_arg0;
  1338. }
  1339. }
  1340. } else {
  1341. execle("/bin/sh", "sh", "-c", cmd_line, (char *) NULL, new_environ);
  1342. }
  1343. child_error:
  1344. _exit(1);
  1345. }
  1346. #if !DISABLE_VFORK
  1347. }
  1348. else { /* Use vfork() */
  1349. char **cs_argv= erts_alloc(ERTS_ALC_T_TMP,(CS_ARGV_NO_OF_ARGS + 1)*
  1350. sizeof(char *));
  1351. char fd_close_range[44]; /* 44 bytes are enough to */
  1352. char dup2_op[CS_ARGV_NO_OF_DUP2_OPS][44]; /* hold any "%d:%d" string */
  1353. /* on a 64-bit machine. */
  1354. /* Setup argv[] for the child setup program (implemented in
  1355. erl_child_setup.c) */
  1356. i = 0;
  1357. if (opts->use_stdio) {
  1358. if (opts->read_write & DO_READ){
  1359. /* stdout for process */
  1360. sprintf(&dup2_op[i++][0], "%d:%d", ifd[1], 1);
  1361. if(opts->redir_stderr)
  1362. /* stderr for process */
  1363. sprintf(&dup2_op[i++][0], "%d:%d", ifd[1], 2);
  1364. }
  1365. if (opts->read_write & DO_WRITE)
  1366. /* stdin for process */
  1367. sprintf(&dup2_op[i++][0], "%d:%d", ofd[0], 0);
  1368. } else { /* XXX will fail if ofd[0] == 4 (unlikely..) */
  1369. if (opts->read_write & DO_READ)
  1370. sprintf(&dup2_op[i++][0], "%d:%d", ifd[1], 4);
  1371. if (opts->read_write & DO_WRITE)
  1372. sprintf(&dup2_op[i++][0], "%d:%d", ofd[0], 3);
  1373. }
  1374. for (; i < CS_ARGV_NO_OF_DUP2_OPS; i++)
  1375. strcpy(&dup2_op[i][0], "-");
  1376. sprintf(fd_close_range, "%d:%d", opts->use_stdio ? 3 : 5, max_files-1);
  1377. cs_argv[CS_ARGV_PROGNAME_IX] = child_setup_prog;
  1378. cs_argv[CS_ARGV_WD_IX] = opts->wd ? opts->wd : ".";
  1379. cs_argv[CS_ARGV_UNBIND_IX]
  1380. = (unbind ? erts_get_unbind_from_cpu_str(erts_cpuinfo) : "false");
  1381. cs_argv[CS_ARGV_FD_CR_IX] = fd_close_range;
  1382. for (i = 0; i < CS_ARGV_NO_OF_DUP2_OPS; i++)
  1383. cs_argv[CS_ARGV_DUP2_OP_IX(i)] = &dup2_op[i][0];
  1384. if (opts->spawn_type == ERTS_SPAWN_EXECUTABLE) {
  1385. int num = 0;
  1386. int j = 0;
  1387. if (opts->argv != NULL) {
  1388. for(; opts->argv[num] != NULL; ++num)
  1389. ;
  1390. }
  1391. cs_argv = erts_realloc(ERTS_ALC_T_TMP,cs_argv, (CS_ARGV_NO_OF_ARGS + 1 + num + 1) * sizeof(char *));
  1392. cs_argv[CS_ARGV_CMD_IX] = "-";
  1393. cs_argv[CS_ARGV_NO_OF_ARGS] = cmd_line;
  1394. if (opts->argv != NULL) {
  1395. for (;opts->argv[j] != NULL; ++j) {
  1396. if (opts->argv[j] == erts_default_arg0) {
  1397. cs_argv[CS_ARGV_NO_OF_ARGS + 1 + j] = cmd_line;
  1398. } else {
  1399. cs_argv[CS_ARGV_NO_OF_ARGS + 1 + j] = opts->argv[j];
  1400. }
  1401. }
  1402. }
  1403. cs_argv[CS_ARGV_NO_OF_ARGS + 1 + j] = NULL;
  1404. } else {
  1405. cs_argv[CS_ARGV_CMD_IX] = cmd_line; /* Command */
  1406. cs_argv[CS_ARGV_NO_OF_ARGS] = NULL;
  1407. }
  1408. DEBUGF(("Using vfork\n"));
  1409. pid = vfork();
  1410. if (pid == 0) {
  1411. /* The child! */
  1412. /* Observe!
  1413. * OTP-4389: The child setup program (implemented in
  1414. * erl_child_setup.c) will perform the necessary setup of the
  1415. * child before it execs to the user program. This because
  1416. * vfork() only allow an *immediate* execve() or _exit() in the
  1417. * child.
  1418. */
  1419. execve(child_setup_prog, cs_argv, new_environ);
  1420. _exit(1);
  1421. }
  1422. erts_free(ERTS_ALC_T_TMP,cs_argv);
  1423. }
  1424. #endif
  1425. if (unbind)
  1426. erts_smp_rwmtx_runlock(&erts_cpu_bind_rwmtx);
  1427. if (pid == -1) {
  1428. saved_errno = errno;
  1429. CHLD_STAT_UNLOCK;
  1430. erts_smp_rwmtx_runlock(&environ_rwmtx);
  1431. erts_free(ERTS_ALC_T_TMP, (void *) cmd_line);
  1432. unblock_signals();
  1433. close_pipes(ifd, ofd, opts->read_write);
  1434. errno = saved_errno;
  1435. return ERL_DRV_ERROR_ERRNO;
  1436. }
  1437. #else /* QNX */
  1438. if (opts->use_stdio) {
  1439. if (opts->read_write & DO_READ)
  1440. qnx_spawn_options.iov[1] = ifd[1]; /* stdout for process */
  1441. if (opts->read_write & DO_WRITE)
  1442. qnx_spawn_options.iov[0] = ofd[0]; /* stdin for process */
  1443. }
  1444. else {
  1445. if (opts->read_write & DO_READ)
  1446. qnx_spawn_options.iov[4] = ifd[1];
  1447. if (opts->read_write & DO_WRITE)
  1448. qnx_spawn_options.iov[3] = ofd[0];
  1449. }
  1450. /* Close fds on exec */
  1451. for (i = 3; i < max_files; i++)
  1452. fcntl(i, F_SETFD, 1);
  1453. qnx_spawn_options.flags = _SPAWN_SETSID;
  1454. if ((pid = spawnl(P_NOWAIT, "/bin/sh", "/bin/sh", "-c", cmd_line,
  1455. (char *) 0)) < 0) {
  1456. erts_free(ERTS_ALC_T_TMP, (void *) cmd_line);
  1457. reset_qnx_spawn();
  1458. erts_smp_rwmtx_runlock(&environ_rwmtx);
  1459. close_pipes(ifd, ofd, opts->read_write);
  1460. return ERL_DRV_ERROR_GENERAL;
  1461. }
  1462. reset_qnx_spawn();
  1463. #endif /* QNX */
  1464. erts_free(ERTS_ALC_T_TMP, (void *) cmd_line);
  1465. if (new_environ != environ)
  1466. erts_free(ERTS_ALC_T_ENVIRONMENT, (void *) new_environ);
  1467. if (opts->read_write & DO_READ)
  1468. (void) close(ifd[1]);
  1469. if (opts->read_write & DO_WRITE)
  1470. (void) close(ofd[0]);
  1471. if (opts->read_write & DO_READ) {
  1472. SET_NONBLOCKING(ifd[0]);
  1473. init_fd_data(ifd[0], port_num);
  1474. }
  1475. if (opts->read_write & DO_WRITE) {
  1476. SET_NONBLOCKING(ofd[1]);
  1477. init_fd_data(ofd[1], port_num);
  1478. }
  1479. res = set_driver_data(port_num, ifd[0], ofd[1], opts->packet_bytes,
  1480. opts->read_write, opts->exit_status, pid);
  1481. /* Don't unblock SIGCHLD until now, since the call above must
  1482. first complete putting away the info about our new subprocess. */
  1483. unblock_signals();
  1484. #if CHLDWTHR
  1485. ASSERT(children_alive >= 0);
  1486. if (!(children_alive++))
  1487. CHLD_STAT_SIGNAL; /* Wake up child waiter thread if no children
  1488. was alive before we fork()ed ... */
  1489. #endif
  1490. /* Don't unlock chld_stat_mtx until now of the same reason as above */
  1491. CHLD_STAT_UNLOCK;
  1492. erts_smp_rwmtx_runlock(&environ_rwmtx);
  1493. return (ErlDrvData)res;
  1494. #undef CMD_LINE_PREFIX_STR
  1495. #undef CMD_LINE_PREFIX_STR_SZ
  1496. }
  1497. #ifdef QNX
  1498. static reset_qnx_spawn()
  1499. {
  1500. int i;
  1501. /* Reset qnx_spawn_options */
  1502. qnx_spawn_options.flags = 0;
  1503. qnx_spawn_options.iov[0] = 0xff;
  1504. qnx_spawn_options.iov[1] = 0xff;
  1505. qnx_spawn_options.iov[2] = 0xff;
  1506. qnx_spawn_options.iov[3] = 0xff;
  1507. }
  1508. #endif
  1509. #define FD_DEF_HEIGHT 24
  1510. #define FD_DEF_WIDTH 80
  1511. /* Control op */
  1512. #define FD_CTRL_OP_GET_WINSIZE 100
  1513. static int fd_get_window_size(int fd, Uint32 *width, Uint32 *height)
  1514. {
  1515. #ifdef TIOCGWINSZ
  1516. struct winsize ws;
  1517. if (ioctl(fd,TIOCGWINSZ,&ws) == 0) {
  1518. *width = (Uint32) ws.ws_col;
  1519. *height = (Uint32) ws.ws_row;
  1520. return 0;
  1521. }
  1522. #endif
  1523. return -1;
  1524. }
  1525. static int fd_control(ErlDrvData drv_data,
  1526. unsigned int command,
  1527. char *buf, int len,
  1528. char **rbuf, int rlen)
  1529. {
  1530. int fd = (int)(long)drv_data;
  1531. char resbuff[2*sizeof(Uint32)];
  1532. switch (command) {
  1533. case FD_CTRL_OP_GET_WINSIZE:
  1534. {
  1535. Uint32 w,h;
  1536. if (fd_get_window_size(fd,&w,&h))
  1537. return 0;
  1538. memcpy(resbuff,&w,sizeof(Uint32));
  1539. memcpy(resbuff+sizeof(Uint32),&h,sizeof(Uint32));
  1540. }
  1541. break;
  1542. default:
  1543. return 0;
  1544. }
  1545. if (rlen < 2*sizeof(Uint32)) {
  1546. *rbuf = driver_alloc(2*sizeof(Uint32));
  1547. }
  1548. memcpy(*rbuf,resbuff,2*sizeof(Uint32));
  1549. return 2*sizeof(Uint32);
  1550. }
  1551. static ErlDrvData fd_start(ErlDrvPort port_num, char* name,
  1552. SysDriverOpts* opts)
  1553. {
  1554. ErlDrvData res;
  1555. if (((opts->read_write & DO_READ) && opts->ifd >= max_files) ||
  1556. ((opts->read_write & DO_WRITE) && opts->ofd >= max_files))
  1557. return ERL_DRV_ERROR_GENERAL;
  1558. /*
  1559. * Historical:
  1560. *
  1561. * "Note about nonblocking I/O.
  1562. *
  1563. * At least on Solaris, setting the write end of a TTY to nonblocking,
  1564. * will set the input end to nonblocking as well (and vice-versa).
  1565. * If erl is run in a pipeline like this: cat | erl
  1566. * the input end of the TTY will be the standard input of cat.
  1567. * And cat is not prepared to handle nonblocking I/O."
  1568. *
  1569. * Actually, the reason for this is not that the tty itself gets set
  1570. * in non-blocking mode, but that the "input end" (cat's stdin) and
  1571. * the "output end" (erlang's stdout) are typically the "same" file
  1572. * descriptor, dup()'ed from a single fd by one of this process'
  1573. * ancestors.
  1574. *
  1575. * The workaround for this problem used to be a rather bad kludge,
  1576. * interposing an extra process ("internal cat") between erlang's
  1577. * stdout and the original stdout, allowing erlang to set its stdout
  1578. * in non-blocking mode without affecting the stdin of the preceding
  1579. * process in the pipeline - and being a kludge, it caused all kinds
  1580. * of weird problems.
  1581. *
  1582. * So, this is the current logic:
  1583. *
  1584. * The only reason to set non-blocking mode on the output fd at all is
  1585. * if it's something that can cause a write() to block, of course,
  1586. * i.e. primarily if it points to a tty, socket, pipe, or fifo.
  1587. *
  1588. * If we don't set non-blocking mode when we "should" have, and output
  1589. * becomes blocked, the entire runtime system will be suspended - this
  1590. * is normally bad of course, and can happen fairly "easily" - e.g. user
  1591. * hits ^S on tty - but doesn't necessarily happen.
  1592. *
  1593. * If we do set non-blocking mode when we "shouldn't" have, the runtime
  1594. * system will end up seeing EOF on the input fd (due to the preceding
  1595. * process dying), which typically will cause the entire runtime system
  1596. * to terminate immediately (due to whatever erlang process is seeing
  1597. * the EOF taking it as a signal to halt the system). This is *very* bad.
  1598. *
  1599. * I.e. we should take a conservative approach, and only set non-
  1600. * blocking mode when we a) need to, and b) are reasonably certain
  1601. * that it won't be a problem. And as in the example above, the problem
  1602. * occurs when input fd and output fd point to different "things".
  1603. *
  1604. * However, determining that they are not just the same "type" of
  1605. * "thing", but actually the same instance of that type of thing, is
  1606. * unreasonably complex in many/most cases.
  1607. *
  1608. * Also, with pipes, sockets, and fifos it's far from obvious that the
  1609. * user *wants* non-blocking output: If you're running erlang inside
  1610. * some complex pipeline, you're probably not running a real-time system
  1611. * that must never stop, but rather *want* it to suspend if the output
  1612. * channel is "full".
  1613. *
  1614. * So, the bottom line: We will only set the output fd non-blocking if
  1615. * it points to a tty, and either a) the input fd also points to a tty,
  1616. * or b) we can make sure that setting the output fd non-blocking
  1617. * doesn't interfere with someone else's input, via a somewhat milder
  1618. * kludge than the above.
  1619. *
  1620. * Also keep in mind that while this code is almost exclusively run as
  1621. * a result of an erlang open_port({fd,0,1}, ...), that isn't the only
  1622. * case - it can be called with any old pre-existing file descriptors,
  1623. * the relations between which (if they're even two) we can only guess
  1624. * at - still, we try our best...
  1625. */
  1626. if (opts->read_write & DO_READ) {
  1627. init_fd_data(opts->ifd, port_num);
  1628. }
  1629. if (opts->read_write & DO_WRITE) {
  1630. init_fd_data(opts->ofd, port_num);
  1631. /* If we don't have a read end, all bets are off - no non-blocking. */
  1632. if (opts->read_write & DO_READ) {
  1633. if (isatty(opts->ofd)) { /* output fd is a tty:-) */
  1634. if (isatty(opts->ifd)) { /* input fd is also a tty */
  1635. /* To really do this "right", we should also check that
  1636. input and output fd point to the *same* tty - but
  1637. this seems like overkill; ttyname() isn't for free,
  1638. and this is a very common case - and it's hard to
  1639. imagine a scenario where setting non-blocking mode
  1640. here would cause problems - go ahead and do it. */
  1641. SET_NONBLOCKING(opts->ofd);
  1642. } else { /* output fd is a tty, input fd isn't */
  1643. /* This is a "problem case", but also common (see the
  1644. example above) - i.e. it makes sense to try a bit
  1645. harder before giving up on non-blocking mode: Try to
  1646. re-open the tty that the output fd points to, and if
  1647. successful replace the original one with the "new" fd
  1648. obtained this way, and set *that* one in non-blocking
  1649. mode. (Yes, this is a kludge.)
  1650. However, re-opening the tty may fail in a couple of
  1651. (unusual) cases:
  1652. 1) The name of the tty (or an equivalent one, i.e.
  1653. same major/minor number) can't be found, because
  1654. it actually lives somewhere other than /dev (or
  1655. wherever ttyname() looks for it), and isn't
  1656. equivalent to any of those that do live in the
  1657. "standard" place - this should be *very* unusual.
  1658. 2) Permissions on the tty don't allow us to open it -
  1659. it's perfectly possible to have an fd open to an
  1660. object whose permissions wouldn't allow us to open
  1661. it. This is not as unusual as it sounds, one case
  1662. is if the user has su'ed to someone else (not
  1663. root) - we have a read/write fd open to the tty
  1664. (because it has been inherited all the way down
  1665. here), but we have neither read nor write
  1666. permission for the tty.
  1667. In these cases, we finally give up, and don't set the
  1668. output fd in non-blocking mode. */
  1669. char *tty;
  1670. int nfd;
  1671. if ((tty = ttyname(opts->ofd)) != NULL &&
  1672. (nfd = open(tty, O_WRONLY)) != -1) {
  1673. dup2(nfd, opts->ofd);
  1674. c

Large files files are truncated, but you can click here to view the full file