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:
parent
8148217b5b
commit
9bc2b4b185
|
@ -4,6 +4,8 @@ extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...
|
||||||
__attribute__ ((__format__ (__printf__, 3, 4)));
|
__attribute__ ((__format__ (__printf__, 3, 4)));
|
||||||
extern void path_getstr(char *result, size_t len, const char *path, ...)
|
extern void path_getstr(char *result, size_t len, const char *path, ...)
|
||||||
__attribute__ ((__format__ (__printf__, 3, 4)));
|
__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, ...)
|
extern int path_getnum(const char *path, ...)
|
||||||
__attribute__ ((__format__ (__printf__, 1, 2)));
|
__attribute__ ((__format__ (__printf__, 1, 2)));
|
||||||
extern int path_exist(const char *path, ...)
|
extern int path_exist(const char *path, ...)
|
||||||
|
|
26
lib/path.c
26
lib/path.c
|
@ -58,6 +58,18 @@ path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap)
|
||||||
return f;
|
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 *
|
FILE *
|
||||||
path_fopen(const char *mode, int exit_on_error, const char *path, ...)
|
path_fopen(const char *mode, int exit_on_error, const char *path, ...)
|
||||||
{
|
{
|
||||||
|
@ -111,6 +123,20 @@ path_getnum(const char *path, ...)
|
||||||
return result;
|
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
|
int
|
||||||
path_exist(const char *path, ...)
|
path_exist(const char *path, ...)
|
||||||
{
|
{
|
||||||
|
|
|
@ -26,7 +26,8 @@ lscpu_SOURCES = lscpu.c $(top_srcdir)/lib/cpuset.c \
|
||||||
$(top_srcdir)/lib/path.c
|
$(top_srcdir)/lib/path.c
|
||||||
dist_man_MANS += lscpu.1
|
dist_man_MANS += lscpu.1
|
||||||
sbin_PROGRAMS += chcpu
|
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
|
dist_man_MANS += chcpu.1
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
|
@ -39,13 +39,12 @@
|
||||||
#include "c.h"
|
#include "c.h"
|
||||||
#include "strutils.h"
|
#include "strutils.h"
|
||||||
#include "bitops.h"
|
#include "bitops.h"
|
||||||
|
#include "path.h"
|
||||||
|
|
||||||
#define _PATH_SYS_CPU "/sys/devices/system/cpu"
|
#define _PATH_SYS_CPU "/sys/devices/system/cpu"
|
||||||
#define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
|
#define _PATH_SYS_CPU_RESCAN _PATH_SYS_CPU "/rescan"
|
||||||
#define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
|
#define _PATH_SYS_CPU_DISPATCH _PATH_SYS_CPU "/dispatching"
|
||||||
|
|
||||||
static char pathbuf[PATH_MAX];
|
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
CMD_CPU_ENABLE = 0,
|
CMD_CPU_ENABLE = 0,
|
||||||
CMD_CPU_DISABLE,
|
CMD_CPU_DISABLE,
|
||||||
|
@ -56,35 +55,10 @@ enum {
|
||||||
CMD_CPU_DISPATCH_VERTICAL,
|
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)
|
static int cpu_enable(cpu_set_t *cpu_set, size_t setsize, int enable)
|
||||||
{
|
{
|
||||||
unsigned int cpu;
|
unsigned int cpu;
|
||||||
int fd, rc;
|
int online, rc;
|
||||||
char c;
|
|
||||||
|
|
||||||
for (cpu = 0; cpu < setsize; cpu++) {
|
for (cpu = 0; cpu < setsize; cpu++) {
|
||||||
if (!CPU_ISSET(cpu, cpu_set))
|
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);
|
printf(_("CPU %d is not hot pluggable\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/online", cpu);
|
online = path_getnum(_PATH_SYS_CPU "/cpu%d/online", cpu);
|
||||||
if (read(fd, &c, 1) == -1)
|
if ((online == 1) && (enable == 1)) {
|
||||||
err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
|
|
||||||
if ((c == '1') && (enable == 1)) {
|
|
||||||
printf(_("CPU %d is already enabled\n"), cpu);
|
printf(_("CPU %d is already enabled\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((c == '0') && (enable == 0)) {
|
if ((online == 0) && (enable == 0)) {
|
||||||
printf(_("CPU %d is already disabled\n"), cpu);
|
printf(_("CPU %d is already disabled\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (enable) {
|
if (enable) {
|
||||||
rc = write(fd, "1", 1);
|
rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/online", cpu);
|
||||||
if (rc == -1)
|
if (rc == -1)
|
||||||
printf(_("CPU %d enable failed (%s)\n"), cpu,
|
printf(_("CPU %d enable failed (%s)\n"), cpu,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
else
|
else
|
||||||
printf(_("CPU %d enabled\n"), cpu);
|
printf(_("CPU %d enabled\n"), cpu);
|
||||||
} else {
|
} else {
|
||||||
rc = write(fd, "0", 1);
|
rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/online", cpu);
|
||||||
if (rc == -1)
|
if (rc == -1)
|
||||||
printf(_("CPU %d disable failed (%s)\n"), cpu,
|
printf(_("CPU %d disable failed (%s)\n"), cpu,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
else
|
else
|
||||||
printf(_("CPU %d disabled\n"), cpu);
|
printf(_("CPU %d disabled\n"), cpu);
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpu_rescan(void)
|
static int cpu_rescan(void)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
|
|
||||||
if (!path_exist(_PATH_SYS_CPU_RESCAN))
|
if (!path_exist(_PATH_SYS_CPU_RESCAN))
|
||||||
errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
|
errx(EXIT_FAILURE, _("This system does not support rescanning of CPUs"));
|
||||||
fd = path_open(O_WRONLY, _PATH_SYS_CPU_RESCAN);
|
if (path_writestr("1", _PATH_SYS_CPU_RESCAN) == -1)
|
||||||
if (write(fd, "1", 1) == -1)
|
|
||||||
err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
|
err(EXIT_FAILURE, _("Failed to trigger rescan of CPUs"));
|
||||||
close(fd);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpu_set_dispatch(int mode)
|
static int cpu_set_dispatch(int mode)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
|
|
||||||
if (!path_exist(_PATH_SYS_CPU_DISPATCH))
|
if (!path_exist(_PATH_SYS_CPU_DISPATCH))
|
||||||
errx(EXIT_FAILURE, _("This system does not support setting "
|
errx(EXIT_FAILURE, _("This system does not support setting "
|
||||||
"the dispatching mode of CPUs"));
|
"the dispatching mode of CPUs"));
|
||||||
fd = path_open(O_WRONLY, _PATH_SYS_CPU_DISPATCH);
|
|
||||||
if (mode == 0) {
|
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"));
|
err(EXIT_FAILURE, _("Failed to set horizontal dispatch mode"));
|
||||||
printf(_("Succesfully set horizontal dispatching mode\n"));
|
printf(_("Succesfully set horizontal dispatching mode\n"));
|
||||||
} else {
|
} 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"));
|
err(EXIT_FAILURE, _("Failed to set vertical dispatch mode"));
|
||||||
printf(_("Succesfully set vertical dispatching mode\n"));
|
printf(_("Succesfully set vertical dispatching mode\n"));
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
|
static int cpu_configure(cpu_set_t *cpu_set, size_t setsize, int configure)
|
||||||
{
|
{
|
||||||
unsigned int cpu;
|
unsigned int cpu;
|
||||||
int fd, rc;
|
int rc, current;
|
||||||
char c;
|
|
||||||
|
|
||||||
for (cpu = 0; cpu < setsize; cpu++) {
|
for (cpu = 0; cpu < setsize; cpu++) {
|
||||||
if (!CPU_ISSET(cpu, cpu_set))
|
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);
|
printf(_("CPU %d is not configurable\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
fd = path_open(O_RDWR, _PATH_SYS_CPU "/cpu%d/configure", cpu);
|
current = path_getnum(_PATH_SYS_CPU "/cpu%d/configure", cpu);
|
||||||
if (read(fd, &c, 1) == -1)
|
if ((current == 1) && (configure == 1)) {
|
||||||
err(EXIT_FAILURE, "error: cannot read from %s", pathbuf);
|
|
||||||
if ((c == '1') && (configure == 1)) {
|
|
||||||
printf(_("CPU %d is already configured\n"), cpu);
|
printf(_("CPU %d is already configured\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((c == '0') && (configure == 0)) {
|
if ((current == 0) && (configure == 0)) {
|
||||||
printf(_("CPU %d is already deconfigured\n"), cpu);
|
printf(_("CPU %d is already deconfigured\n"), cpu);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (configure) {
|
if (configure) {
|
||||||
rc = write(fd, "1", 1);
|
rc = path_writestr("1", _PATH_SYS_CPU "/cpu%d/configure", cpu);
|
||||||
if (rc == -1)
|
if (rc == -1)
|
||||||
printf(_("CPU %d configure failed (%s)\n"), cpu,
|
printf(_("CPU %d configure failed (%s)\n"), cpu,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
else
|
else
|
||||||
printf(_("CPU %d configured\n"), cpu);
|
printf(_("CPU %d configured\n"), cpu);
|
||||||
} else {
|
} else {
|
||||||
rc = write(fd, "0", 1);
|
rc = path_writestr("0", _PATH_SYS_CPU "/cpu%d/configure", cpu);
|
||||||
if (rc == -1)
|
if (rc == -1)
|
||||||
printf(_("CPU %d deconfigure failed (%s)\n"), cpu,
|
printf(_("CPU %d deconfigure failed (%s)\n"), cpu,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
else
|
else
|
||||||
printf(_("CPU %d deconfigured\n"), cpu);
|
printf(_("CPU %d deconfigured\n"), cpu);
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue