]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/prlimit.c
misc: consolidate smartcols error messages
[thirdparty/util-linux.git] / sys-utils / prlimit.c
index 4e0cc5eda0086a0a9876452ef60be5b3588ed2ce..a62d457feb8875ca6ff2f0d31d89c6a5ca0baaaa 100644 (file)
@@ -73,23 +73,25 @@ struct prlimit_desc {
 static struct prlimit_desc prlimit_desc[] =
 {
        [AS]         = { "AS",         N_("address space limit"),                N_("bytes"),     RLIMIT_AS },
-       [CORE]       = { "CORE",       N_("max core file size"),                 N_("blocks"),    RLIMIT_CORE },
+       [CORE]       = { "CORE",       N_("max core file size"),                 N_("bytes"),     RLIMIT_CORE },
        [CPU]        = { "CPU",        N_("CPU time"),                           N_("seconds"),   RLIMIT_CPU },
        [DATA]       = { "DATA",       N_("max data size"),                      N_("bytes"),     RLIMIT_DATA },
-       [FSIZE]      = { "FSIZE",      N_("max file size"),                      N_("blocks"),    RLIMIT_FSIZE },
-       [LOCKS]      = { "LOCKS",      N_("max number of file locks held"),      NULL,            RLIMIT_LOCKS },
+       [FSIZE]      = { "FSIZE",      N_("max file size"),                      N_("bytes"),     RLIMIT_FSIZE },
+       [LOCKS]      = { "LOCKS",      N_("max number of file locks held"),      N_("locks"),     RLIMIT_LOCKS },
        [MEMLOCK]    = { "MEMLOCK",    N_("max locked-in-memory address space"), N_("bytes"),     RLIMIT_MEMLOCK },
        [MSGQUEUE]   = { "MSGQUEUE",   N_("max bytes in POSIX mqueues"),         N_("bytes"),     RLIMIT_MSGQUEUE },
        [NICE]       = { "NICE",       N_("max nice prio allowed to raise"),     NULL,            RLIMIT_NICE },
-       [NOFILE]     = { "NOFILE",     N_("max number of open files"),           NULL,            RLIMIT_NOFILE },
-       [NPROC]      = { "NPROC",      N_("max number of processes"),            NULL,            RLIMIT_NPROC },
-       [RSS]        = { "RSS",        N_("max resident set size"),              N_("pages"),     RLIMIT_RSS },
+       [NOFILE]     = { "NOFILE",     N_("max number of open files"),           N_("files"),     RLIMIT_NOFILE },
+       [NPROC]      = { "NPROC",      N_("max number of processes"),            N_("processes"), RLIMIT_NPROC },
+       [RSS]        = { "RSS",        N_("max resident set size"),              N_("bytes"),     RLIMIT_RSS },
        [RTPRIO]     = { "RTPRIO",     N_("max real-time priority"),             NULL,            RLIMIT_RTPRIO },
        [RTTIME]     = { "RTTIME",     N_("timeout for real-time tasks"),        N_("microsecs"), RLIMIT_RTTIME },
-       [SIGPENDING] = { "SIGPENDING", N_("max number of pending signals"),      NULL,            RLIMIT_SIGPENDING },
+       [SIGPENDING] = { "SIGPENDING", N_("max number of pending signals"),      N_("signals"),   RLIMIT_SIGPENDING },
        [STACK]      = { "STACK",      N_("max stack size"),                     N_("bytes"),     RLIMIT_STACK }
 };
 
+#define MAX_RESOURCES ARRAY_SIZE(prlimit_desc)
+
 struct prlimit {
        struct list_head lims;
 
@@ -117,7 +119,7 @@ struct colinfo {
 };
 
 /* columns descriptions */
-struct colinfo infos[] = {
+static struct colinfo infos[] = {
        [COL_RES]     = { "RESOURCE",    0.25, SCOLS_FL_TRUNC, N_("resource name") },
        [COL_HELP]    = { "DESCRIPTION", 0.1,  SCOLS_FL_TRUNC, N_("resource description")},
        [COL_SOFT]    = { "SOFT",        0.1,  SCOLS_FL_RIGHT, N_("soft limit")},
@@ -125,8 +127,10 @@ struct colinfo infos[] = {
        [COL_UNITS]   = { "UNITS",       0.1,  SCOLS_FL_TRUNC, N_("units")},
 };
 
-#define NCOLS ARRAY_SIZE(infos)
-#define MAX_RESOURCES ARRAY_SIZE(prlimit_desc)
+static int columns[ARRAY_SIZE(infos) * 2];
+static int ncolumns;
+
+
 
 #define INFINITY_STR   "unlimited"
 #define INFINITY_STRLEN        (sizeof(INFINITY_STR) - 1)
@@ -134,8 +138,6 @@ struct colinfo infos[] = {
 #define PRLIMIT_SOFT   (1 << 1)
 #define PRLIMIT_HARD   (1 << 2)
 
-/* array with IDs of enabled columns */
-static int columns[NCOLS], ncolumns;
 static pid_t pid; /* calling process (default) */
 static int verbose;
 
@@ -160,6 +162,9 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
        fprintf(out,
                _(" %s [options] COMMAND\n"), program_invocation_short_name);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Show or change the resource limits of a process.\n"), out);
+
        fputs(_("\nGeneral Options:\n"), out);
        fputs(_(" -p, --pid <pid>        process id\n"
                " -o, --output <list>    define which output columns to use\n"
@@ -190,7 +195,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
        fputs(_("\nAvailable columns (for --output):\n"), out);
 
-       for (i = 0; i < NCOLS; i++)
+       for (i = 0; i < ARRAY_SIZE(infos); i++)
                fprintf(out, " %11s  %s\n", infos[i].name, _(infos[i].help));
 
        fprintf(out, USAGE_MAN_TAIL("prlimit(1)"));
@@ -200,9 +205,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
 static inline int get_column_id(int num)
 {
-       assert(ARRAY_SIZE(columns) == NCOLS);
        assert(num < ncolumns);
-       assert(columns[num] < (int) NCOLS);
+       assert(columns[num] < (int) ARRAY_SIZE(infos));
 
        return columns[num];
 }
@@ -221,10 +225,8 @@ static void add_scols_line(struct libscols_table *table, struct prlimit *l)
        assert(l);
 
        line = scols_table_new_line(table, NULL);
-       if (!line) {
-               warn(_("failed to add line to output"));
-               return;
-       }
+       if (!line)
+               err(EXIT_FAILURE, _("failed to allocate output line"));
 
        for (i = 0; i < ncolumns; i++) {
                char *str = NULL;
@@ -255,8 +257,8 @@ static void add_scols_line(struct libscols_table *table, struct prlimit *l)
                        break;
                }
 
-               if (str)
-                       scols_line_set_data(line, i, str);
+               if (str && scols_line_refer_data(line, i, str))
+                       err(EXIT_FAILURE, _("failed to add output data"));
        }
 }
 
@@ -266,7 +268,7 @@ static int column_name_to_id(const char *name, size_t namesz)
 
        assert(name);
 
-       for (i = 0; i < NCOLS; i++) {
+       for (i = 0; i < ARRAY_SIZE(infos); i++) {
                const char *cn = infos[i].name;
 
                if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
@@ -290,24 +292,20 @@ static int show_limits(struct list_head *lims)
        struct list_head *p, *pnext;
        struct libscols_table *table;
 
-       table = scols_new_table(NULL);
-       if (!table) {
-               warn(_("failed to initialize output table"));
-               return -1;
-       }
-       scols_table_set_raw(table, raw);
-       scols_table_set_no_headings(table, no_headings);
+       table = scols_new_table();
+       if (!table)
+               err(EXIT_FAILURE, _("failed to allocate output table"));
+
+       scols_table_enable_raw(table, raw);
+       scols_table_enable_noheadings(table, no_headings);
 
        for (i = 0; i < ncolumns; i++) {
                struct colinfo *col = get_column_info(i);
 
-               if (!scols_table_new_column(table, col->name, col->whint, col->flags)) {
-                       warnx(_("failed to initialize output column"));
-                       goto done;
-               }
+               if (!scols_table_new_column(table, col->name, col->whint, col->flags))
+                       err(EXIT_FAILURE, _("failed to allocate output column"));
        }
 
-
        list_for_each_safe(p, pnext, lims) {
                struct prlimit *lim = list_entry(p, struct prlimit, lims);
 
@@ -316,7 +314,6 @@ static int show_limits(struct list_head *lims)
        }
 
        scols_print_table(table);
-done:
        scols_unref_table(table);
        return 0;
 }
@@ -362,7 +359,8 @@ static void do_prlimit(struct list_head *lims)
                        old = &lim->rlim;
 
                if (verbose && new) {
-                       printf(_("New %s limit: "), lim->desc->name);
+                       printf(_("New %s limit for pid %d: "), lim->desc->name,
+                               pid ? pid : getpid());
                        if (new->rlim_cur == RLIM_INFINITY)
                                printf("<%s", _("unlimited"));
                        else
@@ -608,9 +606,8 @@ int main(int argc, char **argv)
                case RAW_OPTION:
                        raw = 1;
                        break;
-
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
        if (argc > optind && pid)
@@ -624,6 +621,8 @@ int main(int argc, char **argv)
                columns[ncolumns++] = COL_UNITS;
        }
 
+       scols_init_debug(0);
+
        if (list_empty(&lims)) {
                /* default is to print all resources */
                size_t n;