/* * common.c -- MCDE: the parts of McdeSys that are the same on every system * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * POSIX only: the error text, running a system tool, and the fields of * McdeSysInfo that uname(3), sysconf(3), statvfs(3) and gethostname(3) give. * What differs between systems is in the backend (netbsd.c, linux.c), behind * the two functions in private.h. */ #define _POSIX_C_SOURCE 200809L /* symlink(2), for the time zone: NetBSD declares it only for X/Open 500 and * up or _NETBSD_SOURCE (src/include/unistd.h:249); X/Open 700 is POSIX 2008 * with it, on NetBSD and on glibc alike */ #define _XOPEN_SOURCE 700 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "private.h" static char lastError[256] = ""; const char * mcde_sys_error(void) { return lastError; } int mcde_sys_fail(int status, const char *fmt, ...) { va_list ap; va_start(ap, fmt); vsnprintf(lastError, sizeof lastError, fmt, ap); va_end(ap); return status; } /* A program that ignores SIGCHLD (to have no zombies) gets no exit status * from waitpid(2) -- POSIX: the children are reaped at once. While McdeSys * waits for a tool, SIGCHLD is the default; the caller's way comes back. */ static void ChildSignal(struct sigaction *saved, int restore) { struct sigaction dfl; if (restore) { (void) sigaction(SIGCHLD, saved, NULL); return; } memset(&dfl, 0, sizeof dfl); dfl.sa_handler = SIG_DFL; sigemptyset(&dfl.sa_mask); (void) sigaction(SIGCHLD, &dfl, saved); } int mcde_sys_run(char *const argv[]) { struct sigaction saved; pid_t pid; int st; if (access(argv[0], X_OK) != 0) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), argv[0]); ChildSignal(&saved, 0); pid = fork(); if (pid < 0) { ChildSignal(&saved, 1); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_CANNOT_START, "Cannot start %s: %s."), argv[0], strerror(errno)); } if (pid == 0) { execv(argv[0], argv); _exit(127); } while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) { ChildSignal(&saved, 1); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_EXEC_FAIL, "%s: %s."), argv[0], strerror(errno)); } ChildSignal(&saved, 1); if (WIFEXITED(st) && WEXITSTATUS(st) == 0) return MCDE_SYS_OK; return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_TOOL_FAILED, "%s did not succeed (status %d)."), argv[0], WIFEXITED(st) ? WEXITSTATUS(st) : -1); } int mcde_sys_run_input(char *const argv[], const char *input) { struct sigaction saved; size_t len = strlen(input), done = 0; int fd[2], st = -1; ssize_t w; pid_t pid; if (access(argv[0], X_OK) != 0) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), argv[0]); if (pipe(fd) != 0) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_PIPE, "No pipe: %s."), strerror(errno)); ChildSignal(&saved, 0); pid = fork(); if (pid < 0) { ChildSignal(&saved, 1); close(fd[0]); close(fd[1]); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_CANNOT_START, "Cannot start %s: %s."), argv[0], strerror(errno)); } if (pid == 0) { dup2(fd[0], 0); close(fd[0]); close(fd[1]); execv(argv[0], argv); _exit(127); } close(fd[0]); /* a secret goes through the pipe, never on a command line (ps(1)) */ while (done < len) { w = write(fd[1], input + done, len - done); if (w < 0 && errno == EINTR) continue; if (w <= 0) break; done += (size_t) w; } close(fd[1]); while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) break; ChildSignal(&saved, 1); if (st != -1 && WIFEXITED(st) && WEXITSTATUS(st) == 0) return MCDE_SYS_OK; return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_TOOL_FAILED, "%s did not succeed (status %d)."), argv[0], st != -1 && WIFEXITED(st) ? WEXITSTATUS(st) : -1); } const char * mcde_sys_which(const char *const *paths) { for (; *paths != NULL; paths++) if (access(*paths, X_OK) == 0) return *paths; return NULL; } /* SECONDS > 0: the child is ended when it takes longer (a tool that asks * the network -- showmount waited minutes for an rpcbind that was not * there, measured) */ static int Capture(char *const argv[], char *buf, size_t len, int seconds) { struct sigaction saved; int fd[2], st, nul, late = 0; size_t got = 0; ssize_t r; pid_t pid; time_t end = seconds > 0 ? time(NULL) + seconds : 0; buf[0] = '\0'; if (access(argv[0], X_OK) != 0) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), argv[0]); if (pipe(fd) != 0) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_PIPE, "No pipe: %s."), strerror(errno)); ChildSignal(&saved, 0); pid = fork(); if (pid < 0) { ChildSignal(&saved, 1); close(fd[0]); close(fd[1]); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_CANNOT_START, "Cannot start %s: %s."), argv[0], strerror(errno)); } if (pid == 0) { dup2(fd[1], 1); if ((nul = open("/dev/null", O_WRONLY)) >= 0) dup2(nul, 2); close(fd[0]); close(fd[1]); execv(argv[0], argv); _exit(127); } close(fd[1]); /* read to the end, keep what fits: a child left with a full pipe would * never exit, and waitpid would wait for it for ever */ for (;;) { char scratch[512]; if (end != 0) { struct pollfd pfd; time_t left = end - time(NULL); int pr; pfd.fd = fd[0]; pfd.events = POLLIN; pr = left > 0 ? poll(&pfd, 1, (int) left * 1000) : 0; if (pr < 0 && errno == EINTR) continue; if (pr <= 0) { late = 1; kill(pid, SIGKILL); break; } } if (got + 1 < len) r = read(fd[0], buf + got, len - 1 - got); else r = read(fd[0], scratch, sizeof scratch); if (r < 0 && errno == EINTR) continue; if (r <= 0) break; if (got + 1 < len) got += (size_t) r; } buf[got] = '\0'; close(fd[0]); st = -1; while (waitpid(pid, &st, 0) < 0) if (errno != EINTR) break; ChildSignal(&saved, 1); if (late) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_ANSWER, "%s did not answer within %d seconds."), argv[0], seconds); if (st != -1 && WIFEXITED(st) && WEXITSTATUS(st) == 0) return MCDE_SYS_OK; return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_TOOL_FAILED, "%s did not succeed (status %d)."), argv[0], WIFEXITED(st) ? WEXITSTATUS(st) : -1); } /* * Jobs: a tool that may take minutes (a server that is gone keeps sshfs and * mount_afp busy until their own time-out) run without the program waiting * for it. The tool is started by a process of its own, the runner, which * does what mcde_sys_run does and writes the answer to a pipe before it * ends. The program watches the pipe's read end (XtAppAddInput, poll(2)): * readable means the answer is there -- or the runner is gone. The pipe, * and not waitpid(2), carries the answer because a program may have set * SIGCHLD to SIG_IGN (dtfile does), and then waitpid gives no status. The * runner leads a process group of its own: cancelling ends it and the tool. */ struct McdeSysJob { pid_t pid; /* the runner */ int fd; /* the read end of the pipe */ }; typedef struct { int status; char message[256]; } JobAnswer; McdeSysJob * mcde_sys_job_start(char *const argv[], const char *input) { McdeSysJob *job; int fd[2], i; pid_t pid; if ((job = malloc(sizeof *job)) == NULL) { (void) mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_MEMORY, "Out of memory.")); return NULL; } if (pipe(fd) != 0) { (void) mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_PIPE, "No pipe: %s."), strerror(errno)); free(job); return NULL; } /* neither end goes into the tool: only the runner holds the write * end, so the end of file means the runner has ended */ for (i = 0; i < 2; i++) (void) fcntl(fd[i], F_SETFD, FD_CLOEXEC); pid = fork(); if (pid < 0) { (void) mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_CANNOT_START, "Cannot start %s: %s."), argv[0], strerror(errno)); (void) close(fd[0]); (void) close(fd[1]); free(job); return NULL; } if (pid == 0) { JobAnswer a; /* a session of its own: no controlling terminal (a tool that * asks getpass(3) reads its standard input, not the terminal * the program was started from) and a process group of its own * (cancelling ends the runner and the tool) */ (void) setsid(); (void) close(fd[0]); memset(&a, 0, sizeof a); a.status = input != NULL ? mcde_sys_run_input(argv, input) : mcde_sys_run(argv); if (a.status != MCDE_SYS_OK) snprintf(a.message, sizeof a.message, "%s", mcde_sys_error()); /* one write, less than PIPE_BUF: all of it or none */ (void) write(fd[1], &a, sizeof a); _exit(0); } (void) close(fd[1]); job->pid = pid; job->fd = fd[0]; return job; } int mcde_sys_job_fd(const McdeSysJob *job) { return job->fd; } int mcde_sys_job_finish(McdeSysJob *job) { JobAnswer a; ssize_t n; int st; do n = read(job->fd, &a, sizeof a); while (n < 0 && errno == EINTR); (void) close(job->fd); /* the runner is ending; ECHILD: SIGCHLD is ignored, it is gone */ while (waitpid(job->pid, &st, 0) < 0 && errno == EINTR) ; free(job); if (n != (ssize_t) sizeof a) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_CONNECT_ANSWER, "The program that connects ended without an answer.")); if (a.status == MCDE_SYS_OK) return MCDE_SYS_OK; a.message[sizeof a.message - 1] = '\0'; return mcde_sys_fail(a.status, "%s", a.message); } int mcde_sys_job_cancel(McdeSysJob *job, const char *reason) { int st; /* SIGKILL for the group: the runner and the tool. A daemon that the * tool has started (afpfsd) is in a session of its own and stays. */ (void) kill(-job->pid, SIGKILL); (void) kill(job->pid, SIGKILL); /* before its setsid() there is no group */ (void) close(job->fd); while (waitpid(job->pid, &st, 0) < 0 && errno == EINTR) ; free(job); return mcde_sys_fail(MCDE_SYS_EFAIL, "%s", reason != NULL ? reason : mcde_sys_msg(MSG_CANCELLED, "Cancelled.")); } int mcde_sys_capture(char *const argv[], char *buf, size_t len) { return Capture(argv, buf, len, 0); } int mcde_sys_capture_limited(char *const argv[], char *buf, size_t len, int seconds) { return Capture(argv, buf, len, seconds); } /* the first line of FILE that starts with KEY, the rest of it in BUF */ int mcde_sys_file_value(const char *file, const char *key, char *buf, size_t len) { char line[512]; size_t k = strlen(key); FILE *fp = fopen(file, "r"); int found = 0; if (fp == NULL) return 0; while (!found && fgets(line, sizeof line, fp) != NULL) { char *v, *e; if (strncmp(line, key, k) != 0) continue; v = line + k; while (*v == ' ' || *v == '\t' || *v == ':' || *v == '=' || *v == '"') v++; e = v + strlen(v); while (e > v && (e[-1] == '\n' || e[-1] == '"' || e[-1] == ' ' || e[-1] == '\t')) *--e = '\0'; snprintf(buf, len, "%s", v); found = 1; } fclose(fp); return found; } /* An update source the root update may use (tasksd t0078, docs/reference/ * update-verification.txt): a directory, "file:///DIR" or "https://HOST/DIR". * sysupgrade(8) unpacks the sets as root and checks nothing, pkg_add trusts * the packages it is given; with http:// anyone on the path could replace * them. Fail closed: the scheme in lower case exactly as written here (what * sysupgrade's case patterns and ftp(1) see), and only characters that cannot * mean anything to a shell, a URL query or an option -- a release directory * needs no more. Returns 1 if the source may be used. */ int mcde_sys_update_source_ok(const char *url) { const char *p; if (url == NULL) return 0; if (strncmp(url, "https://", 8) == 0) p = url + 8; else if (strncmp(url, "file:///", 8) == 0) p = url + 7; /* keeps the first "/" of the path */ else if (url[0] == '/') p = url; else return 0; if (url[0] == 'h' && !((p[0] >= 'a' && p[0] <= 'z') || (p[0] >= 'A' && p[0] <= 'Z') || (p[0] >= '0' && p[0] <= '9'))) return 0; /* no host after https:// */ for (; *p != '\0'; p++) { if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || strchr("._~-:/@%+,=", *p) != NULL) continue; return 0; } return 1; } int mcde_sys_hostname_get(char *name, size_t len) { if (gethostname(name, len) != 0) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_NO_HOSTNAME, "No host name: %s."), strerror(errno)); name[len - 1] = '\0'; return MCDE_SYS_OK; } int mcde_sys_info(McdeSysInfo *info) { struct utsname u; struct statvfs vfs; long pages = -1, pagesize = -1; memset(info, 0, sizeof *info); if (uname(&u) == 0) { snprintf(info->kernel, sizeof info->kernel, "%s %s", u.sysname, u.release); snprintf(info->system, sizeof info->system, "%s", u.sysname); snprintf(info->version, sizeof info->version, "%s", u.release); snprintf(info->model, sizeof info->model, "%s", u.machine); } (void) mcde_sys_hostname_get(info->hostname, sizeof info->hostname); #ifdef _SC_NPROCESSORS_ONLN info->cpus = (int) sysconf(_SC_NPROCESSORS_ONLN); #endif #ifdef _SC_PHYS_PAGES pages = sysconf(_SC_PHYS_PAGES); #endif pagesize = sysconf(_SC_PAGESIZE); if (pages > 0 && pagesize > 0) info->memory = (unsigned long long) pages * (unsigned long long) pagesize; if (statvfs("/", &vfs) == 0) { info->diskSize = (unsigned long long) vfs.f_blocks * vfs.f_frsize; info->diskFree = (unsigned long long) vfs.f_bavail * vfs.f_frsize; } mcde_sys_info_backend(info); return MCDE_SYS_OK; } /* ---- the time zone: /etc/localtime, a link into the zoneinfo directory * (src/usr.sbin/sysinst/util.c makes it so: unlink, symlink); the zones of * zone1970.tab (tzfile(5), the tz database's own list) */ #define ZONEINFO "/usr/share/zoneinfo" #define LOCALTIME "/etc/localtime" int mcde_sys_timezone_get(char *zone, size_t len) { char target[PATH_MAX]; ssize_t n = readlink(LOCALTIME, target, sizeof target - 1); const char *z; snprintf(zone, len, "UTC"); if (n <= 0) return MCDE_SYS_OK; target[n] = '\0'; if ((z = strstr(target, "zoneinfo/")) != NULL) snprintf(zone, len, "%s", z + strlen("zoneinfo/")); return MCDE_SYS_OK; } static int ZoneCompare(const void *a, const void *b) { return strcmp((const char *) a, (const char *) b); } int mcde_sys_timezones(char (*zones)[MCDE_SYS_ZONE_LEN], size_t max) { FILE *fp = fopen(ZONEINFO "/zone1970.tab", "r"); char line[512]; size_t n = 0; if (fp == NULL && (fp = fopen(ZONEINFO "/zone.tab", "r")) == NULL) return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_NO_ZONELIST, "No list of time zones in %s."), ZONEINFO); /* CODES COORDINATES TZ [ COMMENTS] */ while (n < max && fgets(line, sizeof line, fp) != NULL) { char *f, *last = NULL; if (line[0] == '#' || strtok_r(line, "\t\n", &last) == NULL || strtok_r(NULL, "\t\n", &last) == NULL || (f = strtok_r(NULL, "\t\n", &last)) == NULL || strlen(f) >= MCDE_SYS_ZONE_LEN) continue; snprintf(zones[n++], MCDE_SYS_ZONE_LEN, "%s", f); } fclose(fp); if (n < max) snprintf(zones[n++], MCDE_SYS_ZONE_LEN, "UTC"); qsort(zones, n, sizeof zones[0], ZoneCompare); return (int) n; } int mcde_sys_timezone_set(const char *zone) { char target[PATH_MAX], tmp[64]; struct stat st; FILE *fp; if (zone == NULL || zone[0] == '\0' || zone[0] == '/' || strstr(zone, "..") != NULL || strlen(zone) >= MCDE_SYS_ZONE_LEN) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_BAD_ZONE, "\"%s\" is no time zone."), zone != NULL ? zone : ""); snprintf(target, sizeof target, ZONEINFO "/%s", zone); if (stat(target, &st) != 0 || !S_ISREG(st.st_mode)) return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_ZONE_NOT_FOUND, "There is no time zone \"%s\"."), zone); if (geteuid() != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_ZONE_EPERM, "Only the administrator can change the time zone.")); /* a new link beside the old one, renamed over it: /etc/localtime is * never missing (sysinst unlinks first, at install time) */ snprintf(tmp, sizeof tmp, LOCALTIME ".%ld", (long) getpid()); (void) unlink(tmp); if (symlink(target, tmp) != 0) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_CANNOT_LINK, "Cannot link %s: %s."), tmp, strerror(errno)); if (rename(tmp, LOCALTIME) != 0) { int e = errno; (void) unlink(tmp); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SHR_REPLACE, "Cannot replace %s: %s."), LOCALTIME, strerror(e)); } /* Debian keeps the name in /etc/timezone as well; only where it is */ if (stat("/etc/timezone", &st) == 0 && (fp = fopen("/etc/timezone", "w")) != NULL) { fprintf(fp, "%s\n", zone); fclose(fp); } return MCDE_SYS_OK; } /* ---- a process's command line: the backend gives the arguments, NUL * separated (mcde_sys_process_args); here they are quoted for sh(1) */ int mcde_sys_process_command(pid_t pid, char *buf, size_t len) { static char args[8192]; size_t n = 0, i; int got = mcde_sys_process_args(pid, args, sizeof args); if (got <= 0 || len < 3) return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_NO_CMDLINE, "No command line for process %ld."), (long) pid); for (i = 0; i < (size_t) got && n + 6 < len; ) { const char *a = args + i; size_t al = strlen(a), k; if (n > 0) buf[n++] = ' '; buf[n++] = '\''; for (k = 0; k < al && n + 6 < len; k++) { if (a[k] == '\'') { /* ' -> '\'' */ memcpy(buf + n, "'\\''", 4); n += 4; } else buf[n++] = a[k]; } buf[n++] = '\''; i += al + 1; } buf[n] = '\0'; return MCDE_SYS_OK; } int mcde_sys_process_stopped(pid_t pid) { if (pid <= 0) return -1; return mcde_sys_process_stopped_backend(pid); } int mcde_sys_cpu_times(McdeSysCpu **out, size_t *n) { if (out == NULL || n == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_CPUTIMES, "No place for the CPU times.")); *out = NULL; *n = 0; return mcde_sys_cpu_times_backend(out, n); } int mcde_sys_memory(McdeSysMemory *m) { if (m == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_MEMSIZE, "No place for the memory.")); memset(m, 0, sizeof *m); if (mcde_sys_memory_backend(m) != MCDE_SYS_OK) return MCDE_SYS_EFAIL; /* the three parts add up to the whole, whatever the system rounded */ if (m->free > m->total) m->free = m->total; if (m->cached > m->total - m->free) m->cached = m->total - m->free; m->used = m->total - m->free - m->cached; if (m->swap_used > m->swap_total) m->swap_used = m->swap_total; return MCDE_SYS_OK; } int mcde_sys_net_io(McdeSysNetIo **out, size_t *n) { if (out == NULL || n == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_NETCOUNTERS, "No place for the network counters.")); *out = NULL; *n = 0; return mcde_sys_net_io_backend(out, n); } int mcde_sys_disk_io(McdeSysDiskIo **out, size_t *n) { if (out == NULL || n == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_DISKCOUNTERS, "No place for the disk counters.")); *out = NULL; *n = 0; return mcde_sys_disk_io_backend(out, n); } int mcde_sys_signal(long pid, McdeSysSignal sig) { static const int numbers[] = { SIGTERM, SIGKILL, SIGSTOP, SIGCONT, SIGHUP, SIGINT }; if (pid <= 1 || (unsigned) sig >= sizeof numbers / sizeof numbers[0]) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_BAD_PROCESS, "That is no process to send a signal to.")); if (kill((pid_t) pid, numbers[sig]) == 0) return MCDE_SYS_OK; if (errno == EPERM) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_PROCESS_EPERM, "The process belongs to somebody else.")); if (errno == ESRCH) return mcde_sys_fail(MCDE_SYS_ENOENT, mcde_sys_msg(MSG_PROCESS_GONE, "The process is gone.")); return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_SIGNAL_FAIL, "Cannot signal the process: %s."), strerror(errno)); } int mcde_sys_processes(McdeSysProcess **out, size_t *n) { if (out == NULL || n == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_PROCLIST, "No place for the list of processes.")); *out = NULL; *n = 0; return mcde_sys_processes_backend(out, n); } /* the comment and the lock of a file: the backends know the extended * attribute and the immutable flag of their system */ int mcde_sys_file_comment_get(const char *path, char *buf, size_t len) { if (path == NULL || buf == NULL || len < 2) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_COMMENT_ROOM, "No file and no room for the comment.")); buf[0] = '\0'; return mcde_sys_file_comment_get_backend(path, buf, len); } int mcde_sys_file_comment_set(const char *path, const char *text) { if (path == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_COMMENT_FILE, "No file for the comment.")); return mcde_sys_file_comment_set_backend(path, text != NULL ? text : ""); } int mcde_sys_file_locked_get(const char *path, int *locked) { if (path == NULL || locked == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_LOCK_FILE, "No file for the lock.")); *locked = 0; return mcde_sys_file_locked_get_backend(path, locked); } /* a name the system takes as its own: letters, digits, '-' and '.', not * starting with '-' */ int mcde_sys_hostname_set(const char *name) { if (name == NULL || name[0] == '\0' || name[0] == '-' || strspn(name, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789.-") != strlen(name)) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_BAD_HOSTNAME, "\"%s\" is not a name a computer can take."), name != NULL ? name : ""); return mcde_sys_hostname_set_backend(name); } int mcde_sys_ntp_get(int *on, char *server, size_t len) { if (on == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_TIMESTATE, "No place for the state of the time service.")); *on = 0; if (server != NULL && len > 0) server[0] = '\0'; return mcde_sys_ntp_get_backend(on, server, len); } int mcde_sys_ntp_set(int on, const char *server) { return mcde_sys_ntp_set_backend(on != 0, server != NULL ? server : ""); } /* the clock, as date(1) sets it: settimeofday(2), root only (POSIX) */ int mcde_sys_time_set(time_t when) { struct timeval tv; tv.tv_sec = when; tv.tv_usec = 0; if (settimeofday(&tv, NULL) != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_SETCLOCK_FAIL, "Cannot set the clock: %s."), strerror(errno)); return MCDE_SYS_OK; } int mcde_sys_file_locked_set(const char *path, int locked) { if (path == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_NO_LOCK_FILE, "No file for the lock.")); return mcde_sys_file_locked_set_backend(path, locked); }