static int read_to_buf(const char *filename, void *buf) { ssize_t ret; ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1); ((char *)buf)[ret > 0 ? ret : 0] = '\0'; return ret; }
static unsigned long get_uptime(void) { #ifdef __linux__ struct sysinfo info; if (sysinfo(&info) < 0) return 0; return info.uptime; #elif 1 unsigned long uptime; char buf[sizeof(uptime)*3 + 2]; /* /proc/uptime is "UPTIME_SEC.NN IDLE_SEC.NN\n" * (where IDLE is cumulative over all CPUs) */ if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0) bb_perror_msg_and_die("can't read '%s'", "/proc/uptime"); buf[sizeof(buf)-1] = '\0'; sscanf(buf, "%lu", &uptime); return uptime; #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) return 0; return ts.tv_sec; #endif }
static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg) { char buf[sizeof(long long)*3]; unsigned long long num; if (open_read_close(path, buf, sizeof(buf)) < 0) bb_perror_msg_and_die(errmsg, path); /* It can be \n terminated, xatoull won't work well */ if (sscanf(buf, "%llu", &num) != 1 || num > max) bb_error_msg_and_die(errmsg, path); return num; }
static NOINLINE pid_t read_pid(const char *filename) { int len; char buf[128]; len = open_read_close(filename, buf, 127); if (len > 0) { buf[len] = '\0'; /* returns ULONG_MAX on error => -1 */ return bb_strtoul(buf, NULL, 10); } return 0; }
static int pid_is_exec(pid_t pid) { ssize_t bytes; char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; sprintf(buf, "/proc/%u/cmdline", (unsigned)pid); bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof); if (bytes > 0) { G.execname_cmpbuf[bytes] = '\0'; return strcmp(execname, G.execname_cmpbuf) == 0; } return 0; }
static NOINLINE bool may_wakeup(const char *rtcname) { ssize_t ret; char buf[128]; /* strip "/dev/" from the rtcname here */ rtcname = skip_dev_pfx(rtcname); snprintf(buf, sizeof(buf), SYS_RTC_PATH, rtcname); ret = open_read_close(buf, buf, sizeof(buf)); if (ret < 0) return false; /* wakeup events could be disabled or not supported */ return strncmp(buf, "enabled\n", 8) == 0; }
static int may_wakeup(const char *rtcname) { ssize_t ret; char buf[128]; /* strip the '/dev/' from the rtcname here */ if (!strncmp(rtcname, "/dev/", 5)) rtcname += 5; snprintf(buf, sizeof(buf), SYS_RTC_PATH, rtcname); ret = open_read_close(buf, buf, sizeof(buf)); if (ret < 0) return 0; /* wakeup events could be disabled or not supported */ return strncmp(buf, "enabled\n", 8) == 0; }
static long get_uptime(void) { #ifdef __linux__ struct sysinfo info; if (sysinfo(&info) < 0) return 0; return info.uptime; #elif 1 char buf[64]; long uptime; if (open_read_close("/proc/uptime", buf, sizeof(buf)) <= 0) bb_perror_msg_and_die("can't read %s", "/proc/uptime"); buf[sizeof(buf)-1] = '\0'; sscanf(buf, "%l", &uptime); return uptime; #else struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) return 0; return ts.tv_sec; #endif }
static int pid_is_exec(pid_t pid) { ssize_t bytes; char buf[sizeof("/proc/%u/cmdline") + sizeof(int)*3]; char *procname, *exelink; int match; procname = buf + sprintf(buf, "/proc/%u/exe", (unsigned)pid) - 3; exelink = xmalloc_readlink(buf); match = (exelink && strcmp(execname, exelink) == 0); free(exelink); if (match) return match; strcpy(procname, "cmdline"); bytes = open_read_close(buf, G.execname_cmpbuf, G.execname_sizeof); if (bytes > 0) { G.execname_cmpbuf[bytes] = '\0'; return strcmp(execname, G.execname_cmpbuf) == 0; } return 0; }
static int pid_is_name(pid_t pid) { /* /proc/PID/stat is "PID (comm_15_bytes_max) ..." */ char buf[32]; /* should be enough */ char *p, *pe; sprintf(buf, "/proc/%u/stat", (unsigned)pid); if (open_read_close(buf, buf, sizeof(buf) - 1) < 0) return 0; buf[sizeof(buf) - 1] = '\0'; /* paranoia */ p = strchr(buf, '('); if (!p) return 0; pe = strrchr(++p, ')'); if (!pe) return 0; *pe = '\0'; /* we require comm to match and to not be truncated */ /* in Linux, if comm is 15 chars, it may be a truncated * name, so we don't allow that to match */ if (strlen(p) >= COMM_LEN - 1) /* COMM_LEN is 16 */ return 0; return strcmp(p, cmdname) == 0; }
static void dev_get_major_minor(char *device_name, int *major, int *minor) { char dev[16]; char *dev_path; char *colon; int sz; dev_path = xasprintf("/sys/block/%s/dev", device_name); sz = open_read_close(dev_path, dev, sizeof(dev) - 1); if (sz < 0) goto ret; dev[sz] = '\0'; colon = strchr(dev, ':'); if (!colon) goto ret; *major = bb_strtou(dev, NULL, 10); *minor = bb_strtou(colon + 1, NULL, 10); ret: free(dev_path); return; }