/* ========================================================================= */ int ALPutLine(char *tag, const char *format, ...) { FILE *f; va_list ap; char fifo_name[128]; if ( ! is_a_fifo(tag) ) { ALOpenPipe(tag); } if ( is_a_fifo(tag) ) { sprintf(fifo_name, "/tmp/%s.fifo", tag); if ( NULL != ( f = fopen(fifo_name, "w") ) ) { va_start(ap, format); vfprintf(f, format, ap); va_end(ap); fclose(f); return(0); } } /* If we are here - then it is an error */ return(1); }
/* ========================================================================= */ int ALOpenPipe(char *tag) { FILE *f; int i; if ( is_a_fifo("console") ) { if ( NULL == ( f = fopen("/tmp/console.fifo", "w") ) ) return(1); fprintf(f, "openpipe %s\n", tag); fclose(f); i = 0; while(!is_a_fifo(tag)) { i++; usleep(100000); /* Wait 1/10th of a second */ if ( i == 10 ) /* For a total of 1 second */ return(1); } return(0); } return(1); }
/* ========================================================================= */ int ALClosePipe(char *tag) { FILE *f; if ( is_a_fifo("console") ) { if ( NULL == ( f = fopen("/tmp/console.fifo", "w") ) ) return(1); fprintf(f, "closepipe %s\n", tag); fclose(f); return(0); } return(1); }
/** * Closes all file descriptors greater or equal to ``first_fd''. */ void close_file_descriptors(const int first_fd) { int fd; g_return_if_fail(first_fd >= 0); if (try_close_from(first_fd)) return; fd = getdtablesize() - 1; while (fd >= first_fd) { #ifdef HAVE_GTKOSXAPPLICATION /* OS X doesn't allow fds being closed not opened by us. During * GUI initialisation a new kqueue fd is created for UI events. This * is visible to us as a fifo which we are not allowed to close. * Set close on exec on all fifo's so we won't leak any of our other * fifo's * -- JA 2011-11-28 */ if (is_a_fifo(fd)) set_close_on_exec(fd); else #endif /* OS X frowns upon random fds being closed --RAM 2011-11-13 */ if (fd_is_opened(fd)) { if (close(fd)) { #if defined(F_MAXFD) fd = fcntl(0, F_MAXFD); continue; #endif /* F_MAXFD */ } } fd--; } }
static inline int emulate_poll_with_select(struct pollfd *fds, unsigned int n, int timeout) { struct timeval tv; unsigned i; fd_set rfds, wfds, efds; int ret, max_fd = -1; FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); for (i = 0; i < n; i++) { int fd = cast_to_fd(fds[i].fd); safety_assert(!is_valid_fd(fd) || is_a_socket(fd) || is_a_fifo(fd)); if (!is_okay_for_select(fd) || i >= FD_SETSIZE) { fds[i].revents = POLLERR; continue; } max_fd = MAX(fd, max_fd); fds[i].revents = 0; if (POLLIN & fds[i].events) { FD_SET(socket_fd(fd), &rfds); } if (POLLOUT & fds[i].events) { FD_SET(socket_fd(fd), &wfds); } FD_SET(socket_fd(fd), &efds); } if (timeout >= 0) { tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000UL; } ret = select(max_fd + 1, &rfds, &wfds, &efds, timeout < 0 ? NULL : &tv); if (ret > 0) { n = MIN(n, FD_SETSIZE); /* POLLERR is already set above */ for (i = 0; i < n; i++) { int fd = cast_to_fd(fds[i].fd); if (!is_okay_for_select(fd)) continue; if (FD_ISSET(fd, &rfds)) { fds[i].revents |= POLLIN; } if (FD_ISSET(fd, &wfds)) { fds[i].revents |= POLLOUT; } if (FD_ISSET(fd, &efds)) { fds[i].revents |= POLLERR; } } } else if (ret < 0) { s_warning("error during select(): %m"); } return ret; }
static inline bool fd_is_opened(const int fd) { return is_open_fd(fd) || is_a_socket(fd) || is_a_fifo(fd); }
/** * Closes all file descriptors greater or equal to ``first_fd'', skipping * preserved ones if ``preserve'' is TRUE. */ static void fd_close_from_internal(const int first_fd, bool preserve) { int fd; g_return_if_fail(first_fd >= 0); if (!preserve && try_close_from(first_fd)) return; fd = getdtablesize() - 1; while (fd >= first_fd) { if (preserve && hset_contains(fd_preserved, int_to_pointer(fd))) goto next; #ifdef HAVE_GTKOSXAPPLICATION /* OS X doesn't allow fds being closed not opened by us. During * GUI initialisation a new kqueue fd is created for UI events. This * is visible to us as a fifo which we are not allowed to close. * Set close on exec on all fifo's so we won't leak any of our other * fifo's * -- JA 2011-11-28 */ if (is_a_fifo(fd)) fd_set_close_on_exec(fd); else #endif /* OS X frowns upon random fds being closed --RAM 2011-11-13 */ if (fd_is_opened(fd)) { if (close(fd)) { #if defined(F_MAXFD) fd = fcntl(0, F_MAXFD); continue; #endif /* F_MAXFD */ } } next: fd--; } /* * When called with a first_fd of 3, and we are on Windows, also make * sure we close all the known sockets we have. This lets the process * safely auto-restart, avoiding multiple listening sockets on the same * port. * --RAM, 2015-04-05 */ if ( is_running_on_mingw() && !preserve && 3 == first_fd && NULL != fd_sockets ) { hset_t *fds = fd_sockets; /* * We're about to exec() another process, and we may be crashing, * hence do not bother using hset_foreach_remove() to ensure minimal * processing. We also reset the fd_sockets pointer to NULL to * make sure s_close() will do nothing when fd_notify_socket_closed() * is called. */ fd_sockets = NULL; /* We don't expect race conditions here */ hset_foreach(fds, fd_socket_close, NULL); /* Don't bother freeing / clearing set, we're about to exec() */ } }