]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
chcpu: convert to use lib/path.c
authorHeiko Carstens <heiko.carstens@de.ibm.com>
Thu, 15 Sep 2011 06:52:30 +0000 (08:52 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 27 Sep 2011 11:36:54 +0000 (13:36 +0200)
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>
include/path.h
lib/path.c
sys-utils/Makefile.am
sys-utils/chcpu.c

index b1ee7248acf0cb17b0820a6f83e4c19fd6ef796e..8e79a856044ccf43a5b2cea3212d29922fed3e49 100644 (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, ...)
index 219eed67eecf2fd2752dcb7954ed2a4bdd144a6b..e2bb398026892105b846a51d97a5278703226404 100644 (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, ...)
 {
index 3b65f03571cc6e6f82284836d6a43494723f2178..c05a0e7e3a5241acdf6b9578af74c02a333ff014 100644 (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
 
index a5d12c71fd06a40f17a1163b3cc98e12128da359..d4a25108c03b5c4f73967b9cb7032f70fd26d181 100644 (file)
 #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;
 }