123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- #include "toolkit.h"
- #include "core.h"
- #include <stdlib.h>
- #include <unistd.h>
- #include <net/if.h>
- #include <stdint.h>
- #include <time.h>
- #include "dbgutil.h"
- #include <sys/time.h>
- #include <sys/resource.h>
- #define HAVE_IFADDRS_H 1
- # include <ifaddrs.h>
- # include <sys/socket.h>
- # include <net/ethernet.h>
- # include <netpacket/packet.h>
- #undef NANOSEC
- #define NANOSEC ((uint64_t) 1e9)
- extern char** environ;
- static int read_models(unsigned int numcpus, toolkit_cpu_info_t* ci);
- static int read_times(FILE* statfile_fp,
- unsigned int numcpus,
- toolkit_cpu_info_t* ci);
- static void read_speeds(unsigned int numcpus, toolkit_cpu_info_t* ci);
- static uint64_t read_cpufreq(unsigned int cpunum);
- int toolkit_translate_sys_error(int sys_errno)
- {
- return sys_errno <= 0 ? sys_errno : -sys_errno;
- }
- int toolkit_environ(toolkit_env_item_t** envitems, int* count)
- {
- int i, j, cnt;
- toolkit_env_item_t* envitem;
- *envitems = NULL;
- *count = 0;
- for (i = 0; environ[i] != NULL; i++);
- *envitems = ZCALLOC(i, sizeof(**envitems));
- if (envitems == NULL)
- return TOOLKIT_ENOMEM;
- for (j = 0, cnt = 0; j < i; j++) {
- char* buf;
- char* ptr;
- if (environ[j] == NULL)
- break;
- buf = STRDUP(environ[j]);
- if (buf == NULL)
- goto fail;
- ptr = strchr(buf, '=');
- if (ptr == NULL) {
- FREE(buf);
- continue;
- }
- *ptr = '\0';
- envitem = &(*envitems)[cnt];
- envitem->name = buf;
- envitem->value = ptr + 1;
- cnt++;
- }
- *count = cnt;
- return 0;
- fail:
- for (i = 0; i < cnt; i++) {
- envitem = &(*envitems)[cnt];
- FREE(envitem->name);
- }
- FREE(*envitems);
- *envitems = NULL;
- *count = 0;
- return TOOLKIT_ENOMEM;
- }
- int toolkit_getenv(const char* name, char* buffer, size_t* size)
- {
- char* var;
- size_t len;
- if (name == NULL || buffer == NULL || size == NULL || *size == 0)
- return TOOLKIT_EINVAL;
- var = getenv(name);
- if (var == NULL)
- return TOOLKIT_ENOENT;
- len = strlen(var);
- if (len >= *size) {
- *size = len + 1;
- return TOOLKIT_ENOBUFS;
- }
- memcpy(buffer, var, len + 1);
- *size = len;
- return 0;
- }
- int toolkit_setenv(const char* name, const char* value)
- {
- if (name == NULL || value == NULL)
- return TOOLKIT_EINVAL;
- if (setenv(name, value, 1) != 0)
- return TOOLKIT__ERR(errno);
- return 0;
- }
- int toolkit_unsetenv(const char* name) {
- if (name == NULL)
- return TOOLKIT_EINVAL;
- if (unsetenv(name) != 0)
- return TOOLKIT__ERR(errno);
- return 0;
- }
- uint64_t toolkit__hrtime()
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (((uint64_t)ts.tv_sec) * NANOSEC + ts.tv_nsec);
- }
- uint64_t toolkit_hrtime(void)
- {
- return toolkit__hrtime();
- }
- static int toolkit__cpu_num(FILE* statfile_fp, unsigned int* numcpus)
- {
- unsigned int num;
- char buf[1024];
- if (!fgets(buf, sizeof(buf), statfile_fp))
- return TOOLKIT_EIO;
- num = 0;
- while (fgets(buf, sizeof(buf), statfile_fp)) {
- if (strncmp(buf, "cpu", 3))
- break;
- num++;
- }
- if (num == 0)
- return TOOLKIT_EIO;
- *numcpus = num;
- return 0;
- }
- int toolkit_cpu_info(toolkit_cpu_info_t** cpu_infos, int* count)
- {
- unsigned int numcpus;
- toolkit_cpu_info_t* ci;
- int err;
- FILE* statfile_fp;
- *cpu_infos = NULL;
- *count = 0;
- statfile_fp = toolkit__open_file("/proc/stat");
- if (statfile_fp == NULL)
- return TOOLKIT__ERR(errno);
- err = toolkit__cpu_num(statfile_fp, &numcpus);
- if (err < 0)
- goto out;
- err = TOOLKIT_ENOMEM;
- ci = ZCALLOC(numcpus, sizeof(*ci));
- if (ci == NULL)
- goto out;
- err = read_models(numcpus, ci);
- if (err == 0)
- err = read_times(statfile_fp, numcpus, ci);
- if (err) {
- toolkit_free_cpu_info(ci, numcpus);
- goto out;
- }
- /* read_models() on x86 also reads the CPU speed from /proc/cpuinfo.
- * We don't check for errors here. Worst case, the field is left zero.
- */
- if (ci[0].speed == 0)
- read_speeds(numcpus, ci);
- *cpu_infos = ci;
- *count = numcpus;
- err = 0;
- out:
- if (fclose(statfile_fp))
- if (errno != EINTR && errno != EINPROGRESS)
- abort();
- return err;
- }
- static void read_speeds(unsigned int numcpus, toolkit_cpu_info_t* ci)
- {
- unsigned int num;
- for (num = 0; num < numcpus; num++)
- ci[num].speed = read_cpufreq(num) / 1000;
- }
- /* Also reads the CPU frequency on x86. The other architectures only have
- * a BogoMIPS field, which may not be very accurate.
- *
- * Note: Simply returns on error, toolkit_cpu_info() takes care of the cleanup.
- */
- static int read_models(unsigned int numcpus, toolkit_cpu_info_t* ci)
- {
- static const char model_marker[] = "model name\t: ";
- static const char speed_marker[] = "cpu MHz\t\t: ";
- const char* inferred_model;
- unsigned int model_idx;
- unsigned int speed_idx;
- char buf[1024];
- char* model;
- FILE* fp;
- /* Most are unused on non-ARM, non-MIPS and non-x86 architectures. */
- (void)&model_marker;
- (void)&speed_marker;
- (void)&speed_idx;
- (void)&model;
- (void)&buf;
- (void)&fp;
- model_idx = 0;
- speed_idx = 0;
- #if defined(__arm__) || \
- defined(__i386__) || \
- defined(__mips__) || \
- defined(__x86_64__)
- fp = toolkit__open_file("/proc/cpuinfo");
- if (fp == NULL)
- return TOOLKIT__ERR(errno);
- while (fgets(buf, sizeof(buf), fp)) {
- if (model_idx < numcpus) {
- if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
- model = buf + sizeof(model_marker) - 1;
- model = toolkit_strndup(model, strlen(model) - 1); /* Strip newline. */
- if (model == NULL) {
- fclose(fp);
- return TOOLKIT_ENOMEM;
- }
- ci[model_idx++].model = model;
- continue;
- }
- }
- #if defined(__arm__) || defined(__mips__)
- if (model_idx < numcpus) {
- #if defined(__arm__)
- /* Fallback for pre-3.8 kernels. */
- static const char model_marker[] = "Processor\t: ";
- #else /* defined(__mips__) */
- static const char model_marker[] = "cpu model\t\t: ";
- #endif
- if (strncmp(buf, model_marker, sizeof(model_marker) - 1) == 0) {
- model = buf + sizeof(model_marker) - 1;
- model = toolkit_strndup(model, strlen(model) - 1); /* Strip newline. */
- if (model == NULL) {
- fclose(fp);
- return TOOLKIT_ENOMEM;
- }
- ci[model_idx++].model = model;
- continue;
- }
- }
- #else /* !__arm__ && !__mips__ */
- if (speed_idx < numcpus) {
- if (strncmp(buf, speed_marker, sizeof(speed_marker) - 1) == 0) {
- ci[speed_idx++].speed = atoi(buf + sizeof(speed_marker) - 1);
- continue;
- }
- }
- #endif /* __arm__ || __mips__ */
- }
- fclose(fp);
- #endif /* __arm__ || __i386__ || __mips__ || __x86_64__ */
- /* Now we want to make sure that all the models contain *something* because
- * it's not safe to leave them as null. Copy the last entry unless there
- * isn't one, in that case we simply put "unknown" into everything.
- */
- inferred_model = "unknown";
- if (model_idx > 0)
- inferred_model = ci[model_idx - 1].model;
- while (model_idx < numcpus) {
- model = toolkit_strndup(inferred_model, strlen(inferred_model));
- if (model == NULL)
- return TOOLKIT_ENOMEM;
- ci[model_idx++].model = model;
- }
- return 0;
- }
- static int read_times(FILE* statfile_fp,
- unsigned int numcpus,
- toolkit_cpu_info_t* ci)
- {
- struct toolkit_cpu_times_s ts;
- uint64_t clock_ticks;
- uint64_t user;
- uint64_t nice;
- uint64_t sys;
- uint64_t idle;
- uint64_t dummy;
- uint64_t irq;
- uint64_t num;
- uint64_t len;
- char buf[1024];
- clock_ticks = sysconf(_SC_CLK_TCK);
- TOOLKIT_ASSERT(clock_ticks != (uint64_t)-1);
- TOOLKIT_ASSERT(clock_ticks != 0);
- rewind(statfile_fp);
- if (!fgets(buf, sizeof(buf), statfile_fp))
- abort();
- num = 0;
- while (fgets(buf, sizeof(buf), statfile_fp)) {
- if (num >= numcpus)
- break;
- if (strncmp(buf, "cpu", 3))
- break;
- /* skip "cpu<num> " marker */
- {
- unsigned int n;
- int r = sscanf(buf, "cpu%u ", &n);
- TOOLKIT_ASSERT(r == 1);
- (void)r; /* silence build warning */
- for (len = sizeof("cpu0"); n /= 10; len++);
- }
- /* Line contains user, nice, system, idle, iowait, irq, softirq, steal,
- * guest, guest_nice but we're only interested in the first four + irq.
- *
- * Don't use %*s to skip fields or %ll to read straight into the uint64_t
- * fields, they're not allowed in C89 mode.
- */
- if (6 != sscanf(buf + len,
- "%" PRIu64 " %" PRIu64 " %" PRIu64
- "%" PRIu64 " %" PRIu64 " %" PRIu64,
- &user,
- &nice,
- &sys,
- &idle,
- &dummy,
- &irq))
- abort();
- ts.user = clock_ticks * user;
- ts.nice = clock_ticks * nice;
- ts.sys = clock_ticks * sys;
- ts.idle = clock_ticks * idle;
- ts.irq = clock_ticks * irq;
- ci[num++].cpu_times = ts;
- }
- TOOLKIT_ASSERT(num == numcpus);
- return 0;
- }
- static uint64_t read_cpufreq(unsigned int cpunum)
- {
- uint64_t val;
- char buf[1024];
- FILE* fp;
- snprintf(buf,
- sizeof(buf),
- "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_cur_freq",
- cpunum);
- fp = toolkit__open_file(buf);
- if (fp == NULL)
- return 0;
- if (fscanf(fp, "%" PRIu64, &val) != 1)
- val = 0;
- fclose(fp);
- return val;
- }
- static int toolkit__ifaddr_exclude(struct ifaddrs* ent, int exclude_type)
- {
- if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
- return 1;
- if (ent->ifa_addr == NULL)
- return 1;
- /*
- * On Linux getifaddrs returns information related to the raw underlying
- * devices. We're not interested in this information yet.
- */
- if (ent->ifa_addr->sa_family == PF_PACKET)
- return exclude_type;
- return !exclude_type;
- }
- int toolkit_interface_addresses(toolkit_interface_address_t** addresses, int* count)
- {
- #ifndef HAVE_IFADDRS_H
- * count = 0;
- *addresses = NULL;
- return TOOLKIT_ENOSYS;
- #else
- struct ifaddrs* addrs, * ent;
- toolkit_interface_address_t* address;
- int i;
- struct sockaddr_ll* sll;
- *count = 0;
- *addresses = NULL;
- if (getifaddrs(&addrs))
- return TOOLKIT__ERR(errno);
- /* Count the number of interfaces */
- for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
- if (toolkit__ifaddr_exclude(ent, TOOLKIT__EXCLUDE_IFADDR))
- continue;
- (*count)++;
- }
- if (*count == 0) {
- freeifaddrs(addrs);
- return 0;
- }
- /* Make sure the memory is initiallized to zero using calloc() */
- *addresses = toolkit_calloc(*count, sizeof(**addresses));
- if (!(*addresses)) {
- freeifaddrs(addrs);
- return TOOLKIT_ENOMEM;
- }
- address = *addresses;
- for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
- if (toolkit__ifaddr_exclude(ent, TOOLKIT__EXCLUDE_IFADDR))
- continue;
- address->name = toolkit_strdup(ent->ifa_name);
- if (ent->ifa_addr->sa_family == AF_INET6) {
- address->address.address6 = *((struct sockaddr_in6*)ent->ifa_addr);
- } else {
- address->address.address4 = *((struct sockaddr_in*)ent->ifa_addr);
- }
- if (ent->ifa_netmask->sa_family == AF_INET6) {
- address->netmask.netmask6 = *((struct sockaddr_in6*)ent->ifa_netmask);
- } else {
- address->netmask.netmask4 = *((struct sockaddr_in*)ent->ifa_netmask);
- }
- address->is_internal = !!(ent->ifa_flags & IFF_LOOPBACK);
- address++;
- }
- /* Fill in physical addresses for each interface */
- for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
- if (toolkit__ifaddr_exclude(ent, TOOLKIT__EXCLUDE_IFPHYS))
- continue;
- address = *addresses;
- for (i = 0; i < (*count); i++) {
- size_t namelen = strlen(ent->ifa_name);
- /* Alias interface share the same physical address */
- if (strncmp(address->name, ent->ifa_name, namelen) == 0 &&
- (address->name[namelen] == 0 || address->name[namelen] == ':')) {
- sll = (struct sockaddr_ll*)ent->ifa_addr;
- memcpy(address->phys_addr, sll->sll_addr, sizeof(address->phys_addr));
- }
- address++;
- }
- }
- freeifaddrs(addrs);
- return 0;
- #endif
- }
- void toolkit_free_interface_addresses(toolkit_interface_address_t* addresses,
- int count)
- {
- int i;
- for (i = 0; i < count; i++) {
- FREE(addresses[i].name);
- }
- FREE(addresses);
- }
- void toolkit_sleep(int msec)
- {
- int sec;
- int usec;
- sec = msec / 1000;
- usec = (msec % 1000) * 1000;
- if (sec > 0)
- sleep(sec);
- if (usec > 0)
- usleep(usec);
- }
- int toolkit_os_getpriority(toolkit_pid_t pid, int* priority)
- {
- int r;
- if (priority == NULL)
- return TOOLKIT_EINVAL;
- errno = 0;
- r = getpriority(PRIO_PROCESS, (int)pid);
- if (r == -1 && errno != 0)
- return TOOLKIT__ERR(errno);
- *priority = r;
- return 0;
- }
- int toolkit_os_setpriority(toolkit_pid_t pid, int priority)
- {
- if (priority < TOOLKIT_PRIORITY_HIGHEST || priority > TOOLKIT_PRIORITY_LOW)
- return TOOLKIT_EINVAL;
- if (setpriority(PRIO_PROCESS, (int)pid, priority) != 0)
- return TOOLKIT__ERR(errno);
- return 0;
- }
|