~ubuntu-branches/ubuntu/precise/util-linux/precise-proposed

« back to all changes in this revision

Viewing changes to schedutils/chrt.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-06-20 22:31:50 UTC
  • mfrom: (1.6.3 upstream) (4.5.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110620223150-lz8wrv0946ihcz3z
Tags: 2.19.1-2ubuntu1
* Merge from Debian unstable, remaining changes:
  - Build for multiarch.
  - Add pre-depends on multiarch-support.
  - configure.ac: don't try to be clever about extracting a path name from
    $libdir to append to /usr in a way that's not overridable; instead,
    reuse the built-in configurable libexecdir.
  - Fix up the .pc.in files to know about libexecdir, so our substitutions
    don't leave us with unusable pkg-config files.
  - Install custom blkid.conf to use /dev/.blkid.tab since we don't
    expect device names to survive a reboot
  - Mention mountall(8) in fstab(5) manpages, along with its special
    options.
  - Since upstart is required in Ubuntu, the hwclock.sh init script is not
    called on startup and the hwclockfirst.sh init script is removed.
  - Drop depends on initscripts for the above.
  - Replace hwclock udev rule with an Upstart job.
  - For the case where mount is called with a directory to mount, look
    that directory up in mountall's /lib/init/fstab if we couldn't find
    it mentioned anywhere else.  This means "mount /proc", "mount /sys",
    etc. work.
  - mount.8 points to the cifs-utils package, not the obsolete smbfs one. 
* Dropped changes:
  - mount.preinst: lsb_release has been fixed in lucid and above to be
    usable without configuration, so we don't have to diverge from Debian
    here anymore.
* Changes merged upstream:
  - sfdisk support for '+' with '-N'
  - mount/umount.c: fix a segfault on umount with empty mtab entry
  - Fix arbitrary unmount with fuse security issue

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include "c.h"
33
33
#include "nls.h"
34
34
 
 
35
#include "strutils.h"
 
36
 
35
37
/* the SCHED_BATCH is supported since Linux 2.6.16
36
38
 *  -- temporary workaround for people with old glibc headers
37
39
 */
47
49
# define SCHED_IDLE 5
48
50
#endif
49
51
 
50
 
static void show_usage(int rc)
 
52
#if defined(__linux__) && !defined(SCHED_RESET_ON_FORK)
 
53
#define SCHED_RESET_ON_FORK 0x40000000
 
54
#endif
 
55
 
 
56
 
 
57
static void __attribute__((__noreturn__)) show_usage(int rc)
51
58
{
52
 
        fprintf(stdout, _(
53
 
        "\nchrt - manipulate real-time attributes of a process.\n"
 
59
        FILE *out = rc == EXIT_SUCCESS ? stdout : stderr;
 
60
 
 
61
        fprintf(out, _(
 
62
        "\nchrt - manipulate real-time attributes of a process\n"
54
63
        "\nSet policy:\n"
55
64
        "  chrt [options] <policy> <priority> {<pid> | <command> [<arg> ...]}\n"
56
65
        "\nGet policy:\n"
57
 
        "  chrt [options] {<pid> | <command> [<arg> ...]}\n\n"
 
66
        "  chrt [options] {<pid> | <command> [<arg> ...]}\n"));
 
67
 
 
68
        fprintf(out, _(
58
69
        "\nScheduling policies:\n"
59
70
        "  -b | --batch         set policy to SCHED_BATCH\n"
60
71
        "  -f | --fifo          set policy to SCHED_FIFO\n"
61
72
        "  -i | --idle          set policy to SCHED_IDLE\n"
62
73
        "  -o | --other         set policy to SCHED_OTHER\n"
63
 
        "  -r | --rr            set policy to SCHED_RR (default)\n"
 
74
        "  -r | --rr            set policy to SCHED_RR (default)\n"));
 
75
 
 
76
#ifdef SCHED_RESET_ON_FORK
 
77
        fprintf(out, _(
 
78
        "\nScheduling flags:\n"
 
79
        "  -R | --reset-on-fork set SCHED_RESET_ON_FORK for FIFO or RR\n"));
 
80
#endif
 
81
        fprintf(out, _(
64
82
        "\nOptions:\n"
65
83
        "  -h | --help          display this help\n"
 
84
        "  -m | --max           show min and max valid priorities\n"
66
85
        "  -p | --pid           operate on existing given pid\n"
67
 
        "  -m | --max           show min and max valid priorities\n"
68
86
        "  -v | --verbose       display status information\n"
69
87
        "  -V | --version       output version information\n\n"));
70
88
 
71
89
        exit(rc);
72
90
}
73
91
 
74
 
static void show_rt_info(const char *what, pid_t pid)
 
92
static void show_rt_info(pid_t pid, int isnew)
75
93
{
76
94
        struct sched_param sp;
77
95
        int policy;
84
102
        if (policy == -1)
85
103
                err(EXIT_FAILURE, _("failed to get pid %d's policy"), pid);
86
104
 
87
 
        printf(_("pid %d's %s scheduling policy: "), pid, what);
 
105
        if (isnew)
 
106
                printf(_("pid %d's new scheduling policy: "), pid);
 
107
        else
 
108
                printf(_("pid %d's current scheduling policy: "), pid);
 
109
 
88
110
        switch (policy) {
89
111
        case SCHED_OTHER:
90
112
                printf("SCHED_OTHER\n");
92
114
        case SCHED_FIFO:
93
115
                printf("SCHED_FIFO\n");
94
116
                break;
 
117
#ifdef SCHED_RESET_ON_FORK
 
118
        case SCHED_FIFO|SCHED_RESET_ON_FORK:
 
119
                printf("SCHED_FIFO|SCHED_RESET_ON_FORK\n");
 
120
                break;
 
121
#endif
95
122
#ifdef SCHED_IDLE
96
123
        case SCHED_IDLE:
97
124
                printf("SCHED_IDLE\n");
100
127
        case SCHED_RR:
101
128
                printf("SCHED_RR\n");
102
129
                break;
 
130
#ifdef SCHED_RESET_ON_FORK
 
131
        case SCHED_RR|SCHED_RESET_ON_FORK:
 
132
                printf("SCHED_RR|SCHED_RESET_ON_FORK\n");
 
133
                break;
 
134
#endif
103
135
#ifdef SCHED_BATCH
104
136
        case SCHED_BATCH:
105
137
                printf("SCHED_BATCH\n");
112
144
        if (sched_getparam(pid, &sp))
113
145
                err(EXIT_FAILURE, _("failed to get pid %d's attributes"), pid);
114
146
 
115
 
        printf(_("pid %d's %s scheduling priority: %d\n"),
116
 
                pid, what, sp.sched_priority);
 
147
        if (isnew)
 
148
                printf(_("pid %d's new scheduling priority: %d\n"),
 
149
                       pid, sp.sched_priority);
 
150
        else
 
151
                printf(_("pid %d's current scheduling priority: %d\n"),
 
152
                       pid, sp.sched_priority);
117
153
}
118
154
 
119
155
static void show_min_max(void)
150
186
 
151
187
int main(int argc, char *argv[])
152
188
{
153
 
        int i, policy = SCHED_RR, priority = 0, verbose = 0;
 
189
        int i, policy = SCHED_RR, priority = 0, verbose = 0, policy_flag = 0;
154
190
        struct sched_param sp;
155
191
        pid_t pid = -1;
156
192
 
163
199
                { "max",        0, NULL, 'm' },
164
200
                { "other",      0, NULL, 'o' },
165
201
                { "rr",         0, NULL, 'r' },
 
202
                { "reset-on-fork", 0, NULL, 'R' },
166
203
                { "verbose",    0, NULL, 'v' },
167
204
                { "version",    0, NULL, 'V' },
168
205
                { NULL,         0, NULL, 0 }
172
209
        bindtextdomain(PACKAGE, LOCALEDIR);
173
210
        textdomain(PACKAGE);
174
211
 
175
 
        while((i = getopt_long(argc, argv, "+bfiphmorvV", longopts, NULL)) != -1)
 
212
        while((i = getopt_long(argc, argv, "+bfiphmoRrvV", longopts, NULL)) != -1)
176
213
        {
177
214
                int ret = EXIT_FAILURE;
178
215
 
185
222
                case 'f':
186
223
                        policy = SCHED_FIFO;
187
224
                        break;
 
225
#ifdef SCHED_RESET_ON_FORK
 
226
                case 'R':
 
227
                        policy_flag |= SCHED_RESET_ON_FORK;
 
228
                        break;
 
229
#endif
188
230
                case 'i':
189
231
#ifdef SCHED_IDLE
190
232
                        policy = SCHED_IDLE;
198
240
                        break;
199
241
                case 'p':
200
242
                        errno = 0;
201
 
                        pid = strtol(argv[argc - 1], NULL, 10);
202
 
                        if (errno)
203
 
                                err(EXIT_FAILURE, _("failed to parse pid"));
 
243
                        pid = strtol_or_err(argv[argc - 1], _("failed to parse pid"));
204
244
                        break;
205
245
                case 'r':
206
246
                        policy = SCHED_RR;
222
262
                show_usage(EXIT_FAILURE);
223
263
 
224
264
        if ((pid > -1) && (verbose || argc - optind == 1)) {
225
 
                show_rt_info(_("current"), pid);
 
265
                show_rt_info(pid, FALSE);
226
266
                if (argc - optind == 1)
227
267
                        return EXIT_SUCCESS;
228
268
        }
229
269
 
230
270
        errno = 0;
231
 
        priority = strtol(argv[optind], NULL, 10);
232
 
        if (errno)
233
 
                err(EXIT_FAILURE, _("failed to parse priority"));
 
271
        priority = strtol_or_err(argv[optind], _("failed to parse priority"));
 
272
 
 
273
#ifdef SCHED_RESET_ON_FORK
 
274
        /* sanity check */
 
275
        if ((policy_flag & SCHED_RESET_ON_FORK) &&
 
276
            !(policy == SCHED_FIFO || policy == SCHED_RR))
 
277
                errx(EXIT_FAILURE, _("SCHED_RESET_ON_FORK flag is suppoted for "
 
278
                                "SCHED_FIFO and SCHED_RR policies only"));
 
279
#endif
 
280
 
 
281
        policy |= policy_flag;
234
282
 
235
283
        if (pid == -1)
236
284
                pid = 0;
239
287
                err(EXIT_FAILURE, _("failed to set pid %d's policy"), pid);
240
288
 
241
289
        if (verbose)
242
 
                show_rt_info(_("new"), pid);
 
290
                show_rt_info(pid, TRUE);
243
291
 
244
292
        if (!pid) {
245
293
                argv += optind + 1;