/** * g_unix_open_pipe: * @fds: Array of two integers * @flags: Bitfield of file descriptor flags, as for fcntl() * @error: a #GError * * Similar to the UNIX pipe() call, but on modern systems like Linux * uses the pipe2() system call, which atomically creates a pipe with * the configured flags. The only supported flag currently is * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that * must still be done separately with fcntl(). * * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if * for fcntl(); these are different on Linux/glibc. * * Returns: %TRUE on success, %FALSE if not (and errno will be set). * * Since: 2.30 */ gboolean g_unix_open_pipe (int *fds, int flags, GError **error) { int ecode; /* We only support FD_CLOEXEC */ g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE); #ifdef HAVE_PIPE2 { int pipe2_flags = 0; if (flags & FD_CLOEXEC) pipe2_flags |= O_CLOEXEC; /* Atomic */ ecode = pipe2 (fds, pipe2_flags); if (ecode == -1 && errno != ENOSYS) return g_unix_set_error_from_errno (error, errno); else if (ecode == 0) return TRUE; /* Fall through on -ENOSYS, we must be running on an old kernel */ } #endif ecode = pipe (fds); if (ecode == -1) return g_unix_set_error_from_errno (error, errno); if (flags == 0) return TRUE; ecode = fcntl (fds[0], F_SETFD, flags); if (ecode == -1) { int saved_errno = errno; close (fds[0]); close (fds[1]); return g_unix_set_error_from_errno (error, saved_errno); } ecode = fcntl (fds[1], F_SETFD, flags); if (ecode == -1) { int saved_errno = errno; close (fds[0]); close (fds[1]); return g_unix_set_error_from_errno (error, saved_errno); } return TRUE; }
pid_t coprocess_common(const char *path, const char *command, char *const argv[], char *const envp[], int pipefd[2]) { if (command) { int pfd_in[2]; int pfd_out[2]; pid_t pid; if (pipe2(pfd_in, O_CLOEXEC) == -1 || pipe2(pfd_out, O_CLOEXEC) == -1) return -1; switch (pid=fork()) { case -1: return -1; case 0: if (__builtin_expect(execs_fork_security && execs_fork_security(execs_fork_security_arg) != 0, 0)) _exit(127); if (dup2(pfd_in[0],0) == -1 || dup2(pfd_out[1],1) == -1) _exit(127); close(pfd_in[0]); close(pfd_in[1]); close(pfd_out[0]); close(pfd_out[1]); if (argv) { if (path) execve(path, argv, envp); else execvpe(command, argv, envp); } else execse(path, command, envp); _exit(127); default: pipefd[0]=pfd_out[0]; pipefd[1]=pfd_in[1]; close(pfd_in[0]); close(pfd_out[1]); return pid; } } else return 1; }
int32_t SystemNative_Pipe(int32_t pipeFds[2], int32_t flags) { switch (flags) { case 0: break; case PAL_O_CLOEXEC: #if HAVE_O_CLOEXEC flags = O_CLOEXEC; #endif break; default: assert_msg(false, "Unknown pipe flag", (int)flags); errno = EINVAL; return -1; } int32_t result; #if HAVE_PIPE2 // If pipe2 is available, use it. This will handle O_CLOEXEC if it was set. while ((result = pipe2(pipeFds, flags)) < 0 && errno == EINTR); #else // Otherwise, use pipe. while ((result = pipe(pipeFds)) < 0 && errno == EINTR); // Then, if O_CLOEXEC was specified, use fcntl to configure the file descriptors appropriately. #if HAVE_O_CLOEXEC if ((flags & O_CLOEXEC) != 0 && result == 0) #else if ((flags & PAL_O_CLOEXEC) != 0 && result == 0) #endif { while ((result = fcntl(pipeFds[0], F_SETFD, FD_CLOEXEC)) < 0 && errno == EINTR); if (result == 0) { while ((result = fcntl(pipeFds[1], F_SETFD, FD_CLOEXEC)) < 0 && errno == EINTR); } if (result != 0) { int tmpErrno = errno; close(pipeFds[0]); close(pipeFds[1]); errno = tmpErrno; } } #endif return result; }
int forkpty(int *pm, char *name, const struct termios *tio, const struct winsize *ws) { int m, s, ec=0, p[2], cs; pid_t pid=-1; sigset_t set, oldset; if (openpty(&m, &s, name, tio, ws) < 0) return -1; sigfillset(&set); pthread_sigmask(SIG_BLOCK, &set, &oldset); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); if (pipe2(p, O_CLOEXEC)) { close(s); goto out; } pid = fork(); if (!pid) { close(m); close(p[0]); if (login_tty(s)) { write(p[1], &errno, sizeof errno); _exit(127); } close(p[1]); pthread_setcancelstate(cs, 0); pthread_sigmask(SIG_SETMASK, &oldset, 0); return 0; } close(s); close(p[1]); if (read(p[0], &ec, sizeof ec) > 0) { int status; waitpid(pid, &status, 0); pid = -1; errno = ec; } close(p[0]); out: if (pid > 0) *pm = m; else close(m); pthread_setcancelstate(cs, 0); pthread_sigmask(SIG_SETMASK, &oldset, 0); return pid; }
int _ribified_pipe2(int pipefd[2], int flags) { if (0 > pipe2(pipefd, flags | O_NONBLOCK)) return -1; if (0 == ribs_epoll_add(pipefd[0], EPOLLIN | EPOLLET, event_loop_ctx) && 0 == ribs_epoll_add(pipefd[1], EPOLLOUT | EPOLLET, event_loop_ctx)) return 0; int my_error = errno; close(pipefd[0]); close(pipefd[1]); errno = my_error; return -1; }
void MakePipe(int out_pipefds[2]) { #if THRILL_HAVE_PIPE2 if (pipe2(out_pipefds, O_CLOEXEC) != 0) throw ErrnoException("Error creating pipe"); #elif defined(_MSC_VER) if (_pipe(out_pipefds, 256, O_BINARY) != 0) throw ErrnoException("Error creating pipe"); #else if (pipe(out_pipefds) != 0) throw ErrnoException("Error creating pipe"); PortSetCloseOnExec(out_pipefds[0]); PortSetCloseOnExec(out_pipefds[1]); #endif }
static void manage_server_event (struct item_s *it, uint32_t evt) { struct sockaddr_storage ss; int cli, rc, opt; ASSERT (evt & EPOLLIN); (void) evt; socklen_t sslen = sizeof (ss); cli = accept4 (it->fd, SA (&ss), &sslen, O_NONBLOCK | O_CLOEXEC); if (cli >= 0) { opt = PIPE_SIZE / 2; setsockopt (cli, SOL_SOCKET, SO_RCVBUF, &opt, sizeof (opt)); opt = PIPE_SIZE; setsockopt (cli, SOL_SOCKET, SO_SNDBUF, &opt, sizeof (opt)); sock_set_chatty (cli, 1); struct item_s *c = malloc (sizeof (struct item_s)); c->loaded = 0; c->pfd[0] = c->pfd[1] = -1; if (0 > (rc = pipe2 (c->pfd, O_NONBLOCK | O_CLOEXEC))) { (void) close (cli); free (c); return; } c->fd = cli; c->events = EPOLLIN; c->type = CLIENT; c->shut = 0; fcntl (c->pfd[1], F_SETPIPE_SZ, PIPE_SIZE); struct epoll_event evt; retry_add: evt.data.ptr = c; evt.events = EPOLLIN; rc = epoll_ctl (fd_epoll, EPOLL_CTL_ADD, cli, &evt); if (rc < 0) { if (rc == EINTR) goto retry_add; abort (); } } }
int init_pipe(/*out*/pipe_t* pipe) { int err; static_assert(1 + &pipe->read == &pipe->write, "Arrayzugriff möglich"); if (/*Fehler?*/ pipe2(&pipe->read, O_NONBLOCK|O_CLOEXEC)) { err = errno; goto ONERR; } return 0; ONERR: TRACEEXIT_ERRLOG(err); return err; }
/** * Creates a pipe (see "man pipe" for further reference). */ int vlc_pipe (int fds[2]) { #ifdef HAVE_PIPE2 if (pipe2 (fds, O_CLOEXEC) == 0) return 0; if (errno != ENOSYS) return -1; #endif if (pipe (fds)) return -1; fcntl (fds[0], F_SETFD, FD_CLOEXEC); fcntl (fds[1], F_SETFD, FD_CLOEXEC); return 0; }
int main(void) { int pipe_fds[2]; int i; char* buf = mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); test_assert(buf != MAP_FAILED); test_assert(0 == pipe2(pipe_fds, O_NONBLOCK)); for (i = 0; i < 10000; ++i) { test_assert(-1 == read(pipe_fds[0], buf, SIZE)); test_assert(errno == EAGAIN); } atomic_puts("EXIT-SUCCESS"); return 0; }
int main(void) { (void) close(0); (void) close(1); int fds[2]; if (pipe(fds)) perror_msg_and_fail("pipe"); (void) close(0); (void) close(1); if (pipe2(fds, O_NONBLOCK)) perror_msg_and_skip("pipe2"); return 0; }
int netcv_open_device(adapter *ad) { //fprintf(stderr, "REEL: netcv_open_device (id=%d)\n", ad->id); SN.want_commit = 0; SN.ncv_rec = NULL; /* create DVR pipe for TS data transfer from libmcli to minisatip */ int pipe_fd[2]; if (pipe2 (pipe_fd, O_NONBLOCK)) LOGL (0, "netceiver: creating pipe failed"); ad->dvr = pipe_fd[0]; // read end of pipe SN.pwfd = pipe_fd[1]; // write end of pipe LOGL(0, "netceiver: creating DVR pipe for adapter %d -> dvr: %d", ad->id, ad->dvr); return 0; }
// not thread safe - needs a lock when called from the main // process. int popen_impl(const char* cmd, const char* mode, pid_t* out_pid) { int p[2]; auto const read = *mode == 'r'; if (!read && *mode != 'w') return -1; if (pipe2(p, O_CLOEXEC) < 0) { return -1; } auto pid = fork(); if (pid < 0) { close(p[0]); close(p[1]); return -1; } int child_pipe = read ? 1 : 0; if (pid == 0) { // child mprotect_1g_pages(PROT_READ); // If anything goes wrong, let the OOM killer kill this child process. Process::OOMScoreAdj(1000); // replace stdin or stdout with the appropriate end // of the pipe if (p[child_pipe] == child_pipe) { // pretty unlikely, but if it was we must clear CLOEXEC, // and the only way to do that is to dup it to a new fd, // and then dup2 it back p[child_pipe] = fcntl(child_pipe, F_DUPFD_CLOEXEC, 3); } dup2(p[child_pipe], child_pipe); // no need to close p[child_pipe] because of O_CLOEXEC signal(SIGINT, SIG_DFL); sigset_t eset; sigemptyset(&eset); sigprocmask(SIG_SETMASK, &eset, nullptr); execl("/bin/sh", "sh", "-c", cmd, nullptr); Logger::Warning("Failed to exec: `%s'", cmd); _Exit(1); } // parent // close the pipe we're not using close(p[child_pipe]); *out_pid = pid; return p[1-child_pipe]; }
void install_generic_signal_handle() { int pipes[2]; struct sigaction action; sigset_t empty_mask; sigemptyset(&empty_mask); /* Fill action with handle_signal function */ action.sa_sigaction = &handle_signal; action.sa_flags = SA_SIGINFO|SA_RESTART; action.sa_mask = empty_mask; singularity_message(DEBUG, "Assigning generic sigaction()s\n"); if ( -1 == sigaction(SIGINT, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGINT signal handler: %s\n", strerror(errno)); ABORT(255); } if ( -1 == sigaction(SIGQUIT, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGQUIT signal handler: %s\n", strerror(errno)); ABORT(255); } if ( -1 == sigaction(SIGTERM, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGTERM signal handler: %s\n", strerror(errno)); ABORT(255); } if ( -1 == sigaction(SIGHUP, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGHUP signal handler: %s\n", strerror(errno)); ABORT(255); } if ( -1 == sigaction(SIGUSR1, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGUSR1 signal handler: %s\n", strerror(errno)); ABORT(255); } if ( -1 == sigaction(SIGUSR2, &action, NULL) ) { singularity_message(ERROR, "Failed to install SIGUSR2 signal handler: %s\n", strerror(errno)); ABORT(255); } /* Open pipes for handle_signal() to write to */ singularity_message(DEBUG, "Creating generic signal pipes\n"); if ( -1 == pipe2(pipes, O_CLOEXEC) ) { singularity_message(ERROR, "Failed to create communication pipes: %s\n", strerror(errno)); ABORT(255); } generic_signal_rpipe = pipes[0]; generic_signal_wpipe = pipes[1]; }
int daemonize(void) { if (0 > pipe2(child_is_up_pipe, O_CLOEXEC)) return LOGGER_ERROR("failed to create pipe"), -1; pid_t pid = fork(); if (pid < 0) { LOGGER_PERROR("daemonize, fork"); exit(EXIT_FAILURE); } if (pid > 0) { close(child_is_up_pipe[1]); /* close the write side */ /* wait for child to come up */ uint8_t t; int res; LOGGER_INFO("waiting for the child process [%d] to start...", pid); if (0 >= (res = read(child_is_up_pipe[0], &t, sizeof(t)))) { if (0 > res) LOGGER_PERROR("pipe"); LOGGER_ERROR("child process failed to start"); exit(EXIT_FAILURE); } LOGGER_INFO("child process started successfully"); exit(EXIT_SUCCESS); } close(child_is_up_pipe[0]); /* close the read side */ umask(0); if (0 > setsid()) { LOGGER_PERROR("daemonize, setsid"); exit(EXIT_FAILURE); } int fdnull = open("/dev/null", O_RDWR); dup2(fdnull, STDIN_FILENO); dup2(fdnull, STDOUT_FILENO); dup2(fdnull, STDERR_FILENO); close(fdnull); pid = getpid(); LOGGER_INFO("child process started (pid=%d)", pid); return 0; }
static void startup(void) { guint id; if (pipe2(pfd, O_CLOEXEC) < 0) { SOL_WRN("pipe()"); goto error; } if (!sol_glib_integration()) { SOL_WRN("sol_glib_integration()"); goto error; } fork_run = sol_platform_linux_fork_run(on_fork, on_child_exit, NULL); if (!fork_run) { SOL_WRN("sol_platform_linux_fork_run()"); goto error; } id = g_idle_add(on_idle, NULL); if (id == 0) { SOL_WRN("g_idle_add()"); goto error; } id = g_timeout_add(100, on_timeout, NULL); if (id == 0) { SOL_WRN("g_timeout_add()"); goto error; } id = g_unix_fd_add(pfd[0], G_IO_IN, on_fd, NULL); if (id == 0) { SOL_WRN("g_unix_fd_add()"); goto error; } sol_timeout_add(5000, on_watchdog, NULL); return; error: sol_quit_with_code(EXIT_FAILURE); return; }
/* * Spawns child process, creating pipe to read child command's output if * requested (create_output_pipe != 0). On success, returns read fd of child * output pipe (or 0 if create_output_pipe == 0); returns -1 on error. */ static int fcd_lib_cmd_spawn(pid_t *child, char **cmd, const int *reaper_pipe, int create_output_pipe) { int output_pipe[2]; if (create_output_pipe) { /* CLOEXEC will not be inherited by dup2'ed file descriptor */ if (pipe2(output_pipe, O_CLOEXEC) == -1) { FCD_PERROR("pipe2"); return -1; } } *child = fcd_proc_fork(reaper_pipe); if (*child == -1) { FCD_PERROR("fork"); if (create_output_pipe) { if (close(output_pipe[0]) == -1) FCD_PERROR("close"); if (close(output_pipe[1]) == -1) FCD_PERROR("close"); } return -1; } if (*child == 0) { fcd_lib_cmd_child(create_output_pipe ? output_pipe[1] : -1, cmd); } if (create_output_pipe) { if (close(output_pipe[1]) == -1) { FCD_PERROR("close"); if (close(output_pipe[0]) == -1) { FCD_PERROR("close"); FCD_ABORT("Failed to close child pipe\n"); } fcd_proc_kill(*child, reaper_pipe); return -1; } } return create_output_pipe ? output_pipe[0] : 0; }
bool EnableRDS(uint32_t aMask) { if (!sRadioEnabled || !sRDSSupported) return false; if (sMsmFMMode) setControl(V4L2_CID_PRIVATE_TAVARUA_RDSGROUP_MASK, aMask); if (sRDSEnabled) return true; int pipefd[2]; int rc = pipe2(pipefd, O_NONBLOCK); if (rc < 0) { HAL_LOG("Could not create RDS thread signaling pipes (%d)", rc); return false; } ScopedClose writefd(pipefd[1]); ScopedClose readfd(pipefd[0]); rc = setControl(V4L2_CID_RDS_RECEPTION, true); if (rc < 0) { HAL_LOG("Could not enable RDS reception (%d)", rc); return false; } sRDSPipeFD = writefd; sRDSEnabled = true; rc = pthread_create(&sRDSThread, nullptr, readRDSDataThread, (void*)pipefd[0]); if (rc) { HAL_LOG("Could not start RDS reception thread (%d)", rc); setControl(V4L2_CID_RDS_RECEPTION, false); sRDSEnabled = false; return false; } readfd.forget(); writefd.forget(); return true; }
int main(int argc, char *argv[]) { int fd[2]; pipe2(fd, O_NONBLOCK); int child = fork(); if (child) { close(fd[1]); char buf; for (;;) { wait(NULL); if (read(fd[0], &buf, 1) > 0) break; ptrace(PTRACE_SYSCALL, child, NULL, NULL); } struct user_regs_struct regs; for (;;) { ptrace(PTRACE_SINGLESTEP, child, NULL, NULL); wait(NULL); ptrace(PTRACE_GETREGS, child, NULL, ®s); #if defined(__i386__) #define instruction_pointer regs.eip #define upper_bound 0xb0000000 #elif defined(__x86_64__) #define instruction_pointer regs.rip #define upper_bound 0x700000000000 #else #error "That platform is not supported." #endif if (instruction_pointer < upper_bound) { unsigned long instruction = ptrace(PTRACE_PEEKTEXT, child, instruction_pointer, NULL); if ((instruction & 0xffff) == 0x25ff /* jmp r/m32 */) { printf("0x%lx\n", instruction_pointer); break; } } } } else { ptrace(PTRACE_TRACEME, 0, NULL, NULL); close(fd[0]); dup2(fd[1], 2); execl("/bin/su", "su", "not-a-valid-user", NULL); } return 0; }
/* virPolkitAgentCreate: * * Allocate and setup a polkit agent * * Returns a virCommandPtr on success and NULL on failure */ virPolkitAgentPtr virPolkitAgentCreate(void) { virPolkitAgentPtr agent = NULL; int pipe_fd[2] = {-1, -1}; struct pollfd pollfd; int outfd = STDOUT_FILENO; int errfd = STDERR_FILENO; if (!isatty(STDIN_FILENO)) goto error; if (pipe2(pipe_fd, 0) < 0) goto error; if (VIR_ALLOC(agent) < 0) goto error; agent->cmd = virCommandNewArgList(PKTTYAGENT, "--process", NULL); virCommandAddArgFormat(agent->cmd, "%lld", (long long int) getpid()); virCommandAddArg(agent->cmd, "--notify-fd"); virCommandAddArgFormat(agent->cmd, "%d", pipe_fd[1]); virCommandAddArg(agent->cmd, "--fallback"); virCommandSetInputFD(agent->cmd, STDIN_FILENO); virCommandSetOutputFD(agent->cmd, &outfd); virCommandSetErrorFD(agent->cmd, &errfd); virCommandPassFD(agent->cmd, pipe_fd[1], VIR_COMMAND_PASS_FD_CLOSE_PARENT); if (virCommandRunAsync(agent->cmd, NULL) < 0) goto error; pollfd.fd = pipe_fd[0]; pollfd.events = POLLHUP; if (poll(&pollfd, 1, -1) < 0) goto error; return agent; error: VIR_FORCE_CLOSE(pipe_fd[0]); VIR_FORCE_CLOSE(pipe_fd[1]); virPolkitAgentDestroy(agent); return NULL; }
/* * Creates a pipe with FD_CLOEXEC set on both file descriptors */ int qemu_pipe(int pipefd[2]) { int ret; #ifdef CONFIG_PIPE2 ret = pipe2(pipefd, O_CLOEXEC); if (ret != -1 || errno != ENOSYS) { return ret; } #endif ret = pipe(pipefd); if (ret == 0) { qemu_set_cloexec(pipefd[0]); qemu_set_cloexec(pipefd[1]); } return ret; }
OpenconnectAuthWidget::OpenconnectAuthWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget * parent) : SettingWidget(setting, parent) , d_ptr(new OpenconnectAuthWidgetPrivate) { Q_D(OpenconnectAuthWidget); d->setting = setting; d->ui.setupUi(this); d->userQuit = false; d->formGroupChanged = false; if (pipe2(d->cancelPipes, O_NONBLOCK|O_CLOEXEC)) { // Should never happen. Just don't do real cancellation if it does d->cancelPipes[0] = -1; d->cancelPipes[1] = -1; } connect(d->ui.cmbLogLevel, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &OpenconnectAuthWidget::logLevelChanged); connect(d->ui.viewServerLog, &QCheckBox::toggled, this, &OpenconnectAuthWidget::viewServerLogToggled); connect(d->ui.btnConnect, &QPushButton::clicked, this, &OpenconnectAuthWidget::connectHost); d->ui.cmbLogLevel->setCurrentIndex(OpenconnectAuthWidgetPrivate::Debug); d->ui.btnConnect->setIcon(QIcon::fromTheme("network-connect")); d->ui.viewServerLog->setChecked(false); d->worker = new OpenconnectAuthWorkerThread(&d->mutex, &d->workerWaiting, &d->userQuit, &d->formGroupChanged, d->cancelPipes[0]); // gets the pointer to struct openconnect_info (defined in openconnect.h), which contains data that OpenConnect needs, // and which needs to be populated with settings we get from NM, like host, certificate or private key d->vpninfo = d->worker->getOpenconnectInfo(); connect(d->worker, static_cast<void (OpenconnectAuthWorkerThread::*)(const QString &, const QString &, const QString &, bool*)>(&OpenconnectAuthWorkerThread::validatePeerCert), this, &OpenconnectAuthWidget::validatePeerCert); connect(d->worker, &OpenconnectAuthWorkerThread::processAuthForm, this, &OpenconnectAuthWidget::processAuthForm); connect(d->worker, &OpenconnectAuthWorkerThread::updateLog, this, &OpenconnectAuthWidget::updateLog); connect(d->worker, static_cast<void (OpenconnectAuthWorkerThread::*)(const QString&)>(&OpenconnectAuthWorkerThread::writeNewConfig), this, &OpenconnectAuthWidget::writeNewConfig); connect(d->worker, &OpenconnectAuthWorkerThread::cookieObtained, this, &OpenconnectAuthWidget::workerFinished); readConfig(); readSecrets(); // This might be set by readSecrets() so don't connect it until now connect(d->ui.cmbHosts, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &OpenconnectAuthWidget::connectHost); KAcceleratorManager::manage(this); }
static __inline__ void rtp_session_resume_live(RTP_session *rtp_s) { GEvent *ev; if (rtp_s->playing) return; if (pipe2(rtp_s->fd, O_NONBLOCK) < 0) { rc_log( RC_LOG_ERR, "[RTP] play live stream faild, pipe() < 0, err:%d.", errno ); rtp_s->fd[0] = -1; rtp_s->fd[1] = -1; return; } if (G_UNLIKELY(rtp_s->transport.rtp_writer)) /* BUG */ { rc_log( RC_LOG_ERR, "[RTP] play live stream faild, 'rtp_s->transport.rtp_writer' exists." ); rtp_s->playing = TRUE; return; } ev = g_event_new(sizeof(RTP_writer), rtp_s->fd[0], EV_READ); g_event_set_timeout(ev, 100); g_event_set_callback(ev, rtp_lwrite_cb, rtp_s, on_rtp_writer_destroy); ((RTP_writer*)ev)->rtp_s = rtp_s; rtp_s->transport.rtp_writer = ev; rtp_session_ref(rtp_s); bq_producer_add_fd(rtp_s->track->producer, rtp_s->fd[1], NULL, on_producer_working, NULL); g_scheduler_add(ev, LOOP_WEIGHT_VIDEO); rtp_s->playing = TRUE; }
Pipe::Pipe() { #ifdef _WIN32 HANDLE readPipe; HANDLE writePipe; SECURITY_ATTRIBUTES sec; memset(&sec, 0, sizeof(sec)); sec.nLength = sizeof(sec); sec.bInheritHandle = FALSE; // O_CLOEXEC equivalent constexpr DWORD kPipeSize = 64 * 1024; if (!CreatePipe(&readPipe, &writePipe, &sec, kPipeSize)) { throw std::system_error( GetLastError(), std::system_category(), "CreatePipe failed"); } read = FileDescriptor(intptr_t(readPipe)); write = FileDescriptor(intptr_t(writePipe)); #else int fds[2]; int res; #if HAVE_PIPE2 res = pipe2(fds, O_NONBLOCK | O_CLOEXEC); #else res = pipe(fds); #endif if (res) { throw std::system_error( errno, std::system_category(), std::string("pipe error: ") + strerror(errno)); } read = FileDescriptor(fds[0]); write = FileDescriptor(fds[1]); #if !HAVE_PIPE2 read.setCloExec(); read.setNonBlock(); write.setCloExec(); write.setNonBlock(); #endif #endif }
static void create_thread(lwan_t *l, lwan_thread_t *thread) { pthread_attr_t attr; memset(thread, 0, sizeof(*thread)); thread->lwan = l; if ((thread->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) lwan_status_critical_perror("epoll_create"); if (pthread_attr_init(&attr)) lwan_status_critical_perror("pthread_attr_init"); if (pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)) lwan_status_critical_perror("pthread_attr_setscope"); if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE)) lwan_status_critical_perror("pthread_attr_setdetachstate"); if (pipe2(thread->pipe_fd, O_NONBLOCK | O_CLOEXEC) < 0) lwan_status_critical_perror("pipe"); struct epoll_event event = { .events = EPOLLIN, .data.ptr = NULL }; if (epoll_ctl(thread->epoll_fd, EPOLL_CTL_ADD, thread->pipe_fd[0], &event) < 0) lwan_status_critical_perror("epoll_ctl"); if (pthread_create(&thread->self, &attr, thread_io_loop, thread)) lwan_status_critical_perror("pthread_create"); if (pthread_attr_destroy(&attr)) lwan_status_critical_perror("pthread_attr_destroy"); } void lwan_thread_add_client(lwan_thread_t *t, int fd) { t->lwan->conns[fd].flags = 0; t->lwan->conns[fd].thread = t; if (UNLIKELY(write(t->pipe_fd[1], &fd, sizeof(int)) < 0)) lwan_status_perror("write"); }
int main(void) { (void) close(0); (void) close(1); int fds[2]; if (pipe(fds) || fds[0] != 0 || fds[1] != 1) return 77; #ifdef HAVE_PIPE2 (void) close(0); (void) close(1); if (pipe2(fds, O_NONBLOCK) || fds[0] != 0 || fds[1] != 1) return 77; return 0; #else return 77; #endif }
void xpipe(int* read_end, int* write_end) { struct cleanup* cl[2]; cl[0] = cleanup_allocate(); cl[1] = cleanup_allocate(); int fd[2]; if (pipe2(fd, O_CLOEXEC) < 0) die_errno("pipe2"); assert_cloexec(fd[0]); assert_cloexec(fd[1]); cleanup_commit_close_fd(cl[0], fd[0]); cleanup_commit_close_fd(cl[1], fd[1]); *read_end = fd[0]; *write_end = fd[1]; }
/*********************************************************************** * server_pipe * * Create a pipe for communicating with the server. */ int server_pipe( int fd[2] ) { int ret; #ifdef HAVE_PIPE2 static int have_pipe2 = 1; if (have_pipe2) { if (!(ret = pipe2( fd, O_CLOEXEC ))) return ret; if (errno == ENOSYS || errno == EINVAL) have_pipe2 = 0; /* don't try again */ } #endif if (!(ret = pipe( fd ))) { fcntl( fd[0], F_SETFD, FD_CLOEXEC ); fcntl( fd[1], F_SETFD, FD_CLOEXEC ); } return ret; }
int main(int argc, const char* argv[]){ CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); CppUnit::TextUi::TestRunner runner; tweak_output(output); if ( pipe2(worker.pipe, O_NONBLOCK) != 0){ /* non-block so it is possible to easily test that the pipe is actually empty */ abort(); } runner.addTest( suite ); runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr)); return runner.run() ? 0 : 1; }
int initConexiones() { conexiones = list_create(); pthread_mutex_init(&mConexiones, NULL); FD_ZERO(&nodos); pthread_mutex_init(&mNodos, NULL); escuchaConexiones = socket(AF_INET, SOCK_STREAM, 0); if (escuchaConexiones == -1) { log_error(logFile,"No se pudo crear el socket para escuchar " "nuevas conexiones."); } else { log_info(logFile, "El socket para escuchar nuevas conexiones se " "creo correctamente."); } pipe2(desbloquearSelect, O_NONBLOCK); FD_SET(desbloquearSelect[0], &nodos); int yes=1; if (setsockopt(escuchaConexiones,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { perror("setsockopt"); exit(1); } Sockaddr_in miDirecc; miDirecc.sin_family = AF_INET; miDirecc.sin_port = htons(PUERTO_LISTEN); inet_aton(IP_LISTEN, &(miDirecc.sin_addr)); memset(&(miDirecc.sin_zero), '\0', 8); if (0 > bind(escuchaConexiones, (Sockaddr_in*) &miDirecc, sizeof(Sockaddr_in))) { log_error(logFile, "No se pudo bindear el socket para escuchar conexiones."); } if (listen(escuchaConexiones, NODOS_MAX+1) == -1) { log_error(logFile,"No se pueden escuchar conexiones." "El FileSystem se cerrara"); return(-1); } else { log_info(logFile, "Escuchando conexiones."); } }