コード例 #1
0
static int ref(void) {

    if (n_ref > 0) {

        pa_assert(pipe_fd[0] >= 0);
        pa_assert(pipe_fd[1] >= 0);
        pa_assert(lock_fd_mutex);

        n_ref++;

        return 0;
    }

    pa_assert(!lock_fd_mutex);
    pa_assert(state == STATE_IDLE);
    pa_assert(lock_fd < 0);
    pa_assert(!thread);
    pa_assert(pipe_fd[0] < 0);
    pa_assert(pipe_fd[1] < 0);

    if (pipe(pipe_fd) < 0)
        return -1;

    pa_make_fd_cloexec(pipe_fd[0]);
    pa_make_fd_cloexec(pipe_fd[1]);

    pa_make_fd_nonblock(pipe_fd[1]);
    pa_make_fd_nonblock(pipe_fd[0]);

    lock_fd_mutex = pa_mutex_new(FALSE, FALSE);

    n_ref = 1;
    return 0;
}
コード例 #2
0
ファイル: database-tdb.c プロジェクト: BYSTROSTREL/pulseaudio
static struct tdb_context *tdb_open_cloexec(
        const char *name,
        int hash_size,
        int tdb_flags,
        int open_flags,
        mode_t mode) {

    /* Mimics pa_open_cloexec() */

    struct tdb_context *c;

#ifdef O_NOCTTY
    open_flags |= O_NOCTTY;
#endif

#ifdef O_CLOEXEC
    errno = 0;
    if ((c = tdb_open(name, hash_size, tdb_flags, open_flags | O_CLOEXEC, mode)))
        goto finish;

    if (errno != EINVAL)
        return NULL;
#endif

    errno = 0;
    if (!(c = tdb_open(name, hash_size, tdb_flags, open_flags, mode)))
        return NULL;

finish:
    pa_make_fd_cloexec(tdb_fd(c));
    return c;
}
コード例 #3
0
ファイル: cpulimit.c プロジェクト: thewb/mokoiax
/* Initializes CPU load limiter */
int pa_cpu_limit_init(pa_mainloop_api *m) {
    struct sigaction sa;

    pa_assert(m);
    pa_assert(!api);
    pa_assert(!io_event);
    pa_assert(the_pipe[0] == -1);
    pa_assert(the_pipe[1] == -1);
    pa_assert(!installed);

    time(&last_time);

    /* Prepare the main loop pipe */
    if (pipe(the_pipe) < 0) {
        pa_log("pipe() failed: %s", pa_cstrerror(errno));
        return -1;
    }

    pa_make_fd_nonblock(the_pipe[0]);
    pa_make_fd_nonblock(the_pipe[1]);
    pa_make_fd_cloexec(the_pipe[0]);
    pa_make_fd_cloexec(the_pipe[1]);

    api = m;
    io_event = api->io_new(m, the_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);

    phase = PHASE_IDLE;

    /* Install signal handler for SIGXCPU */
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    if (sigaction(SIGXCPU, &sa, &sigaction_prev) < 0) {
        pa_cpu_limit_done();
        return -1;
    }

    installed = 1;

    reset_cpu_time(CPUTIME_INTERVAL_SOFT);

    return 0;
}
コード例 #4
0
ファイル: fdsem.c プロジェクト: felfert/pulseaudio
pa_fdsem *pa_fdsem_open_shm(pa_fdsem_data *data, int event_fd) {
    pa_fdsem *f = NULL;

    pa_assert(data);
    pa_assert(event_fd >= 0);

#ifdef HAVE_SYS_EVENTFD_H
    f = pa_xnew(pa_fdsem, 1);

    f->efd = event_fd;
    pa_make_fd_cloexec(f->efd);
    f->fds[0] = f->fds[1] = -1;
    f->data = data;
#endif

    return f;
}
コード例 #5
0
int pa__init(pa_module*m) {
    struct userdata *u;
    struct stat st;
    pa_sample_spec ss;
    pa_channel_map map;
    pa_modargs *ma;
    struct pollfd *pollfd;
    pa_source_new_data data;

    pa_assert(m);

    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
        pa_log("failed to parse module arguments.");
        goto fail;
    }

    ss = m->core->default_sample_spec;
    map = m->core->default_channel_map;
    if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
        pa_log("invalid sample format specification or channel map");
        goto fail;
    }

    m->userdata = u = pa_xnew0(struct userdata, 1);
    u->core = m->core;
    u->module = m;
    pa_memchunk_reset(&u->memchunk);
    u->rtpoll = pa_rtpoll_new();
    pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);

    u->filename = pa_runtime_path(pa_modargs_get_value(ma, "file", DEFAULT_FILE_NAME));

    mkfifo(u->filename, 0666);
    if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
        pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
        goto fail;
    }

    pa_make_fd_cloexec(u->fd);
    pa_make_fd_nonblock(u->fd);

    if (fstat(u->fd, &st) < 0) {
        pa_log("fstat('%s'): %s",u->filename, pa_cstrerror(errno));
        goto fail;
    }

    if (!S_ISFIFO(st.st_mode)) {
        pa_log("'%s' is not a FIFO.", u->filename);
        goto fail;
    }

    pa_source_new_data_init(&data);
    data.driver = __FILE__;
    data.module = m;
    pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
    pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, u->filename);
    pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Unix FIFO source %s", u->filename);
    pa_source_new_data_set_sample_spec(&data, &ss);
    pa_source_new_data_set_channel_map(&data, &map);

    if (pa_modargs_get_proplist(ma, "source_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
        pa_log("Invalid properties");
        pa_source_new_data_done(&data);
        goto fail;
    }

    u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY);
    pa_source_new_data_done(&data);

    if (!u->source) {
        pa_log("Failed to create source.");
        goto fail;
    }

    u->source->parent.process_msg = source_process_msg;
    u->source->userdata = u;

    pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
    pa_source_set_rtpoll(u->source, u->rtpoll);
    pa_source_set_fixed_latency(u->source, pa_bytes_to_usec(PIPE_BUF, &u->source->sample_spec));

    u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
    pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
    pollfd->fd = u->fd;
    pollfd->events = pollfd->revents = 0;

    if (!(u->thread = pa_thread_new(thread_func, u))) {
        pa_log("Failed to create thread.");
        goto fail;
    }

    pa_source_put(u->source);

    pa_modargs_free(ma);

    return 0;

fail:
    if (ma)
        pa_modargs_free(ma);

    pa__done(m);

    return -1;
}