summaryrefslogtreecommitdiffstats
path: root/schedutils
diff options
context:
space:
mode:
authorKarel Zak2010-09-30 23:27:42 +0200
committerKarel Zak2010-09-30 23:29:14 +0200
commit24295096586c848a5eb3f8d8b73a73f03c174fc1 (patch)
tree1c071246891d5182753589a334e2ca0cd2d5bad4 /schedutils
parentfdisk: eliminate redundant call to open() (diff)
downloadkernel-qcow2-util-linux-24295096586c848a5eb3f8d8b73a73f03c174fc1.tar.gz
kernel-qcow2-util-linux-24295096586c848a5eb3f8d8b73a73f03c174fc1.tar.xz
kernel-qcow2-util-linux-24295096586c848a5eb3f8d8b73a73f03c174fc1.zip
taskset: proper numbers parsing
Reported-by: Davidlohr Bueso <dave@gnu.org> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'schedutils')
-rw-r--r--schedutils/Makefile.am7
-rw-r--r--schedutils/chrt.c10
-rw-r--r--schedutils/ionice.c34
-rw-r--r--schedutils/schedutils.c34
-rw-r--r--schedutils/schedutils.h7
-rw-r--r--schedutils/taskset.c4
6 files changed, 61 insertions, 35 deletions
diff --git a/schedutils/Makefile.am b/schedutils/Makefile.am
index 13d45c85d..c83e5eaff 100644
--- a/schedutils/Makefile.am
+++ b/schedutils/Makefile.am
@@ -2,19 +2,24 @@ include $(top_srcdir)/config/include-Makefile.am
if BUILD_SCHEDUTILS
+srcs_common = schedutils.c schedutils.h
+
usrbin_exec_PROGRAMS = chrt
dist_man_MANS = chrt.1
+chrt_SOURCES = chrt.c $(srcs_common)
+
if HAVE_IOPRIO_GET
if HAVE_IOPRIO_SET
usrbin_exec_PROGRAMS += ionice
+ionice_SOURCES = ionice.c $(srcs_common)
dist_man_MANS += ionice.1
endif
endif
if HAVE_SCHED_GETAFFINITY
usrbin_exec_PROGRAMS += taskset
-taskset_SOURCES = taskset.c $(top_srcdir)/lib/cpuset.c
+taskset_SOURCES = taskset.c $(top_srcdir)/lib/cpuset.c $(srcs_common)
dist_man_MANS += taskset.1
endif
diff --git a/schedutils/chrt.c b/schedutils/chrt.c
index 2d0254fa9..89b12c701 100644
--- a/schedutils/chrt.c
+++ b/schedutils/chrt.c
@@ -32,6 +32,8 @@
#include "c.h"
#include "nls.h"
+#include "schedutils.h"
+
/* the SCHED_BATCH is supported since Linux 2.6.16
* -- temporary workaround for people with old glibc headers
*/
@@ -238,9 +240,7 @@ int main(int argc, char *argv[])
break;
case 'p':
errno = 0;
- pid = strtol(argv[argc - 1], NULL, 10);
- if (errno)
- err(EXIT_FAILURE, _("failed to parse pid"));
+ pid = getnum(argv[argc - 1], _("failed to parse pid"));
break;
case 'r':
policy = SCHED_RR;
@@ -268,9 +268,7 @@ int main(int argc, char *argv[])
}
errno = 0;
- priority = strtol(argv[optind], NULL, 10);
- if (errno)
- err(EXIT_FAILURE, _("failed to parse priority"));
+ priority = getnum(argv[optind], _("failed to parse priority"));
#ifdef SCHED_RESET_ON_FORK
/* sanity check */
diff --git a/schedutils/ionice.c b/schedutils/ionice.c
index 3ad4ef9a6..34132f071 100644
--- a/schedutils/ionice.c
+++ b/schedutils/ionice.c
@@ -18,6 +18,8 @@
#include "nls.h"
+#include "schedutils.h"
+
static int tolerant;
static inline int ioprio_set(int which, int who, int ioprio)
@@ -91,28 +93,6 @@ static void usage(int rc)
exit(rc);
}
-static long getnum(const char *str)
-{
- long num;
- char *end = NULL;
-
- if (str == NULL || *str == '\0')
- goto err;
- errno = 0;
- num = strtol(str, &end, 10);
-
- if (errno || (end && *end))
- goto err;
-
- return num;
-err:
- if (errno)
- err(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
- else
- errx(EXIT_SUCCESS, _("cannot parse number '%s'"), str);
- return 0;
-}
-
int main(int argc, char *argv[])
{
int ioprio = 4, set = 0, ioclass = IOPRIO_CLASS_BE, c;
@@ -125,15 +105,15 @@ int main(int argc, char *argv[])
while ((c = getopt(argc, argv, "+n:c:p:th")) != EOF) {
switch (c) {
case 'n':
- ioprio = getnum(optarg);
+ ioprio = getnum(optarg, _("failed to parse class data"));
set |= 1;
break;
case 'c':
- ioclass = getnum(optarg);
+ ioclass = getnum(optarg, _("failed to parse class"));
set |= 2;
break;
case 'p':
- pid = getnum(optarg);
+ pid = getnum(optarg, _("failed to parse pid"));
break;
case 't':
tolerant = 1;
@@ -167,7 +147,7 @@ int main(int argc, char *argv[])
ioprio_print(pid);
for(; argv[optind]; ++optind) {
- pid = getnum(argv[optind]);
+ pid = getnum(argv[optind], _("failed to parse pid"));
ioprio_print(pid);
}
} else {
@@ -176,7 +156,7 @@ int main(int argc, char *argv[])
for(; argv[optind]; ++optind)
{
- pid = getnum(argv[optind]);
+ pid = getnum(argv[optind], _("failed to parse pid"));
ioprio_setpid(pid, ioprio, ioclass);
}
}
diff --git a/schedutils/schedutils.c b/schedutils/schedutils.c
new file mode 100644
index 000000000..9d6051ba1
--- /dev/null
+++ b/schedutils/schedutils.c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
+ *
+ * Released under the terms of the GNU General Public License version 2
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <err.h>
+
+#include "nls.h"
+
+long getnum(const char *str, const char *errmesg)
+{
+ long num;
+ char *end = NULL;
+
+ if (str == NULL || *str == '\0')
+ goto err;
+ errno = 0;
+ num = strtol(str, &end, 10);
+
+ if (errno || (end && *end))
+ goto err;
+
+ return num;
+err:
+ if (errno)
+ err(EXIT_FAILURE, "%s: '%s'", errmesg, str);
+ else
+ errx(EXIT_FAILURE, "%s: '%s'", errmesg, str);
+ return 0;
+}
diff --git a/schedutils/schedutils.h b/schedutils/schedutils.h
new file mode 100644
index 000000000..80e4f7bfe
--- /dev/null
+++ b/schedutils/schedutils.h
@@ -0,0 +1,7 @@
+#ifndef UTIL_LINUX_SCHED_UTILS
+#define UTIL_LINUX_SCHED_UTILS
+
+extern long getnum(const char *str, const char *errmesg);
+
+#endif
+
diff --git a/schedutils/taskset.c b/schedutils/taskset.c
index 2f1bc745f..201fc1589 100644
--- a/schedutils/taskset.c
+++ b/schedutils/taskset.c
@@ -32,6 +32,8 @@
#include "cpuset.h"
#include "nls.h"
+#include "schedutils.h"
+
static void __attribute__((__noreturn__)) usage(FILE *out)
{
fprintf(out,
@@ -87,7 +89,7 @@ int main(int argc, char *argv[])
while ((opt = getopt_long(argc, argv, "+pchV", longopts, NULL)) != -1) {
switch (opt) {
case 'p':
- pid = atoi(argv[argc - 1]);
+ pid = getnum(argv[argc - 1], _("failed to parse pid"));
break;
case 'c':
c_opt = 1;