-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.c
185 lines (169 loc) · 4.8 KB
/
config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <limits.h>
#include <xtend/dsv.h>
#include <xtend/string.h> // xt_strtrim()
#include "node-list.h"
#include "config.h"
#include "misc.h"
#include "lpjs.h"
/***************************************************************************
* Description:
* Load LPJS config file, which contains the names of the head node
* and compute nodes.
*
* History:
* Date Name Modification
* 2021-09-23 Jason Bacon Begin
***************************************************************************/
/*
* Keep error_stream separate from Log_stream, as this function is
* called by both daemons and user commands. error_stream should be
* stderr for non-daemons, and Log_stream for daemons.
*/
int lpjs_load_config(node_list_t *node_list, int flags, FILE *error_stream)
{
FILE *config_fp;
char field[LPJS_FIELD_MAX + 1];
char config_file[PATH_MAX + 1];
int delim;
size_t len;
snprintf(config_file, PATH_MAX + 1, "%s/etc/lpjs/config", PREFIX);
if ( (config_fp = fopen(config_file, "r")) == NULL )
{
fprintf(error_stream, "Cannot open %s.\n", config_file);
exit(EX_NOINPUT);
}
node_list_init(node_list);
while ( ((delim = xt_dsv_read_field(config_fp, field, LPJS_FIELD_MAX + 1,
" \t", &len)) != EOF) )
{
// FIXME: Be more robust about what's a comment
if ( field[0] == '#' )
xt_dsv_skip_rest_of_line(config_fp); // Comment
else if ( strcmp(field, "head") == 0 )
{
if ( xt_dsv_read_field(config_fp, field, LPJS_FIELD_MAX + 1, " \t", &len)
!= '\n' )
{
fprintf(error_stream, "load_config(): 'head' must be followed by a single hostname.\n");
exit(EX_DATAERR);
}
// FIXME: Check malloc() and sanity
node_list_set_head_node(node_list, strdup(field));
// printf("Head node = %s\n", field);
}
else if ( strcmp(field, "compute") == 0 )
{
/*
* Only dispatch daemon needs to query compute nodes for specs
* Most other programs just need the head node hostname
*/
if ( delim != EOF )
{
if ( flags == LPJS_CONFIG_ALL )
lpjs_load_compute_config(node_list, config_fp, config_file);
else
xt_dsv_skip_rest_of_line(config_fp);
}
}
else
{
fprintf(error_stream, "Skipping unknown tag %s...", field);
xt_dsv_skip_rest_of_line(config_fp);
}
}
if ( flags == LPJS_CONFIG_ALL )
fprintf(error_stream, "%u compute nodes found.\n",
node_list_get_compute_node_count(node_list));
fclose(config_fp);
return delim;
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Name:
* -
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-02-24 Jason Bacon Begin
***************************************************************************/
int lpjs_load_compute_config(node_list_t *node_list, FILE *input_stream,
const char *conf_file)
{
int delim, processors;
char field[LPJS_FIELD_MAX + 1],
hostname[LPJS_FIELD_MAX + 1],
*end;
size_t len, pmem;
node_t *node;
pmem = processors = 0;
while ( ((delim = xt_dsv_read_field(input_stream, field,
LPJS_FIELD_MAX + 1,
" \t", &len)) != EOF) )
{
xt_strtrim(field, " ");
lpjs_debug("%s(): field = %s, delim = %d\n", __FUNCTION__, field, delim);
if ( memcmp(field, "pmem=", 5) ==0 )
{
pmem = lpjs_parse_phys_MiB(field + 5);
if ( pmem == 0 )
{
lpjs_log("%s(): pmem specifier '%s':\n", __FUNCTION__, field);
lpjs_log("%s(): Requires a decimal number followed by MB, MiB, GB, or GiB.\n", __FUNCTION__);
exit(EX_DATAERR);
}
lpjs_debug("%s(): pmem override = %zu\n", __FUNCTION__, pmem);
}
else if ( memcmp(field, "processors=", 11) == 0 )
{
processors = strtoul(field + 11, &end, 10);
if ( *end != '\0' )
{
lpjs_log("%s(): Invalid proc count: %s\n", __FUNCTION__, field);
exit(EX_DATAERR);
}
lpjs_debug("%s(): processors override = %zu\n", __FUNCTION__, pmem);
}
else
strlcpy(hostname, field, LPJS_FIELD_MAX + 1);
if ( delim == '\n' )
break;
}
if ( delim == EOF )
{
lpjs_log("%s(): Error: Unexpected EOF reading %s.\n",
__FUNCTION__, conf_file);
exit(EX_DATAERR);
}
// Terminates process if malloc() fails, no check required
node = node_new();
if ( pmem != 0 )
node_set_phys_MiB(node, pmem);
if ( processors != 0 )
node_set_processors(node, processors);
node_set_hostname(node, strdup(hostname));
node_list_add_compute_node(node_list, node);
return 0; // NL_OK?
}