Commit | Line | Data |
---|---|---|
c018628f DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
7 | #ifndef lint | |
8 | char copyright[] = | |
9 | "@(#) Copyright (c) 1980 Regents of the University of California.\n\ | |
10 | All rights reserved.\n"; | |
11 | #endif not lint | |
12 | ||
47aa95b6 | 13 | #ifndef lint |
c018628f DF |
14 | static char sccsid[] = "@(#)renice.c 5.1 (Berkeley) %G%"; |
15 | #endif not lint | |
47aa95b6 | 16 | |
ce4fd43b SL |
17 | #include <sys/time.h> |
18 | #include <sys/resource.h> | |
47aa95b6 SL |
19 | #include <stdio.h> |
20 | #include <pwd.h> | |
21 | ||
22 | /* | |
23 | * Change the priority (nice) of processes | |
24 | * or groups of processes which are already | |
25 | * running. | |
26 | */ | |
27 | main(argc, argv) | |
28 | char **argv; | |
29 | { | |
30 | int which = PRIO_PROCESS; | |
fe3b5491 | 31 | int who = 0, prio, errs = 0; |
47aa95b6 SL |
32 | |
33 | argc--, argv++; | |
a9ad9e31 | 34 | if (argc < 2) { |
29bac035 SL |
35 | fprintf(stderr, "usage: renice priority [ [ -p ] pids ] "); |
36 | fprintf(stderr, "[ [ -g ] pgrps ] [ [ -u ] users ]\n"); | |
6824099d | 37 | exit(1); |
47aa95b6 SL |
38 | } |
39 | prio = atoi(*argv); | |
40 | argc--, argv++; | |
41 | if (prio > PRIO_MAX) | |
42 | prio = PRIO_MAX; | |
43 | if (prio < PRIO_MIN) | |
44 | prio = PRIO_MIN; | |
47aa95b6 | 45 | for (; argc > 0; argc--, argv++) { |
6824099d SL |
46 | if (strcmp(*argv, "-g") == 0) { |
47 | which = PRIO_PGRP; | |
48 | continue; | |
49 | } | |
50 | if (strcmp(*argv, "-u") == 0) { | |
51 | which = PRIO_USER; | |
52 | continue; | |
53 | } | |
54 | if (strcmp(*argv, "-p") == 0) { | |
55 | which = PRIO_PROCESS; | |
56 | continue; | |
57 | } | |
47aa95b6 SL |
58 | if (which == PRIO_USER) { |
59 | register struct passwd *pwd = getpwnam(*argv); | |
60 | ||
61 | if (pwd == NULL) { | |
62 | fprintf(stderr, "renice: %s: unknown user\n", | |
63 | *argv); | |
64 | continue; | |
65 | } | |
fe3b5491 | 66 | who = pwd->pw_uid; |
47aa95b6 | 67 | } else { |
fe3b5491 SL |
68 | who = atoi(*argv); |
69 | if (who < 0) { | |
47aa95b6 SL |
70 | fprintf(stderr, "renice: %s: bad value\n", |
71 | *argv); | |
72 | continue; | |
73 | } | |
74 | } | |
fe3b5491 | 75 | errs += donice(which, who, prio); |
47aa95b6 SL |
76 | } |
77 | exit(errs != 0); | |
78 | } | |
79 | ||
fe3b5491 SL |
80 | donice(which, who, prio) |
81 | int which, who, prio; | |
82 | { | |
6824099d | 83 | int oldprio; |
fe3b5491 SL |
84 | extern int errno; |
85 | ||
6824099d | 86 | errno = 0, oldprio = getpriority(which, who); |
fe3b5491 SL |
87 | if (oldprio == -1 && errno) { |
88 | fprintf(stderr, "renice: %d: ", who); | |
89 | perror("getpriority"); | |
90 | return (1); | |
91 | } | |
92 | if (setpriority(which, who, prio) < 0) { | |
93 | fprintf(stderr, "renice: %d: ", who); | |
94 | perror("setpriority"); | |
95 | return (1); | |
96 | } | |
97 | printf("%d: old priority %d, new priority %d\n", who, oldprio, prio); | |
98 | return (0); | |
99 | } |