1992-11-01 05:44:29 +00:00
|
|
|
/* nice -- run a program with modified scheduling priority
|
2002-02-24 17:24:06 +00:00
|
|
|
Copyright (C) 1990-2002 Free Software Foundation, Inc.
|
1992-11-01 05:44:29 +00:00
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
1996-03-24 18:33:12 +00:00
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
|
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1993-09-08 18:23:12 +00:00
|
|
|
/* David MacKenzie <djm@gnu.ai.mit.edu> */
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1993-10-12 14:49:11 +00:00
|
|
|
#include <config.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <stdio.h>
|
1994-09-24 15:20:21 +00:00
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#ifndef NICE_PRIORITY
|
1997-05-01 20:51:16 +00:00
|
|
|
# include <sys/time.h>
|
|
|
|
|
# include <sys/resource.h>
|
1992-11-01 05:44:29 +00:00
|
|
|
#endif
|
1993-09-08 18:23:12 +00:00
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
#include "system.h"
|
2002-02-24 17:24:06 +00:00
|
|
|
#include "closeout.h"
|
1994-12-20 05:26:44 +00:00
|
|
|
#include "error.h"
|
1999-08-29 21:09:34 +00:00
|
|
|
#include "long-options.h"
|
2002-02-24 17:24:06 +00:00
|
|
|
#include "posixver.h"
|
1999-08-29 21:09:34 +00:00
|
|
|
#include "xstrtol.h"
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
/* The official name of this program (e.g., no `g' prefix). */
|
|
|
|
|
#define PROGRAM_NAME "nice"
|
|
|
|
|
|
|
|
|
|
#define AUTHORS "David MacKenzie"
|
|
|
|
|
|
1993-10-12 01:52:24 +00:00
|
|
|
#ifdef NICE_PRIORITY
|
1997-05-01 20:51:16 +00:00
|
|
|
# define GET_PRIORITY() nice (0)
|
1993-10-12 01:52:24 +00:00
|
|
|
#else
|
1997-05-01 20:51:16 +00:00
|
|
|
# define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
|
1993-10-12 01:52:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
1992-11-01 05:44:29 +00:00
|
|
|
/* The name this program was run with. */
|
|
|
|
|
char *program_name;
|
|
|
|
|
|
1992-11-12 04:14:54 +00:00
|
|
|
static struct option const longopts[] =
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
1992-12-02 18:51:53 +00:00
|
|
|
{"adjustment", required_argument, NULL, 'n'},
|
1992-11-01 05:44:29 +00:00
|
|
|
{NULL, 0, NULL, 0}
|
|
|
|
|
};
|
|
|
|
|
|
1999-01-25 14:28:31 +00:00
|
|
|
void
|
|
|
|
|
usage (int status)
|
|
|
|
|
{
|
|
|
|
|
if (status != 0)
|
|
|
|
|
fprintf (stderr, _("Try `%s --help' for more information.\n"),
|
|
|
|
|
program_name);
|
|
|
|
|
else
|
|
|
|
|
{
|
1999-08-29 20:47:17 +00:00
|
|
|
printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
|
2001-12-15 20:46:30 +00:00
|
|
|
fputs (_("\
|
1999-01-25 14:28:31 +00:00
|
|
|
Run COMMAND with an adjusted scheduling priority.\n\
|
|
|
|
|
With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
|
|
|
|
|
by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
|
|
|
|
|
\n\
|
2002-02-11 23:23:42 +00:00
|
|
|
-n, --adjustment=ADJUST increment priority by ADJUST first\n\
|
2001-12-15 20:46:30 +00:00
|
|
|
"), stdout);
|
|
|
|
|
fputs (HELP_OPTION_DESCRIPTION, stdout);
|
|
|
|
|
fputs (VERSION_OPTION_DESCRIPTION, stdout);
|
2002-07-02 09:09:10 +00:00
|
|
|
printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
|
1999-01-25 14:28:31 +00:00
|
|
|
}
|
|
|
|
|
exit (status);
|
|
|
|
|
}
|
|
|
|
|
|
1996-03-21 22:47:02 +00:00
|
|
|
int
|
1996-01-06 11:44:05 +00:00
|
|
|
main (int argc, char **argv)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
int current_priority;
|
1999-08-29 21:09:34 +00:00
|
|
|
long int adjustment = 0;
|
1992-11-01 05:44:29 +00:00
|
|
|
int minusflag = 0;
|
|
|
|
|
int adjustment_given = 0;
|
1997-01-08 04:47:56 +00:00
|
|
|
int i;
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2003-06-17 18:13:23 +00:00
|
|
|
initialize_main (&argc, &argv);
|
1992-11-01 05:44:29 +00:00
|
|
|
program_name = argv[0];
|
1996-03-12 23:49:29 +00:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
|
2000-05-07 14:55:15 +00:00
|
|
|
atexit (close_stdout);
|
|
|
|
|
|
1999-03-31 04:11:35 +00:00
|
|
|
parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
|
|
|
|
|
AUTHORS, usage);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1997-01-08 04:47:56 +00:00
|
|
|
for (i = 1; i < argc; /* empty */)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
1997-01-08 04:47:56 +00:00
|
|
|
char *s = argv[i];
|
1992-11-01 05:44:29 +00:00
|
|
|
|
2002-02-16 09:03:12 +00:00
|
|
|
if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2])
|
|
|
|
|
&& posix2_version () < 200112)
|
1994-09-24 15:20:21 +00:00
|
|
|
{
|
1999-08-29 21:09:34 +00:00
|
|
|
if (xstrtol (&s[2], NULL, 10, &adjustment, "") != LONGINT_OK)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, 0, _("invalid option `%s'"), s);
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1994-09-24 15:20:21 +00:00
|
|
|
minusflag = 1;
|
|
|
|
|
adjustment_given = 1;
|
1997-01-08 04:47:56 +00:00
|
|
|
++i;
|
1994-09-24 15:20:21 +00:00
|
|
|
}
|
2002-02-16 09:03:12 +00:00
|
|
|
else if (s[0] == '-'
|
|
|
|
|
&& (ISDIGIT (s[1]) || (s[1] == '+' && ISDIGIT (s[2])))
|
|
|
|
|
&& posix2_version () < 200112)
|
1996-04-05 13:32:39 +00:00
|
|
|
{
|
1996-08-04 14:15:25 +00:00
|
|
|
if (s[1] == '+')
|
|
|
|
|
++s;
|
1999-08-29 21:09:34 +00:00
|
|
|
if (xstrtol (&s[1], NULL, 10, &adjustment, "") != LONGINT_OK)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, 0, _("invalid option `%s'"), s);
|
1996-08-03 04:23:43 +00:00
|
|
|
|
|
|
|
|
minusflag = 0;
|
1996-04-05 13:32:39 +00:00
|
|
|
adjustment_given = 1;
|
1997-01-08 04:47:56 +00:00
|
|
|
++i;
|
1996-04-05 13:32:39 +00:00
|
|
|
}
|
1994-09-24 15:20:21 +00:00
|
|
|
else
|
|
|
|
|
{
|
1996-04-05 13:32:39 +00:00
|
|
|
int optc;
|
1997-01-08 04:47:56 +00:00
|
|
|
char **fake_argv = argv + i - 1;
|
|
|
|
|
|
|
|
|
|
/* Initialize getopt_long's internal state. */
|
|
|
|
|
optind = 0;
|
|
|
|
|
|
|
|
|
|
if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
|
1997-02-01 03:05:36 +00:00
|
|
|
longopts, NULL)) != -1)
|
1994-09-24 12:50:59 +00:00
|
|
|
{
|
1996-04-05 13:32:39 +00:00
|
|
|
switch (optc)
|
1994-09-24 15:20:21 +00:00
|
|
|
{
|
1996-04-05 13:32:39 +00:00
|
|
|
case '?':
|
2002-08-31 08:52:10 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1996-04-05 13:32:39 +00:00
|
|
|
|
|
|
|
|
case 'n':
|
1999-08-29 21:09:34 +00:00
|
|
|
if (xstrtol (optarg, NULL, 10, &adjustment, "")
|
|
|
|
|
!= LONGINT_OK)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, 0, _("invalid priority `%s'"), optarg);
|
1996-08-03 04:23:43 +00:00
|
|
|
|
|
|
|
|
minusflag = 0;
|
1996-04-05 13:32:39 +00:00
|
|
|
adjustment_given = 1;
|
|
|
|
|
break;
|
1994-09-24 15:20:21 +00:00
|
|
|
}
|
1994-09-24 12:50:59 +00:00
|
|
|
}
|
1996-04-05 13:32:39 +00:00
|
|
|
|
1997-01-08 04:47:56 +00:00
|
|
|
i += optind - 1;
|
|
|
|
|
|
1996-04-05 13:32:39 +00:00
|
|
|
if (optc == EOF)
|
|
|
|
|
break;
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (minusflag)
|
|
|
|
|
adjustment = -adjustment;
|
|
|
|
|
if (!adjustment_given)
|
|
|
|
|
adjustment = 10;
|
|
|
|
|
|
1997-01-08 04:47:56 +00:00
|
|
|
if (i == argc)
|
1992-11-01 05:44:29 +00:00
|
|
|
{
|
|
|
|
|
if (adjustment_given)
|
1993-11-27 21:59:45 +00:00
|
|
|
{
|
1995-08-08 04:37:34 +00:00
|
|
|
error (0, 0, _("a command must be given with an adjustment"));
|
2002-08-31 08:52:10 +00:00
|
|
|
usage (EXIT_FAILURE);
|
1993-11-27 21:59:45 +00:00
|
|
|
}
|
1992-11-01 05:44:29 +00:00
|
|
|
/* No command given; print the priority. */
|
|
|
|
|
errno = 0;
|
1993-10-12 01:52:24 +00:00
|
|
|
current_priority = GET_PRIORITY ();
|
1992-11-01 05:44:29 +00:00
|
|
|
if (current_priority == -1 && errno != 0)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("cannot get priority"));
|
1992-11-01 05:44:29 +00:00
|
|
|
printf ("%d\n", current_priority);
|
2002-08-31 08:52:10 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef NICE_PRIORITY
|
1993-10-12 01:52:24 +00:00
|
|
|
errno = 0;
|
|
|
|
|
current_priority = GET_PRIORITY ();
|
1992-11-01 05:44:29 +00:00
|
|
|
if (current_priority == -1 && errno != 0)
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("cannot get priority"));
|
1992-11-01 05:44:29 +00:00
|
|
|
if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
|
|
|
|
|
#else
|
|
|
|
|
if (nice (adjustment) == -1)
|
|
|
|
|
#endif
|
2002-08-30 23:04:53 +00:00
|
|
|
error (EXIT_FAILURE, errno, _("cannot set priority"));
|
1992-11-01 05:44:29 +00:00
|
|
|
|
1997-01-08 04:47:56 +00:00
|
|
|
execvp (argv[i], &argv[i]);
|
1999-08-29 20:47:17 +00:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
int exit_status = (errno == ENOENT ? 127 : 126);
|
|
|
|
|
error (0, errno, "%s", argv[i]);
|
|
|
|
|
exit (exit_status);
|
|
|
|
|
}
|
1992-11-01 05:44:29 +00:00
|
|
|
}
|