~ubuntu-branches/ubuntu/vivid/slurm-llnl/vivid

« back to all changes in this revision

Viewing changes to src/sprio/opts.c

  • Committer: Bazaar Package Importer
  • Author(s): Gennaro Oliva
  • Date: 2009-09-24 23:28:15 UTC
  • mfrom: (1.1.11 upstream) (3.2.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090924232815-enh65jn32q1ebg07
Tags: 2.0.5-1
* New upstream release 
* Changed dependecy from lib-mysqlclient15 to lib-mysqlclient 
* Added Default-Start for runlevel 2 and 4 and $remote_fs requirement in
  init.d scripts (Closes: #541252)
* Postinst checks for wrong runlevels 2 and 4 links
* Upgraded to standard version 3.8.3
* Add lintian overrides for missing slurm-llnl-configurator.html in doc
  base registration
* modified postrm scripts to ignore pkill return value in order to avoid
  postrm failure when no slurm process is running
* Checking for slurmctld.pid before cancelling running and pending
  jobs during package removal 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************\
 
2
 *  opts.c - sprio command line option parsing
 
3
 *****************************************************************************
 
4
 *  Copyright (C) 2009 Lawrence Livermore National Security.
 
5
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 
6
 *  Written by Don Lipari <lipari1@llnl.gov>
 
7
 *  CODE-OCEC-09-009. All rights reserved.
 
8
 *
 
9
 *  This file is part of SLURM, a resource management program.
 
10
 *  For details, see <https://computing.llnl.gov/linux/slurm/>.
 
11
 *  Please also read the included file: DISCLAIMER.
 
12
 *
 
13
 *  SLURM is free software; you can redistribute it and/or modify it under
 
14
 *  the terms of the GNU General Public License as published by the Free
 
15
 *  Software Foundation; either version 2 of the License, or (at your option)
 
16
 *  any later version.
 
17
 *
 
18
 *  In addition, as a special exception, the copyright holders give permission 
 
19
 *  to link the code of portions of this program with the OpenSSL library under 
 
20
 *  certain conditions as described in each individual source file, and 
 
21
 *  distribute linked combinations including the two. You must obey the GNU 
 
22
 *  General Public License in all respects for all of the code used other than 
 
23
 *  OpenSSL. If you modify file(s) with this exception, you may extend this 
 
24
 *  exception to your version of the file(s), but you are not obligated to do 
 
25
 *  so. If you do not wish to do so, delete this exception statement from your
 
26
 *  version.  If you delete this exception statement from all source files in 
 
27
 *  the program, then also delete it here.
 
28
 *
 
29
 *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
 
30
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
31
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
32
 *  details.
 
33
 *
 
34
 *  You should have received a copy of the GNU General Public License along
 
35
 *  with SLURM; if not, write to the Free Software Foundation, Inc.,
 
36
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
 
37
\*****************************************************************************/
 
38
 
 
39
#ifndef _GNU_SOURCE
 
40
#  define _GNU_SOURCE
 
41
#endif
 
42
 
 
43
#ifdef HAVE_CONFIG_H
 
44
#  include "config.h"
 
45
#endif
 
46
 
 
47
#if HAVE_GETOPT_H
 
48
#  include <getopt.h>
 
49
#else
 
50
#  include "src/common/getopt.h"
 
51
#endif
 
52
 
 
53
#include <pwd.h>
 
54
#include <stdlib.h>
 
55
#include <string.h>
 
56
#include <sys/types.h>
 
57
#include <unistd.h>
 
58
 
 
59
#include "src/common/read_config.h"
 
60
#include "src/common/uid.h"
 
61
#include "src/common/xstring.h"
 
62
#include "src/sprio/sprio.h"
 
63
 
 
64
/* getopt_long options, integers but not characters */
 
65
#define OPT_LONG_HELP  0x100
 
66
#define OPT_LONG_USAGE 0x101
 
67
 
 
68
/* FUNCTIONS */
 
69
static List  _build_job_list( char* str );
 
70
static List  _build_user_list( char* str );
 
71
static char *_get_prefix(char *token);
 
72
static void  _help( void );
 
73
static void  _parse_token( char *token, char *field, int *field_size, 
 
74
                           bool *right_justify, char **suffix);
 
75
static void  _print_options( void );
 
76
static void  _print_version( void );
 
77
static void  _usage( void );
 
78
 
 
79
/*
 
80
 * parse_command_line
 
81
 */
 
82
extern void
 
83
parse_command_line( int argc, char* argv[] )
 
84
{
 
85
        int opt_char;
 
86
        int option_index;
 
87
        static struct option long_options[] = {
 
88
                {"noheader",   no_argument,       0, 'h'},
 
89
                {"jobs",       optional_argument, 0, 'j'},
 
90
                {"long",       no_argument,       0, 'l'},
 
91
                {"norm",       no_argument,       0, 'n'},
 
92
                {"format",     required_argument, 0, 'o'},
 
93
                {"user",       required_argument, 0, 'u'},
 
94
                {"users",      required_argument, 0, 'u'},
 
95
                {"verbose",    no_argument,       0, 'v'},
 
96
                {"version",    no_argument,       0, 'V'},
 
97
                {"weights",    no_argument,       0, 'w'},
 
98
                {"help",       no_argument,       0, OPT_LONG_HELP},
 
99
                {"usage",      no_argument,       0, OPT_LONG_USAGE},
 
100
                {NULL,         0,                 0, 0}
 
101
        };
 
102
 
 
103
        while((opt_char = getopt_long(argc, argv, "hj::lno:u:vVw",
 
104
                                      long_options, &option_index)) != -1) {
 
105
                switch (opt_char) {
 
106
                case (int)'?':
 
107
                        fprintf(stderr, "Try \"sprio --help\" "
 
108
                                "for more information\n");
 
109
                        exit(1);
 
110
                case (int)'h':
 
111
                        params.no_header = true;
 
112
                        break;
 
113
                case (int) 'j':
 
114
                        if (optarg) {
 
115
                                params.jobs = xstrdup(optarg);
 
116
                                params.job_list = _build_job_list(params.jobs);
 
117
                        }
 
118
                        params.job_flag = true;
 
119
                        break;
 
120
                case (int) 'l':
 
121
                        params.long_list = true;
 
122
                        break;
 
123
                case (int) 'n':
 
124
                        params.normalized = true;
 
125
                        break;
 
126
                case (int) 'o':
 
127
                        xfree(params.format);
 
128
                        params.format = xstrdup(optarg);
 
129
                        break;
 
130
                case (int) 'u':
 
131
                        xfree(params.users);
 
132
                        params.users = xstrdup(optarg);
 
133
                        params.user_list = _build_user_list(params.users);
 
134
                        break;
 
135
                case (int) 'v':
 
136
                        params.verbose++;
 
137
                        break;
 
138
                case (int) 'V':
 
139
                        _print_version();
 
140
                        exit(0);
 
141
                case (int) 'w':
 
142
                        params.weights = true;
 
143
                        break;
 
144
                case OPT_LONG_HELP:
 
145
                        _help();
 
146
                        exit(0);
 
147
                case OPT_LONG_USAGE:
 
148
                        _usage();
 
149
                        exit(0);
 
150
                }
 
151
        }
 
152
 
 
153
        if (optind < argc) {
 
154
                if (params.job_flag) {
 
155
                        params.jobs = xstrdup(argv[optind++]);
 
156
                        params.job_list = _build_job_list(params.jobs);
 
157
                }
 
158
                if (optind < argc) {
 
159
                        error("Unrecognized option: %s",argv[optind]);
 
160
                        _usage();
 
161
                        exit(1);
 
162
                }
 
163
        }
 
164
 
 
165
        if ( params.verbose )
 
166
                _print_options();
 
167
}
 
168
 
 
169
/*
 
170
 * parse_format - Take the user's format specification and use it to build
 
171
 *      build the format specifications (internalize it to print.c data
 
172
 *      structures)
 
173
 * IN format - user's format specification
 
174
 * RET zero or error code
 
175
 */
 
176
extern int parse_format( char* format )
 
177
{
 
178
        int field_size;
 
179
        bool right_justify;
 
180
        char *prefix = NULL, *suffix = NULL, *token = NULL;
 
181
        char *tmp_char = NULL, *tmp_format = NULL;
 
182
        char field[1];
 
183
 
 
184
        if (format == NULL) {
 
185
                error ("Format option lacks specification.");
 
186
                exit( 1 );
 
187
        }
 
188
 
 
189
        params.format_list = list_create( NULL );
 
190
        if ((prefix = _get_prefix(format))) {
 
191
                job_format_add_prefix( params.format_list, 0, 0, prefix);
 
192
        }
 
193
 
 
194
        field_size = strlen( format );
 
195
        tmp_format = xmalloc( field_size + 1 );
 
196
        strcpy( tmp_format, format );
 
197
 
 
198
        token = strtok_r( tmp_format, "%", &tmp_char);
 
199
        if (token && (format[0] != '%'))        /* toss header */
 
200
                token = strtok_r( NULL, "%", &tmp_char );
 
201
        while (token) {
 
202
                _parse_token( token, field, &field_size, &right_justify,
 
203
                              &suffix);
 
204
                if (field[0] == 'a')
 
205
                        job_format_add_age_priority_normalized(params.format_list,
 
206
                                                               field_size,
 
207
                                                               right_justify,
 
208
                                                               suffix );
 
209
                else if (field[0] == 'A')
 
210
                        job_format_add_age_priority_weighted(params.format_list,
 
211
                                                             field_size,
 
212
                                                             right_justify,
 
213
                                                             suffix );
 
214
                else if (field[0] == 'f')
 
215
                        job_format_add_fs_priority_normalized(params.format_list,
 
216
                                                              field_size,
 
217
                                                              right_justify,
 
218
                                                              suffix );
 
219
                else if (field[0] == 'F')
 
220
                        job_format_add_fs_priority_weighted(params.format_list,
 
221
                                                            field_size,
 
222
                                                            right_justify,
 
223
                                                            suffix );
 
224
                else if (field[0] == 'i')
 
225
                        job_format_add_job_id(params.format_list,
 
226
                                              field_size,
 
227
                                              right_justify,
 
228
                                              suffix );
 
229
                else if (field[0] == 'j')
 
230
                        job_format_add_js_priority_normalized(params.format_list,
 
231
                                                              field_size,
 
232
                                                              right_justify,
 
233
                                                              suffix );
 
234
                else if (field[0] == 'J')
 
235
                        job_format_add_js_priority_weighted(params.format_list,
 
236
                                                            field_size,
 
237
                                                            right_justify,
 
238
                                                            suffix );
 
239
                else if (field[0] == 'N')
 
240
                        job_format_add_job_nice(params.format_list,
 
241
                                                field_size,
 
242
                                                right_justify,
 
243
                                                suffix );
 
244
                else if (field[0] == 'p')
 
245
                        job_format_add_part_priority_normalized(params.format_list,
 
246
                                                                field_size,
 
247
                                                                right_justify,
 
248
                                                                suffix );
 
249
                else if (field[0] == 'P')
 
250
                        job_format_add_part_priority_weighted(params.format_list,
 
251
                                                              field_size,
 
252
                                                              right_justify,
 
253
                                                              suffix );
 
254
                else if (field[0] == 'q')
 
255
                        job_format_add_qos_priority_normalized(params.format_list,
 
256
                                                               field_size,
 
257
                                                               right_justify,
 
258
                                                               suffix );
 
259
                else if (field[0] == 'Q')
 
260
                        job_format_add_qos_priority_weighted(params.format_list,
 
261
                                                             field_size,
 
262
                                                             right_justify,
 
263
                                                             suffix );
 
264
                else if (field[0] == 'u')
 
265
                        job_format_add_user_name(params.format_list,
 
266
                                                 field_size,
 
267
                                                 right_justify,
 
268
                                                 suffix );
 
269
                else if (field[0] == 'y')
 
270
                        job_format_add_job_priority_normalized(params.format_list,
 
271
                                                               field_size,
 
272
                                                               right_justify,
 
273
                                                               suffix );
 
274
                else if (field[0] == 'Y')
 
275
                        job_format_add_job_priority_weighted(params.format_list,
 
276
                                                             field_size,
 
277
                                                             right_justify,
 
278
                                                             suffix );
 
279
                else
 
280
                        error( "Invalid job format specification: %c",
 
281
                               field[0] );
 
282
 
 
283
                token = strtok_r( NULL, "%", &tmp_char);
 
284
        }
 
285
 
 
286
        xfree( tmp_format );
 
287
        return SLURM_SUCCESS;
 
288
}
 
289
 
 
290
/* Take a format specification and copy out it's prefix
 
291
 * IN/OUT token - input specification, everything before "%" is removed
 
292
 * RET - everything before "%" in the token
 
293
 */
 
294
static char *
 
295
_get_prefix( char *token )
 
296
{
 
297
        char *pos, *prefix;
 
298
 
 
299
        if (token == NULL)
 
300
                return NULL;
 
301
 
 
302
        pos = strchr(token, (int) '%');
 
303
        if (pos == NULL)        /* everything is prefix */
 
304
                return xstrdup(token);
 
305
        if (pos == token)       /* no prefix */
 
306
                return NULL;
 
307
 
 
308
        pos[0] = '\0';          /* some prefix */
 
309
        prefix = xstrdup(token);
 
310
        pos[0] = '%';
 
311
        memmove(token, pos, (strlen(pos)+1));
 
312
        return prefix;
 
313
}
 
314
 
 
315
/* Take a format specification and break it into its components
 
316
 * IN token - input specification without leading "%", eg. ".5u"
 
317
 * OUT field - the letter code for the data type
 
318
 * OUT field_size - byte count
 
319
 * OUT right_justify - true of field to be right justified
 
320
 * OUT suffix - string containing everthing after the field specification
 
321
 */
 
322
static void
 
323
_parse_token( char *token, char *field, int *field_size, bool *right_justify, 
 
324
              char **suffix)
 
325
{
 
326
        int i = 0;
 
327
 
 
328
        assert (token != NULL);
 
329
 
 
330
        if (token[i] == '.') {
 
331
                *right_justify = true;
 
332
                i++;
 
333
        } else
 
334
                *right_justify = false;
 
335
 
 
336
        *field_size = 0;
 
337
        while ((token[i] >= '0') && (token[i] <= '9'))
 
338
                *field_size = (*field_size * 10) + (token[i++] - '0');
 
339
 
 
340
        field[0] = token[i++];
 
341
 
 
342
        *suffix = xstrdup(&token[i]);
 
343
}
 
344
 
 
345
/* print the parameters specified */
 
346
static void
 
347
_print_options()
 
348
{
 
349
        ListIterator iterator;
 
350
        int i;
 
351
        uint32_t *job_id;
 
352
        uint32_t *user;
 
353
 
 
354
        printf( "-----------------------------\n" );
 
355
        printf( "format     = %s\n", params.format );
 
356
        printf( "job_flag   = %d\n", params.job_flag );
 
357
        printf( "jobs       = %s\n", params.jobs );
 
358
        printf( "users      = %s\n", params.users );
 
359
        printf( "verbose    = %d\n", params.verbose );
 
360
 
 
361
        if ((params.verbose > 1) && params.job_list) {
 
362
                i = 0;
 
363
                iterator = list_iterator_create( params.job_list );
 
364
                while ( (job_id = list_next( iterator )) ) {
 
365
                        printf( "job_list[%d] = %u\n", i++, *job_id);
 
366
                }
 
367
                list_iterator_destroy( iterator );
 
368
        }
 
369
 
 
370
        if ((params.verbose > 1) && params.user_list) {
 
371
                i = 0;
 
372
                iterator = list_iterator_create( params.user_list );
 
373
                while ( (user = list_next( iterator )) ) {
 
374
                        printf( "user_list[%d] = %u\n", i++, *user);
 
375
                }
 
376
                list_iterator_destroy( iterator );
 
377
        }
 
378
 
 
379
        printf( "-----------------------------\n\n\n" );
 
380
} ;
 
381
 
 
382
 
 
383
/*
 
384
 * _build_job_list- build a list of job_ids
 
385
 * IN str - comma separated list of job_ids
 
386
 * RET List of job_ids (uint32_t)
 
387
 */
 
388
static List
 
389
_build_job_list( char* str )
 
390
{
 
391
        List my_list;
 
392
        char *job = NULL, *tmp_char = NULL, *my_job_list = NULL;
 
393
        int i;
 
394
        uint32_t *job_id = NULL;
 
395
 
 
396
        if ( str == NULL)
 
397
                return NULL;
 
398
        my_list = list_create( NULL );
 
399
        my_job_list = xstrdup( str );
 
400
        job = strtok_r( my_job_list, ",", &tmp_char );
 
401
        while (job)
 
402
        {
 
403
                i = strtol( job, (char **) NULL, 10 );
 
404
                if (i <= 0) {
 
405
                        error( "Invalid job id: %s", job );
 
406
                        exit( 1 );
 
407
                }
 
408
                job_id = xmalloc( sizeof( uint32_t ) );
 
409
                *job_id = (uint32_t) i;
 
410
                list_append( my_list, job_id );
 
411
                job = strtok_r (NULL, ",", &tmp_char);
 
412
        }
 
413
        return my_list;
 
414
}
 
415
 
 
416
/*
 
417
 * _build_user_list- build a list of UIDs
 
418
 * IN str - comma separated list of user names
 
419
 * RET List of UIDs (uint32_t)
 
420
 */
 
421
static List
 
422
_build_user_list( char* str )
 
423
{
 
424
        List my_list;
 
425
        char *user = NULL;
 
426
        char *tmp_char = NULL, *my_user_list = NULL;
 
427
        uid_t uid = (uid_t) 0;
 
428
 
 
429
        if ( str == NULL)
 
430
                return NULL;
 
431
 
 
432
        my_list = list_create( NULL );
 
433
        my_user_list = xstrdup( str );
 
434
        user = strtok_r( my_user_list, ",", &tmp_char );
 
435
        while (user) {
 
436
                if (uid_from_string (user, &uid) < 0) {
 
437
                        error( "Invalid user: %s\n", user);
 
438
                } else {
 
439
                        uint32_t *u_tmp = xmalloc( sizeof( uint32_t ));
 
440
                        *u_tmp = (uint32_t) uid;
 
441
                        list_append( my_list, u_tmp );
 
442
                }
 
443
                user = strtok_r (NULL, ",", &tmp_char);
 
444
        }
 
445
        return my_list;
 
446
}
 
447
 
 
448
static void _print_version(void)
 
449
{
 
450
        printf("%s %s\n", PACKAGE, SLURM_VERSION);
 
451
}
 
452
 
 
453
static void _usage(void)
 
454
{
 
455
        printf("Usage: sprio [-j jid[s]] [-u user_name[s]] [-o format] [--usage] [-hlnvVw]\n");
 
456
}
 
457
 
 
458
static void _help(void)
 
459
{
 
460
        printf("\
 
461
Usage: sprio [OPTIONS]\n\
 
462
  -h, --noheader                  no headers on output\n\
 
463
  -j, --jobs                      comma separated list of jobs\n\
 
464
                                  to view, default is all\n\
 
465
  -l, --long                      long report\n\
 
466
  -n, --norm                      display normalized values\n\
 
467
  -o, --format=format             format specification\n\
 
468
  -u, --user=user_name            comma separated list of users to view\n\
 
469
  -v, --verbose                   verbosity level\n\
 
470
  -V, --version                   output version information and exit\n\
 
471
  -w, --weights                   show the weights for each priority factor\n\
 
472
\nHelp options:\n\
 
473
  --help                          show this help message\n\
 
474
  --usage                         display a brief summary of sprio options\n");
 
475
}