[Toybox] [patch] add renice

Felix Janda felix.janda at posteo.de
Tue Jul 30 11:43:11 PDT 2013


Strake wrote:
> On 28/07/2013, Felix Janda <felix.janda at posteo.de> wrote:
> > Some comments below.
> 
> Thanks.

Thanks for your new patch.

[...]
> >> +  for (ii = 0; ii < toys.optc; ii++) {
> >
> > You don't really need the index. Just loop over toys.optargs, which are
> > conveniently null terminated.
> 
> To me, at least, it is clearer with the index, as it's a commoner idiom.

I can't really argue with that.

> >> +    id_t id;
> >> +
> >> +    if (isdigit (toys.optargs[ii][0])) id = strtoul (toys.optargs[ii], 0,
> >> 10);
> >> +    else if (toys.optflags & FLAG_u) id = getpwnam (toys.optargs[ii])
> >> -> pw_uid;
> >
> > Mail mungling FTW.
> 
> ?

Sorry, s/mungling/mangling/. Your mail client or something else has broken
the patch. Anyway, this doesn't apply to the new patch.

[...]
> From 2741527c9b56cf654ff653c6171751112985bae6 Mon Sep 17 00:00:00 2001
> From: Strake <strake888 at gmail.com>
> Date: Sun, 28 Jul 2013 09:30:35 -0500
> Subject: [PATCH] add renice
> 
> ---
>  toys/pending/renice.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 55 insertions(+)
>  create mode 100644 toys/pending/renice.c
> 
> diff --git a/toys/pending/renice.c b/toys/pending/renice.c
> new file mode 100644
> index 0000000..3671f91
> --- /dev/null
> +++ b/toys/pending/renice.c
> @@ -0,0 +1,55 @@
> +/* renice.c - renice process
> + *
> + * Copyright 2013 CE Strake <strake888 at gmail.com>
> + *
> + * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/
> + * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/cmdbehav.html
> +
> +TO DO: -n is required, so use '|' modifier when available
> +
> +USE_RENICE(NEWTOY(renice, "gpun#", TOYFLAG_BIN))
> +
> +config RENICE
> +  bool "renice"
> +  default n
> +  help
> +    usage: renice [-gpu] -n increment ID ...
> +*/
> +
> +#define FOR_renice
> +#include "toys.h"
> +
> +GLOBALS(
> +  long nArgu;
> +)
> +
> +void renice_main (void) {
> +  int ii;
> +  int which = toys.optflags & FLAG_g ? PRIO_PGRP :
> +              toys.optflags & FLAG_u ? PRIO_USER :
> +              PRIO_PROCESS;
> +
> +  if (!(toys.optflags & FLAG_n)) error_exit ("no increment given");
> +
> +  for (ii = 0; ii < toys.optc; ii++) {
> +    id_t id;
> +
> +    if (toys.optflags & FLAG_u) {
> +      struct passwd *p_pw;
> +      p_pw = getpwnam (toys.optargs[ii]);
> +      if (p_pw) {
> +        id = p_pw -> pw_uid;
> +        goto have_id;
> +      }
> +    }

> +    if (!isdigit (toys.optargs[ii][0])) {
> +      error_msg ("not a number: %s", toys.optargs[ii]);
> +      continue;
> +    }
> +    id = strtoul (toys.optargs[ii], 0, 10);

Any reason only to check the first char? Why not

    char *endptr;
...
    id = strtoul (toys.optargs[ii], &endptr, 10);
    if (!(toys.optargs[ii][0] && *endptr)) {
      error_msg ("not a number: %s", toys.optargs[ii]);
      continue;
    }

> +have_id:
> +    if (setpriority (which, id, getpriority (which, id) + TT.nArgu) < 0) {
> +      perror_msg ("failed to setpriority of %d", id);
> +    }
> +  }
> +}

Felix

 1375209791.0


More information about the Toybox mailing list