chcpu: convert to use lib/path.c

Use the common path access functions. In order to simplify chcpu also implement
and use path_writestr() which writes a string to the path specified.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
This commit is contained in:
Heiko Carstens 2011-09-15 08:52:30 +02:00 committed by Karel Zak
parent 8148217b5b
commit 9bc2b4b185
4 changed files with 46 additions and 58 deletions

View File

@ -4,6 +4,8 @@ extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...
__attribute__ ((__format__ (__printf__, 3, 4)));
extern void path_getstr(char *result, size_t len, const char *path, ...)
__attribute__ ((__format__ (__printf__, 3, 4)));
extern int path_writestr(const char *str, const char *path, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
extern int path_getnum(const char *path, ...)
__attribute__ ((__format__ (__printf__, 1, 2)));
extern int path_exist(const char *path, ...)

View File

@ -58,6 +58,18 @@ path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
return f;
}
static int
path_vopen(int flags, const char *path, va_list ap)
{
int fd;
const char *p = path_vcreate(path, ap);
fd = open(p, flags);
if (fd == -1)
err(EXIT_FAILURE, _("error: cannot open %s"), p);
return fd;
}
FILE *
path_fopen(const char *mode, int exit_on_error, const char *path, ...)
{
@ -111,6 +123,20 @@ path_getnum(const char *path, ...)
return result;
}
int
path_writestr(const char *str, const char *path, ...)
{
int fd, result;
va_list ap;
va_start(ap, path);
fd = path_vopen(O_WRONLY, path, ap);
va_end(ap);
result = write(fd, str, strlen(str));
close(fd);
return result;
}
int
path_exist(const char *path, ...)
{

View File

@ -26,7 +26,8 @@ lscpu_SOURCES = lscpu.c $(top_srcdir)/lib/cpuset.c \
$(top_srcdir)/lib/path.c
dist_man_MANS += lscpu.1
sbin_PROGRAMS += chcpu
chcpu_SOURCES = chcpu.c $(top_srcdir)/lib/cpuset.c
chcpu_SOURCES = chcpu.c $(top_srcdir)/lib/cpuset.c \
$(top_srcdir)/lib/path.c
dist_man_MANS += chcpu.1
endif

View File

@ -39,13 +39,12 @@
#include "c.h"
#include "strutils.h"
#include "bitops.h"
#include "path.h"
#define _PATH_SYS_CPU "/sys/devices/system/cpu"
#define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
#define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
static char pathbuf[PATH_MAX];
enum {
CMD_CPU_ENABLE = 0,
CMD_CPU_DISABLE,
@ -56,35 +55,10 @@ enum {
CMD_CPU_DISPATCH_VERTICAL,
};
static int path_open(mode_t mode, const char *path, ...)
{
va_list ap;
int fd;
va_start(ap, path);
vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
va_end(ap);
fd = open(pathbuf, mode);
if (fd == -1)
err(EXIT_FAILURE, "error: cannot open %s", pathbuf);
return fd;
}
static int path_exist(const char *path, ...)
{
va_list ap;
va_start(ap, path);
vsnprintf(pathbuf, sizeof(pathbuf), path, ap);
va_end(ap);
return access(pathbuf, F_OK) == 0;
}
static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
{
unsigned int cpu;
int fd, rc;
char c;
int online, rc;
for (cpu = 0; cpu < setsize; cpu++) {
if (!CPU_ISSET(cpu, cpu_set))
@ -97,76 +71,64 @@ static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
printf(_("CPU %d is not hot pluggable\n"), cpu);
continue;
}
fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/online", cpu);
if (read(fd, &c, 1) == -1)
err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
if ((c == '1') && (enable == 1)) {
online = path_getnum(_PATH_SYS_CPU "/cpu%d/online", cpu);
if ((online == 1) && (enable == 1)) {
printf(_("CPU %d is already enabled\n"), cpu);
continue;
}
if ((c == '0') && (enable == 0)) {
if ((online == 0) && (enable == 0)) {
printf(_("CPU %d is already disabled\n"), cpu);
continue;
}
if (enable) {
rc = write(fd, "1", 1);
rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
if (rc == -1)
printf(_("CPU %d enable failed (%s)\n"), cpu,
strerror(errno));
else
printf(_("CPU %d enabled\n"), cpu);
} else {
rc = write(fd, "0", 1);
rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
if (rc == -1)
printf(_("CPU %d disable failed (%s)\n"), cpu,
strerror(errno));
else
printf(_("CPU %d disabled\n"), cpu);
}
close(fd);
}
return EXIT_SUCCESS;
}
static int cpu_rescan(void)
{
int fd;
if (!path_exist(_PATH_SYS_CPU_RESCAN))
errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
fd = path_open(O_WRONLY, _PATH_SYS_CPU_RESCAN);
if (write(fd, "1", 1) == -1)
if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1)
err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
close(fd);
return EXIT_SUCCESS;
}
static int cpu_set_dispatch(int mode)
{
int fd;
if (!path_exist(_PATH_SYS_CPU_DISPATCH))
errx(EXIT_FAILURE, _("This system does not support setting "
"the dispatching mode of CPUs"));
fd = path_open(O_WRONLY, _PATH_SYS_CPU_DISPATCH);
if (mode == 0) {
if (write(fd, "0", 1) == -1)
if (path_writestr("0", _PATH_SYS_CPU_DISPATCH) == -1)
err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode"));
printf(_("Succesfully set horizontal dispatching mode\n"));
} else {
if (write(fd, "1", 1) == -1)
if (path_writestr("1", _PATH_SYS_CPU_DISPATCH) == -1)
err(EXIT_FAILURE, _("Failed to set vertical dispatch mode"));
printf(_("Succesfully set vertical dispatching mode\n"));
}
close(fd);
return EXIT_SUCCESS;
}
static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
{
unsigned int cpu;
int fd, rc;
char c;
int rc, current;
for (cpu = 0; cpu < setsize; cpu++) {
if (!CPU_ISSET(cpu, cpu_set))
@ -179,33 +141,30 @@ static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
printf(_("CPU %d is not configurable\n"), cpu);
continue;
}
fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/configure", cpu);
if (read(fd, &c, 1) == -1)
err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
if ((c == '1') && (configure == 1)) {
current = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
if ((current == 1) && (configure == 1)) {
printf(_("CPU %d is already configured\n"), cpu);
continue;
}
if ((c == '0') && (configure == 0)) {
if ((current == 0) && (configure == 0)) {
printf(_("CPU %d is already deconfigured\n"), cpu);
continue;
}
if (configure) {
rc = write(fd, "1", 1);
rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
if (rc == -1)
printf(_("CPU %d configure failed (%s)\n"), cpu,
strerror(errno));
else
printf(_("CPU %d configured\n"), cpu);
} else {
rc = write(fd, "0", 1);
rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/configure", cpu);
if (rc == -1)
printf(_("CPU %d deconfigure failed (%s)\n"), cpu,
strerror(errno));
else
printf(_("CPU %d deconfigured\n"), cpu);
}
close(fd);
}
return EXIT_SUCCESS;
}