static void save(struct userdata *u) { FILE *f; if (!u->modified) return; if (u->sink_filename) { if ((f = pa_fopen_cloexec(u->sink_filename, "w"))) { pa_sink *s = pa_namereg_get_default_sink(u->core); fprintf(f, "%s\n", s ? s->name : ""); fclose(f); } else pa_log("Failed to save default sink: %s", pa_cstrerror(errno)); } if (u->source_filename) { if ((f = pa_fopen_cloexec(u->source_filename, "w"))) { pa_source *s = pa_namereg_get_default_source(u->core); fprintf(f, "%s\n", s ? s->name : ""); fclose(f); } else pa_log("Failed to save default source: %s", pa_cstrerror(errno)); } u->modified = false; }
static int detect_oss(pa_core *c, int just_one) { FILE *f; int n = 0, b = 0; if (!(f = pa_fopen_cloexec("/dev/sndstat", "r")) && !(f = pa_fopen_cloexec("/proc/sndstat", "r")) && !(f = pa_fopen_cloexec("/proc/asound/oss/sndstat", "r"))) { if (errno != ENOENT) pa_log_error("failed to open OSS sndstat device: %s", pa_cstrerror(errno)); return -1; } while (!feof(f)) { char line[256], args[64]; unsigned device; if (!fgets(line, sizeof(line), f)) break; line[strcspn(line, "\r\n")] = 0; if (!b) { b = pa_streq(line, "Audio devices:") || pa_streq(line, "Installed devices:"); continue; } if (line[0] == 0) break; if (sscanf(line, "%u: ", &device) == 1) { if (device == 0) pa_snprintf(args, sizeof(args), "device=/dev/dsp"); else pa_snprintf(args, sizeof(args), "device=/dev/dsp%u", device); if (!pa_module_load(c, "module-oss", args)) continue; } else if (sscanf(line, "pcm%u: ", &device) == 1) { /* FreeBSD support, the devices are named /dev/dsp0.0, dsp0.1 and so on */ pa_snprintf(args, sizeof(args), "device=/dev/dsp%u.0", device); if (!pa_module_load(c, "module-oss", args)) continue; } n++; if (just_one) break; } fclose(f); return n; }
int main () { FILE *f; int r; sqlite3 *db; const char *filename = "./elec_meters.conf"; if (!(f = pa_fopen_cloexec(filename, "r"))) { printf("open meter config failed.\n"); goto error_exit; } state.filename = filename; state.item_table = items; r = build_glb_meter_list(f, &state); if (r != 0) goto error_exit; mb_read_all_meters(glb_meter); print_all_meters(); db = db_glb_init(GLB_DBFILE); if (db == NULL) { printf("openning database failed\n"); goto error_exit; } db_insert_meters(db, glb_meter); db_glb_close(db); error_exit: return -1; }
void pa_memchunk_dump_to_file(pa_memchunk *c, const char *fn) { FILE *f; void *p; pa_assert(c); pa_assert(fn); /* Only for debugging purposes */ f = pa_fopen_cloexec(fn, "a"); if (!f) { pa_log_warn("Failed to open '%s': %s", fn, pa_cstrerror(errno)); return; } p = pa_memblock_acquire(c->memblock); if (fwrite((uint8_t*) p + c->index, 1, c->length, f) != c->length) pa_log_warn("Failed to write to '%s': %s", fn, pa_cstrerror(errno)); pa_memblock_release(c->memblock); fclose(f); }
static void load(struct userdata *u) { FILE *f; /* We never overwrite manually configured settings */ if (u->core->configured_default_sink) pa_log_info("Manually configured default sink, not overwriting."); else if ((f = pa_fopen_cloexec(u->sink_filename, "r"))) { char ln[256] = ""; if (fgets(ln, sizeof(ln)-1, f)) pa_strip_nl(ln); fclose(f); if (!ln[0]) pa_log_info("No previous default sink setting, ignoring."); else if (!pa_namereg_is_valid_name(ln)) pa_log_warn("Invalid sink name: %s", ln); else { pa_log_info("Restoring default sink '%s'.", ln); pa_core_set_configured_default_sink(u->core, ln); } } else if (errno != ENOENT) pa_log("Failed to load default sink: %s", pa_cstrerror(errno)); if (u->core->configured_default_source) pa_log_info("Manually configured default source, not overwriting."); else if ((f = pa_fopen_cloexec(u->source_filename, "r"))) { char ln[256] = ""; if (fgets(ln, sizeof(ln)-1, f)) pa_strip_nl(ln); fclose(f); if (!ln[0]) pa_log_info("No previous default source setting, ignoring."); else if (!pa_namereg_is_valid_name(ln)) pa_log_warn("Invalid source name: %s", ln); else { pa_log_info("Restoring default source '%s'.", ln); pa_core_set_configured_default_source(u->core, ln); } } else if (errno != ENOENT) pa_log("Failed to load default source: %s", pa_cstrerror(errno)); }
static void load(struct userdata *u) { FILE *f; /* We never overwrite manually configured settings */ if (u->core->default_sink) pa_log_info("Manually configured default sink, not overwriting."); else if ((f = pa_fopen_cloexec(u->sink_filename, "r"))) { char ln[256] = ""; pa_sink *s; if (fgets(ln, sizeof(ln)-1, f)) pa_strip_nl(ln); fclose(f); if (!ln[0]) pa_log_info("No previous default sink setting, ignoring."); else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SINK))) { pa_namereg_set_default_sink(u->core, s); pa_log_info("Restored default sink '%s'.", ln); } else pa_log_info("Saved default sink '%s' not existent, not restoring default sink setting.", ln); } else if (errno != ENOENT) pa_log("Failed to load default sink: %s", pa_cstrerror(errno)); if (u->core->default_source) pa_log_info("Manually configured default source, not overwriting."); else if ((f = pa_fopen_cloexec(u->source_filename, "r"))) { char ln[256] = ""; pa_source *s; if (fgets(ln, sizeof(ln)-1, f)) pa_strip_nl(ln); fclose(f); if (!ln[0]) pa_log_info("No previous default source setting, ignoring."); else if ((s = pa_namereg_get(u->core, ln, PA_NAMEREG_SOURCE))) { pa_namereg_set_default_source(u->core, s); pa_log_info("Restored default source '%s'.", ln); } else pa_log_info("Saved default source '%s' not existent, not restoring default source setting.", ln); } else if (errno != ENOENT) pa_log("Failed to load default sink: %s", pa_cstrerror(errno)); }
static int detect_alsa(pa_core *c, int just_one) { FILE *f; int n = 0, n_sink = 0, n_source = 0; if (!(f = pa_fopen_cloexec("/proc/asound/devices", "r"))) { if (errno != ENOENT) pa_log_error("open(\"/proc/asound/devices\") failed: %s", pa_cstrerror(errno)); return -1; } while (!feof(f)) { char line[64], args[64]; unsigned device, subdevice; int is_sink; if (!fgets(line, sizeof(line), f)) break; line[strcspn(line, "\r\n")] = 0; if (pa_endswith(line, "digital audio playback")) is_sink = 1; else if (pa_endswith(line, "digital audio capture")) is_sink = 0; else continue; if (just_one && is_sink && n_sink >= 1) continue; if (just_one && !is_sink && n_source >= 1) continue; if (sscanf(line, " %*i: [%u- %u]: ", &device, &subdevice) != 2) continue; /* Only one sink per device */ if (subdevice != 0) continue; pa_snprintf(args, sizeof(args), "device_id=%u", device); if (!pa_module_load(c, is_sink ? "module-alsa-sink" : "module-alsa-source", args)) continue; n++; if (is_sink) n_sink++; else n_source++; } fclose(f); return n; }
FILE *pa_daemon_conf_open_default_script_file(pa_daemon_conf *c) { FILE *f; pa_assert(c); if (!c->default_script_file) { if (c->system_instance) f = pa_open_config_file(DEFAULT_SYSTEM_SCRIPT_FILE, NULL, ENV_SCRIPT_FILE, &c->default_script_file); else f = pa_open_config_file(DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_USER, ENV_SCRIPT_FILE, &c->default_script_file); } else f = pa_fopen_cloexec(c->default_script_file, "r"); return f; }
static void save(struct userdata *u) { FILE *f; if (!u->modified) return; if (u->sink_filename) { if ((f = pa_fopen_cloexec(u->sink_filename, "w"))) { fprintf(f, "%s\n", u->core->configured_default_sink ? u->core->configured_default_sink : ""); fclose(f); } else pa_log("Failed to save default sink: %s", pa_cstrerror(errno)); } if (u->source_filename) { if ((f = pa_fopen_cloexec(u->source_filename, "w"))) { fprintf(f, "%s\n", u->core->configured_default_source ? u->core->configured_default_source : ""); fclose(f); } else pa_log("Failed to save default source: %s", pa_cstrerror(errno)); } u->modified = false; }
static int proc_name_ours(pid_t pid, const char *procname) { #ifdef __linux__ char bn[PATH_MAX]; FILE *f; pa_snprintf(bn, sizeof(bn), "/proc/%lu/stat", (unsigned long) pid); if (!(f = pa_fopen_cloexec(bn, "r"))) { pa_log_info("Failed to open %s: %s", bn, pa_cstrerror(errno)); return -1; } else { char *expected; pa_bool_t good; char stored[64]; if (!(fgets(stored, sizeof(stored), f))) { int saved_errno = feof(f) ? EINVAL : errno; pa_log_info("Failed to read from %s: %s", bn, feof(f) ? "EOF" : pa_cstrerror(errno)); fclose(f); errno = saved_errno; return -1; } fclose(f); expected = pa_sprintf_malloc("%lu (%s)", (unsigned long) pid, procname); good = pa_startswith(stored, expected); pa_xfree(expected); /*#if !defined(__OPTIMIZE__)*/ if (!good) { /* libtool likes to rename our binary names ... */ expected = pa_sprintf_malloc("%lu (lt-%s)", (unsigned long) pid, procname); good = pa_startswith(stored, expected); pa_xfree(expected); } /*#endif*/ return !!good; } #else return 1; #endif }
int pa_daemon_conf_load(pa_daemon_conf *c, const char *filename) { int r = -1; FILE *f = NULL; struct channel_conf_info ci; pa_config_item table[] = { { "daemonize", pa_config_parse_bool, &c->daemonize, NULL }, { "fail", pa_config_parse_bool, &c->fail, NULL }, { "high-priority", pa_config_parse_bool, &c->high_priority, NULL }, { "realtime-scheduling", pa_config_parse_bool, &c->realtime_scheduling, NULL }, { "disallow-module-loading", pa_config_parse_bool, &c->disallow_module_loading, NULL }, { "allow-module-loading", pa_config_parse_not_bool, &c->disallow_module_loading, NULL }, { "disallow-exit", pa_config_parse_bool, &c->disallow_exit, NULL }, { "allow-exit", pa_config_parse_not_bool, &c->disallow_exit, NULL }, { "use-pid-file", pa_config_parse_bool, &c->use_pid_file, NULL }, { "system-instance", pa_config_parse_bool, &c->system_instance, NULL }, #ifdef HAVE_DBUS { "local-server-type", parse_server_type, c, NULL }, #endif { "no-cpu-limit", pa_config_parse_bool, &c->no_cpu_limit, NULL }, { "cpu-limit", pa_config_parse_not_bool, &c->no_cpu_limit, NULL }, { "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL }, { "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL }, { "enable-memfd", pa_config_parse_not_bool, &c->disable_memfd, NULL }, { "flat-volumes", pa_config_parse_bool, &c->flat_volumes, NULL }, { "lock-memory", pa_config_parse_bool, &c->lock_memory, NULL }, { "enable-deferred-volume", pa_config_parse_bool, &c->deferred_volume, NULL }, { "exit-idle-time", pa_config_parse_int, &c->exit_idle_time, NULL }, { "scache-idle-time", pa_config_parse_int, &c->scache_idle_time, NULL }, { "realtime-priority", parse_rtprio, c, NULL }, { "dl-search-path", pa_config_parse_string, &c->dl_search_path, NULL }, { "default-script-file", pa_config_parse_string, &c->default_script_file, NULL }, { "log-target", parse_log_target, c, NULL }, { "log-level", parse_log_level, c, NULL }, { "verbose", parse_log_level, c, NULL }, { "resample-method", parse_resample_method, c, NULL }, { "default-sample-format", parse_sample_format, c, NULL }, { "default-sample-rate", parse_sample_rate, c, NULL }, { "alternate-sample-rate", parse_alternate_sample_rate, c, NULL }, { "default-sample-channels", parse_sample_channels, &ci, NULL }, { "default-channel-map", parse_channel_map, &ci, NULL }, { "default-fragments", parse_fragments, c, NULL }, { "default-fragment-size-msec", parse_fragment_size_msec, c, NULL }, { "deferred-volume-safety-margin-usec", pa_config_parse_unsigned, &c->deferred_volume_safety_margin_usec, NULL }, { "deferred-volume-extra-delay-usec", pa_config_parse_int, &c->deferred_volume_extra_delay_usec, NULL }, { "nice-level", parse_nice_level, c, NULL }, { "avoid-resampling", pa_config_parse_bool, &c->avoid_resampling, NULL }, { "disable-remixing", pa_config_parse_bool, &c->disable_remixing, NULL }, { "enable-remixing", pa_config_parse_not_bool, &c->disable_remixing, NULL }, { "remixing-use-all-sink-channels", pa_config_parse_bool, &c->remixing_use_all_sink_channels, NULL }, { "disable-lfe-remixing", pa_config_parse_bool, &c->disable_lfe_remixing, NULL }, { "enable-lfe-remixing", pa_config_parse_not_bool, &c->disable_lfe_remixing, NULL }, { "lfe-crossover-freq", pa_config_parse_unsigned, &c->lfe_crossover_freq, NULL }, { "load-default-script-file", pa_config_parse_bool, &c->load_default_script_file, NULL }, { "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL }, { "log-meta", pa_config_parse_bool, &c->log_meta, NULL }, { "log-time", pa_config_parse_bool, &c->log_time, NULL }, { "log-backtrace", pa_config_parse_unsigned, &c->log_backtrace, NULL }, #ifdef HAVE_SYS_RESOURCE_H { "rlimit-fsize", parse_rlimit, &c->rlimit_fsize, NULL }, { "rlimit-data", parse_rlimit, &c->rlimit_data, NULL }, { "rlimit-stack", parse_rlimit, &c->rlimit_stack, NULL }, { "rlimit-core", parse_rlimit, &c->rlimit_core, NULL }, #ifdef RLIMIT_RSS { "rlimit-rss", parse_rlimit, &c->rlimit_rss, NULL }, #endif #ifdef RLIMIT_NOFILE { "rlimit-nofile", parse_rlimit, &c->rlimit_nofile, NULL }, #endif #ifdef RLIMIT_AS { "rlimit-as", parse_rlimit, &c->rlimit_as, NULL }, #endif #ifdef RLIMIT_NPROC { "rlimit-nproc", parse_rlimit, &c->rlimit_nproc, NULL }, #endif #ifdef RLIMIT_MEMLOCK { "rlimit-memlock", parse_rlimit, &c->rlimit_memlock, NULL }, #endif #ifdef RLIMIT_LOCKS { "rlimit-locks", parse_rlimit, &c->rlimit_locks, NULL }, #endif #ifdef RLIMIT_SIGPENDING { "rlimit-sigpending", parse_rlimit, &c->rlimit_sigpending, NULL }, #endif #ifdef RLIMIT_MSGQUEUE { "rlimit-msgqueue", parse_rlimit, &c->rlimit_msgqueue, NULL }, #endif #ifdef RLIMIT_NICE { "rlimit-nice", parse_rlimit, &c->rlimit_nice, NULL }, #endif #ifdef RLIMIT_RTPRIO { "rlimit-rtprio", parse_rlimit, &c->rlimit_rtprio, NULL }, #endif #ifdef RLIMIT_RTTIME { "rlimit-rttime", parse_rlimit, &c->rlimit_rttime, NULL }, #endif #endif { NULL, NULL, NULL, NULL }, }; pa_xfree(c->config_file); c->config_file = NULL; f = filename ? pa_fopen_cloexec(c->config_file = pa_xstrdup(filename), "r") : pa_open_config_file(DEFAULT_CONFIG_FILE, DEFAULT_CONFIG_FILE_USER, ENV_CONFIG_FILE, &c->config_file); if (!f && errno != ENOENT) { pa_log_warn(_("Failed to open configuration file: %s"), pa_cstrerror(errno)); goto finish; } ci.default_channel_map_set = ci.default_sample_spec_set = false; ci.conf = c; r = f ? pa_config_parse(c->config_file, f, table, NULL, true, NULL) : 0; if (r >= 0) { /* Make sure that channel map and sample spec fit together */ if (ci.default_sample_spec_set && ci.default_channel_map_set && c->default_channel_map.channels != c->default_sample_spec.channels) { pa_log_error(_("The specified default channel map has a different number of channels than the specified default number of channels.")); r = -1; goto finish; } else if (ci.default_sample_spec_set) pa_channel_map_init_extend(&c->default_channel_map, c->default_sample_spec.channels, PA_CHANNEL_MAP_DEFAULT); else if (ci.default_channel_map_set) c->default_sample_spec.channels = c->default_channel_map.channels; } finish: if (f) fclose(f); return r; }
static bool is_card_busy(const char *id) { char *card_path = NULL, *pcm_path = NULL, *sub_status = NULL; DIR *card_dir = NULL, *pcm_dir = NULL; FILE *status_file = NULL; size_t len; struct dirent *space = NULL, *de; bool busy = false; int r; pa_assert(id); /* This simply uses /proc/asound/card.../pcm.../sub.../status to * check whether there is still a process using this audio device. */ card_path = pa_sprintf_malloc("/proc/asound/card%s", id); if (!(card_dir = opendir(card_path))) { pa_log_warn("Failed to open %s: %s", card_path, pa_cstrerror(errno)); goto fail; } len = offsetof(struct dirent, d_name) + fpathconf(dirfd(card_dir), _PC_NAME_MAX) + 1; space = pa_xmalloc(len); for (;;) { de = NULL; if ((r = readdir_r(card_dir, space, &de)) != 0) { pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r)); goto fail; } if (!de) break; if (!pa_startswith(de->d_name, "pcm")) continue; if (pcm_is_modem(id, de->d_name + 3)) continue; pa_xfree(pcm_path); pcm_path = pa_sprintf_malloc("%s/%s", card_path, de->d_name); if (pcm_dir) closedir(pcm_dir); if (!(pcm_dir = opendir(pcm_path))) { pa_log_warn("Failed to open %s: %s", pcm_path, pa_cstrerror(errno)); continue; } for (;;) { char line[32]; if ((r = readdir_r(pcm_dir, space, &de)) != 0) { pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r)); goto fail; } if (!de) break; if (!pa_startswith(de->d_name, "sub")) continue; pa_xfree(sub_status); sub_status = pa_sprintf_malloc("%s/%s/status", pcm_path, de->d_name); if (status_file) fclose(status_file); if (!(status_file = pa_fopen_cloexec(sub_status, "r"))) { pa_log_warn("Failed to open %s: %s", sub_status, pa_cstrerror(errno)); continue; } if (!(fgets(line, sizeof(line)-1, status_file))) { pa_log_warn("Failed to read from %s: %s", sub_status, pa_cstrerror(errno)); continue; } if (!pa_streq(line, "closed\n")) { busy = true; break; } } } fail: pa_xfree(card_path); pa_xfree(pcm_path); pa_xfree(sub_status); pa_xfree(space); if (card_dir) closedir(card_dir); if (pcm_dir) closedir(pcm_dir); if (status_file) fclose(status_file); return busy; }