Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/miscutils/chrt.c

    r1765 r2725  
    22/*
    33 * chrt - manipulate real-time attributes of a process
    4  * Copyright (c) 2006-2007 Bernhard Fischer
     4 * Copyright (c) 2006-2007 Bernhard Reutner-Fischer
    55 *
    6  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    77 */
    8 
    98#include <sched.h>
    10 #include <getopt.h> /* optind */
    119#include "libbb.h"
    1210#ifndef _POSIX_PRIORITY_SCHEDULING
    1311#warning your system may be foobared
    1412#endif
     13
    1514static const struct {
    16     const int policy;
    17     const char const name[12];
     15    int policy;
     16    char name[sizeof("SCHED_OTHER")];
    1817} policies[] = {
    1918    {SCHED_OTHER, "SCHED_OTHER"},
     
    2221};
    2322
     23//TODO: add
     24// -b, SCHED_BATCH
     25// -i, SCHED_IDLE
     26
    2427static void show_min_max(int pol)
    2528{
    26     const char *fmt = "%s min/max priority\t: %d/%d\n\0%s not supported?\n";
     29    const char *fmt = "%s min/max priority\t: %u/%u\n";
    2730    int max, min;
     31
    2832    max = sched_get_priority_max(pol);
    2933    min = sched_get_priority_min(pol);
    30     if (max >= 0 && min >= 0)
    31         printf(fmt, policies[pol].name, min, max);
    32     else {
    33         fmt += 29;
    34         printf(fmt, policies[pol].name);
    35     }
     34    if ((max|min) < 0)
     35        fmt = "%s not supported\n";
     36    printf(fmt, policies[pol].name, min, max);
    3637}
    3738
     
    4243#define OPT_o (1<<4)
    4344
    44 int chrt_main(int argc, char** argv);
    45 int chrt_main(int argc, char** argv)
     45int chrt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     46int chrt_main(int argc UNUSED_PARAM, char **argv)
    4647{
    4748    pid_t pid = 0;
    4849    unsigned opt;
    4950    struct sched_param sp;
    50     char *p_opt = NULL, *priority = NULL;
    51     const char *state = "current\0new";
    52     int prio = 0, policy = SCHED_RR;
     51    char *pid_str;
     52    char *priority = priority; /* for compiler */
     53    const char *current_new;
     54    int policy = SCHED_RR;
    5355
    54     opt_complementary = "r--fo:f--ro:r--fo"; /* only one policy accepted */
    55     opt = getopt32(argv, "+mp:rfo", &p_opt);
     56    /* at least 1 arg; only one policy accepted */
     57    opt_complementary = "-1:r--fo:f--ro:r--fo";
     58    opt = getopt32(argv, "+mprfo");
    5659    if (opt & OPT_r)
    5760        policy = SCHED_RR;
     
    6063    if (opt & OPT_o)
    6164        policy = SCHED_OTHER;
    62 
    6365    if (opt & OPT_m) { /* print min/max */
    6466        show_min_max(SCHED_FIFO);
     
    6769        fflush_stdout_and_exit(EXIT_SUCCESS);
    6870    }
     71
     72    argv += optind;
    6973    if (opt & OPT_p) {
    70         if (argc == optind+1) { /* -p <priority> <pid> */
    71             priority = p_opt;
    72             p_opt = argv[optind];
     74        pid_str = *argv++;
     75        if (*argv) { /* "-p <priority> <pid> [...]" */
     76            priority = pid_str;
     77            pid_str = *argv;
    7378        }
    74         argv += optind; /* me -p <arg> */
    75         pid = xatoul_range(p_opt, 1, ULONG_MAX); /* -p <pid> */
     79        /* else "-p <pid>", and *argv == NULL */
     80        pid = xatoul_range(pid_str, 1, ((unsigned)(pid_t)ULONG_MAX) >> 1);
    7681    } else {
    77         argv += optind; /* me -p <arg> */
    78         priority = *argv;
    79     }
    80     if (priority) {
    81         /* from the manpage of sched_getscheduler:
    82            [...] sched_priority can have a value
    83            in the range 0 to 99.
    84            [...] SCHED_OTHER or SCHED_BATCH  must  be  assigned
    85            the  static  priority  0. [...] SCHED_FIFO  or
    86            SCHED_RR can have a static priority in the range 1 to 99.
    87          */
    88         prio = xstrtol_range(priority, 0, policy == SCHED_OTHER
    89                                                      ? 0 : 1, 99);
     82        priority = *argv++;
     83        if (!*argv)
     84            bb_show_usage();
    9085    }
    9186
     87    current_new = "current\0new";
    9288    if (opt & OPT_p) {
    93         int pol = 0;
    94 print_rt_info:
     89        int pol;
     90 print_rt_info:
    9591        pol = sched_getscheduler(pid);
    9692        if (pol < 0)
    97             bb_perror_msg_and_die("failed to %cet pid %d's policy", 'g', pid);
     93            bb_perror_msg_and_die("can't %cet pid %d's policy", 'g', pid);
    9894        printf("pid %d's %s scheduling policy: %s\n",
    99                 pid, state, policies[pol].name);
     95                pid, current_new, policies[pol].name);
    10096        if (sched_getparam(pid, &sp))
    101             bb_perror_msg_and_die("failed to get pid %d's attributes", pid);
     97            bb_perror_msg_and_die("can't get pid %d's attributes", pid);
    10298        printf("pid %d's %s scheduling priority: %d\n",
    103                 pid, state, sp.sched_priority);
    104         if (!*argv) /* no new prio given or we did print already, done. */
     99                pid, current_new, sp.sched_priority);
     100        if (!*argv) {
     101            /* Either it was just "-p <pid>",
     102             * or it was "-p <priority> <pid>" and we came here
     103             * for the second time (see goto below) */
    105104            return EXIT_SUCCESS;
     105        }
     106        *argv = NULL;
     107        current_new += 8;
    106108    }
    107109
    108     sp.sched_priority = prio;
     110    /* from the manpage of sched_getscheduler:
     111    [...] sched_priority can have a value in the range 0 to 99.
     112    [...] SCHED_OTHER or SCHED_BATCH must be assigned static priority 0.
     113    [...] SCHED_FIFO or SCHED_RR can have static priority in 1..99 range.
     114    */
     115    sp.sched_priority = xstrtou_range(priority, 0, policy != SCHED_OTHER ? 1 : 0, 99);
     116
    109117    if (sched_setscheduler(pid, policy, &sp) < 0)
    110         bb_perror_msg_and_die("failed to %cet pid %d's policy", 's', pid);
    111     if (opt & OPT_p) {
    112         state += 8;
    113         ++argv;
     118        bb_perror_msg_and_die("can't %cet pid %d's policy", 's', pid);
     119
     120    if (!argv[0]) /* "-p <priority> <pid> [...]" */
    114121        goto print_rt_info;
    115     }
    116     ++argv;
    117     BB_EXECVP(*argv, argv);
    118     bb_perror_msg_and_die("%s", *argv);
     122
     123    BB_EXECVP_or_die(argv);
    119124}
    120 #undef OPT_p
    121 #undef OPT_r
    122 #undef OPT_f
    123 #undef OPT_o
    124 #undef OPT_m
Note: See TracChangeset for help on using the changeset viewer.