summaryrefslogtreecommitdiffstats
path: root/sys-utils/choom.c
diff options
context:
space:
mode:
authorKarel Zak2018-06-21 13:46:20 +0200
committerKarel Zak2018-06-21 13:49:16 +0200
commitbd5f7f2ef304fed20ae5321770a8a718f9a0f9a8 (patch)
tree2c403feb30c016ad4e5bd43ab5f49dbd8970f149 /sys-utils/choom.c
parentlib/path: allow dir-path formatting (diff)
downloadkernel-qcow2-util-linux-bd5f7f2ef304fed20ae5321770a8a718f9a0f9a8.tar.gz
kernel-qcow2-util-linux-bd5f7f2ef304fed20ae5321770a8a718f9a0f9a8.tar.xz
kernel-qcow2-util-linux-bd5f7f2ef304fed20ae5321770a8a718f9a0f9a8.zip
choom: use new ul_path_* API
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/choom.c')
-rw-r--r--sys-utils/choom.c43
1 files changed, 26 insertions, 17 deletions
diff --git a/sys-utils/choom.c b/sys-utils/choom.c
index 21cb0ee10..6895aef43 100644
--- a/sys-utils/choom.c
+++ b/sys-utils/choom.c
@@ -52,31 +52,36 @@ static void __attribute__((__noreturn__)) usage(void)
exit(EXIT_SUCCESS);
}
-static int get_score(const pid_t pid)
+static int get_score(struct path_cxt *pc)
{
- return path_read_s32("/proc/%d/oom_score", (int) pid);
-}
+ int ret;
-static int get_score_adj(const pid_t pid)
-{
- return path_read_s32("/proc/%d/oom_score_adj", (int) pid);
+ if (ul_path_read_s32(pc, &ret, "oom_score") != 0)
+ err(EXIT_FAILURE, _("failed to read OOM score value"));
+
+ return ret;
}
-static int set_score_adj(const pid_t pid, int adj)
+static int get_score_adj(struct path_cxt *pc)
{
- char buf[sizeof(stringify_value(INT_MAX))];
+ int ret;
- snprintf(buf, sizeof(buf), "%d", adj);
+ if (ul_path_read_s32(pc, &ret, "oom_score_adj") != 0)
+ err(EXIT_FAILURE, _("failed to read OOM score adjust value"));
- if (path_write_str(buf, "/proc/%d/oom_score_adj", (int) pid) < 0)
- return -1;
- return 0;
+ return ret;
+}
+
+static int set_score_adj(struct path_cxt *pc, int adj)
+{
+ return ul_path_write_u64(pc, adj, "oom_score_adj");
}
int main(int argc, char **argv)
{
pid_t pid = 0;
int c, adj = 0, has_adj = 0;
+ struct path_cxt *pc = NULL;
static const struct option longopts[] = {
{ "adjust", required_argument, NULL, 'n' },
@@ -123,28 +128,32 @@ int main(int argc, char **argv)
errtryhelp(EXIT_FAILURE);
}
+ pc = ul_new_path("/proc/%d", (int) (pid ? pid : getpid()));
+
/* Show */
if (!has_adj) {
- printf(_("pid %d's current OOM score: %d\n"), pid, get_score(pid));
- printf(_("pid %d's current OOM score adjust value: %d\n"), pid, get_score_adj(pid));
+ printf(_("pid %d's current OOM score: %d\n"), pid, get_score(pc));
+ printf(_("pid %d's current OOM score adjust value: %d\n"), pid, get_score_adj(pc));
/* Change */
} else if (pid) {
- int old = get_score_adj(pid);
+ int old = get_score_adj(pc);
- if (set_score_adj(pid, adj))
+ if (set_score_adj(pc, adj))
err(EXIT_FAILURE, _("failed to set score adjust value"));
printf(_("pid %d's OOM score adjust value changed from %d to %d\n"), pid, old, adj);
/* Start new process */
} else {
- if (set_score_adj(getpid(), adj))
+ if (set_score_adj(pc, adj))
err(EXIT_FAILURE, _("failed to set score adjust value"));
+ ul_unref_path(pc);
argv += optind;
execvp(argv[0], argv);
errexec(argv[0]);
}
+ ul_unref_path(pc);
return EXIT_SUCCESS;
}