logo

utils-std

Collection of commonly available Unix tools
commit: f0ce444240d26044f870cb9a26f2d3c8cf38ce11
parent aab88c6c5d3ad3a69add4436100e8523b5552fd5
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 18 Apr 2024 03:34:07 +0200

cmd/nice: new

Diffstat:

Acmd/nice.157+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acmd/nice.c91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcoreutils.txt2+-
Mlsb_commands.txt2+-
Mposix_utilities.txt2+-
5 files changed, 151 insertions(+), 3 deletions(-)

diff --git a/cmd/nice.1 b/cmd/nice.1 @@ -0,0 +1,57 @@ +.\" utils-std: Collection of commonly available Unix tools +.\" Copyright 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +.\" SPDX-License-Identifier: MPL-2.0 +.Dd 2024-04-18 +.Dt NICE 1 +.Os +.Sh NAME +.Nm nice +.Nd invoke command with modified nice scheduling value +.Sh SYNOPSIS +.Nm +.Op Fl n Ar increment +.Ar command +.Op Ar arguments ... +.Sh DESCRIPTION +.Nm +runs +.Ar command +with a modified niceness, changing the scheduling of the process. +.Pp +As required by POSIX, if the user lacks the privileges to change the niceness a warning message is printed to stderr but the execution continues and +.Ar command +gets executed, without it's niceness changed. +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl n Ar increment +Positive or negative integer used to adjust the niceness of the process. (default: 0) +.El +.Sh EXIT STATUS +If +.Ar command +is invoked, the exit status of +.Nm +shall be the exit status of +.Ar command ; +Otherwise, the +.Nm +utility shall exit with one of the following values: +.Bl -tag -width Ds +.It 125 +An error occured within the +.Nm +utility. +.It 126 +.Ar command +was found but couldn't be invoked. +.It 127 +.Ar command +wasn't found. +.El +.Sh STANDARDS +.Nm +should be compliant with the +.St -p1003.1-2008 +specification. +.Sh AUTHORS +.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me diff --git a/cmd/nice.c b/cmd/nice.c @@ -0,0 +1,91 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _POSIX_C_SOURCE 200809L +#define _XOPEN_SOURCE 700 // nice() is in XSI + +#include <assert.h> +#include <errno.h> +#include <stdio.h> // fprintf +#include <stdlib.h> // abort +#include <string.h> // strerror +#include <unistd.h> // getopt, nice + +static void +usage() +{ + fprintf(stderr, "Usage: nice [-n increment] command [argument ...]\n"); +} + +int +main(int argc, char *argv[]) +{ + long incr = 0; + + int c = -1; + while((c = getopt(argc, argv, ":n:")) != -1) + { + switch(c) + { + case 'n': + assert(errno == 0); + char *endptr = ""; + + incr = strtol(optarg, &endptr, 10); + + if(*endptr != 0) errno = EINVAL; + if(errno != 0) + { + fprintf( + stderr, "nice: Error: Failed parsing '%s' as a number: %s\n", optarg, strerror(errno)); + usage(); + return 125; + } + break; + case ':': + fprintf(stderr, "nice: Error: Missing operand for option: '-%c'\n", optopt); + usage(); + return 125; + case '?': + fprintf(stderr, "nice: Error: Unrecognised option: '-%c'\n", optopt); + usage(); + return 125; + default: + abort(); + } + } + + argc -= optind; + argv += optind; + + errno = 0; + + if(nice((int)incr) == -1) + { + switch(errno) + { + case 0: + break; + case EPERM: + fprintf(stderr, "nice: Warning: Failed setting nice to %ld: %s\n", incr, strerror(errno)); + errno = 0; + break; + default: + fprintf(stderr, "nice: Error: Failed setting nice to %ld: %s\n", incr, strerror(errno)); + return 125; + } + } + + assert(argv[0]); + assert(errno == 0); + /* flawfinder: ignore. No restrictions on commands is intended */ + if(execvp(argv[0], argv) < 0) + { + fprintf(stderr, "nice: execvp(\"%s\", ...): %s\n", argv[0], strerror(errno)); + + return (errno == ENOENT) ? 127 : 126; + } + + abort(); +} diff --git a/coreutils.txt b/coreutils.txt @@ -48,7 +48,7 @@ mkfifo: ? mknod: Todo mktemp: Todo mv: Todo -nice: Maybe +nice: Done nl: No, use sed nohup: Done nproc: ? diff --git a/lsb_commands.txt b/lsb_commands.txt @@ -86,7 +86,7 @@ mount: out of scope msgfmt: out of scope mv: Todo newgrp: out of scope -nice: Maybe +nice: Done nl: No, use sed nohup: Done od: Todo diff --git a/posix_utilities.txt b/posix_utilities.txt @@ -85,7 +85,7 @@ mkfifo more: no? mv newgrp -nice +nice: done nl nm: no, toolchain nohup: done