ret_t cherokee_init (void) { ret_t ret; if (_cherokee_init) return ret_ok; #ifdef _WIN32 init_win32(); #endif /* Init the tracing facility */ cherokee_trace_init(); /* Init the bogotime mechanism */ cherokee_bogotime_init(); cherokee_bogotime_update(); /* Init threading stuff */ cherokee_threading_init(); /* Get the CPU number */ dcc_ncpus (&cherokee_cpu_number); if (cherokee_cpu_number < 1) { LOG_WARNING (CHEROKEE_ERROR_INIT_CPU_NUMBER, cherokee_cpu_number); cherokee_cpu_number = 1; } /* Try to figure the L2 cache line size */ cherokee_cacheline_size_get (&cherokee_cacheline_size); /* Get the file descriptor number limit */ ret = cherokee_sys_fdlimit_get (&cherokee_fdlimit); if (ret < ret_ok) { LOG_ERROR_S (CHEROKEE_ERROR_INIT_GET_FD_LIMIT); return ret; } /* Temp directory */ ret = init_tmp_dir(); if (ret != ret_ok) { return ret; } cherokee_admin_child = false; cherokee_readable_errors = NULLB_NULL; _cherokee_init = true; return ret_ok; }
void *dcc_xci_zeroconf_register(void) { int ncpus; const char *service_name = dcc_xci_zeroconf_service_name(); if (dcc_ncpus(&ncpus)) ncpus = 1; return dcc_zeroconf_register_extended(0, service_name, arg_port, ncpus); }
int dcc_get_cpp_lock() { int lock_fd; char *lockdir; int i, ncpus, sleepTime = 10000; if (dcc_get_lock_dir(&lockdir)) return -1; if (dcc_ncpus(&ncpus)) ncpus = 1; ncpus++; for (i=0; i<ncpus; i++) { sprintf(cpp_lock_filename, "%s/%s_%d", lockdir, "cpp_lock", i); if (dcc_open_lockfile(cpp_lock_filename, &lock_fd)) lock_fd = -1; else { if (sys_lock(lock_fd, 0) != 0) { rs_trace("someone already has cpp lock: %s (%s)", cpp_lock_filename, strerror(errno)); close(lock_fd); lock_fd = -1; } else { break; } } } if (lock_fd == -1) { srandom(getpid()); sprintf(cpp_lock_filename, "%s/%s_%d", lockdir, "cpp_lock", random()%(ncpus)); rs_trace("blocking for cpp lock: %s (%s)", cpp_lock_filename, strerror(errno)); if (dcc_open_lockfile(cpp_lock_filename, &lock_fd)) lock_fd = -1; else { if (sys_lock(lock_fd, 1) != 0) { rs_log_warning("failed to get cpp lock: %s (%s)", cpp_lock_filename, strerror(errno)); close(lock_fd); lock_fd = -1; } } } if (lock_fd != -1) rs_trace("got cpp lock: %s", cpp_lock_filename); cpp_lock = lock_fd; return lock_fd; }
/** * Send down the host info. This includes the OS version, hardware info, and compiler versions. */ int dcc_send_host_info(int out_fd) { int ret; char *sysKey = "SYSTEM="; char *compilerKey = "COMPILER="; char *ncpusKey = "CPUS="; char *cpuSpeedKey = "CPUSPEED="; char *maxJobsKey = "JOBS="; char *priorityKey = "PRIORITY="; char *distcc = "DISTCC=" PACKAGE_VERSION "\n"; char *sysInfo = dcc_get_system_version(); char **compilers = dcc_get_all_compiler_versions(); int ncpus; unsigned long long cpuSpeed; if (dcc_ncpus(&ncpus)) ncpus = 0; if (dcc_cpuspeed(&cpuSpeed)) cpuSpeed = 0; int i, len = 0; char *msg; if (sysInfo) { len += strlen(sysInfo) + strlen(sysKey) + 1; } if (distcc) len += strlen(distcc); if (compilers) { for (i=0; compilers && compilers[i] != NULL; i++) { len += strlen(compilers[i]) + strlen(compilerKey) + 1; } } if (ncpus > 0) { len += strlen(ncpusKey) + 8; // 7 digits for cpu count should be plenty } if (cpuSpeed > 0) { len += strlen(cpuSpeedKey) + 32; // 31 digits for cpu speed } if (dcc_max_kids > 0) { len += strlen(maxJobsKey) + 8; // 7 digits for job count should be plenty } if (build_machine_priority > 0) { len += strlen(maxJobsKey) + 32; // 31 digits for priority } msg = malloc(len+1); msg[0] = 0; if (sysInfo) { strcat(msg, sysKey); strcat(msg, sysInfo); strcat(msg, "\n"); } if (distcc) strcat(msg, distcc); if (compilers) { for (i=0; compilers && compilers[i]!=NULL; i++) { strcat(msg, compilerKey); strcat(msg, compilers[i]); strcat(msg, "\n"); } free(compilers); } if (ncpus > 0) { strcat(msg, ncpusKey); sprintf(&msg[strlen(msg)], "%d\n", ncpus); } if (cpuSpeed > 0) { strcat(msg, cpuSpeedKey); sprintf(&msg[strlen(msg)], "%llu\n", cpuSpeed); } if (dcc_max_kids > 0) { strcat(msg, maxJobsKey); sprintf(&msg[strlen(msg)], "%d\n", dcc_max_kids); } if (build_machine_priority > 0) { strcat(msg, priorityKey); sprintf(&msg[strlen(msg)], "%d\n", build_machine_priority); } // a bit of a hack - if we are writing to stdout then just print the string if (out_fd == 1) ret = write(out_fd, msg, len)==len; else ret = dcc_x_token_string(out_fd, "HINF", msg); free(msg); return ret; }
/** * Be a standalone server, with responsibility for sockets and forking * children. Puts the daemon in the background and detaches from the * controlling tty. **/ int dcc_standalone_server(void) { int listen_fd; int n_cpus; int ret; #ifdef HAVE_AVAHI void *avahi = NULL; #endif if ((ret = dcc_socket_listen(arg_port, &listen_fd, opt_listen_addr)) != 0) return ret; dcc_defer_accept(listen_fd); set_cloexec_flag(listen_fd, 1); if (dcc_ncpus(&n_cpus) == 0) rs_log_info("%d CPU%s online on this server", n_cpus, n_cpus == 1 ? "" : "s"); /* By default, allow one job per CPU, plus two for the pot. The extra * ones are started to allow for a bit of extra concurrency so that the * machine is not idle waiting for disk or network IO. */ if (arg_max_jobs) dcc_max_kids = arg_max_jobs; else dcc_max_kids = 2 + n_cpus; rs_log_info("allowing up to %d active jobs", dcc_max_kids); if (!opt_no_detach) { /* Don't go into the background until we're listening and * ready. This is useful for testing -- when the daemon * detaches, we know we can go ahead and try to connect. */ dcc_detach(); } else { /* Still create a new process group, even if not detached */ rs_trace("not detaching"); if ((ret = dcc_new_pgrp()) != 0) return ret; dcc_save_pid(getpid()); } /* Don't catch signals until we've detached or created a process group. */ dcc_daemon_catch_signals(); #ifdef HAVE_AVAHI /* Zeroconf registration */ if (opt_zeroconf) { if (!(avahi = dcc_zeroconf_register((uint16_t) arg_port, n_cpus, dcc_max_kids))) return EXIT_CONNECT_FAILED; } #endif /* This is called in the master daemon, whether that is detached or * not. */ dcc_master_pid = getpid(); if (opt_no_fork) { dcc_log_daemon_started("non-forking daemon"); dcc_nofork_parent(listen_fd); ret = 0; } else { dcc_log_daemon_started("preforking daemon"); ret = dcc_preforking_parent(listen_fd); } #ifdef HAVE_AVAHI /* Remove zeroconf registration */ if (opt_zeroconf) { if (dcc_zeroconf_unregister(avahi) != 0) return EXIT_CONNECT_FAILED; } #endif return ret; }