-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc.c
410 lines (343 loc) · 8.6 KB
/
misc.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sysexits.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h> // PATH_MAX
#include <fcntl.h> // open()
#include <xtend/file.h> // xt_rmkdir()
#include "lpjs.h"
#include "misc.h"
#include "node-list.h"
#include "network.h"
/*
* Avoid globals like the plague, but make an exception here so
* signal handlers can log messages. All commands might use
* lpjs_log(), so Log_stream must be always be initialized in main().
* Default linkage on Apple clang14 causes extern variables to be
* undefined, unless they are initialized.
*/
FILE *Log_stream = NULL;
FILE *Job_history_stream = NULL;
node_list_t *Node_list = NULL;
bool Debug = true; // FIXME: Control with --debug flag
char Pid_path[PATH_MAX + 1] = "";
/***************************************************************************
* Description:
* Log messages to stream of choice, usually either stderr if running
* as a foreground process, or PREFIX/var/log/lpjs by default
* if daemonized.
*
* History:
* Date Name Modification
* 2021-09-28 Jason Bacon Begin
***************************************************************************/
int lpjs_log(const char *format, ...)
{
int status;
va_list ap;
// Code duplicated in lpjs_debug(), but not worth factoring out
va_start(ap, format);
fprintf(Log_stream, "%s ", xt_str_localtime("%m-%d %H:%M:%S"));
// FIXME: Add time stamp?
status = vfprintf(Log_stream, format, ap);
// Commit immediately so last message in the log is not
// misleading in the event of a crash
fflush(Log_stream);
fsync(fileno(Log_stream));
va_end(ap);
return status;
}
int lpjs_debug(const char *format, ...)
{
int status;
va_list ap;
if ( Debug )
{
// Code duplicated in lpjs_log), but not worth factoring out
va_start(ap, format);
fprintf(Log_stream, "%s ", xt_str_localtime("%m-%d %H:%M:%S"));
// FIXME: Add time stamp?
status = vfprintf(Log_stream, format, ap);
// Commit immediately so last message in the log is not
// misleading in the event of a crash
fflush(Log_stream);
fsync(fileno(Log_stream));
va_end(ap);
return status;
}
else
return 0;
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-01-27 Jason Bacon Begin
***************************************************************************/
FILE *lpjs_log_output(const char *pathname, const char *mode)
{
FILE *fp;
char *log_dir,
*end;
// FIXME: Prevent unchecked log growth
// FIXME: Should we even be doing this here, or rely on the package
// manager and compd to create directory structure?
// FIXME: Error out if strdup() fails
log_dir = strdup(pathname);
if ( (end = strrchr(log_dir, '/')) != NULL )
{
*end = '\0';
fprintf(stderr, "Creating %s...\n", log_dir);
if ( xt_rmkdir(log_dir, 0755) != 0 )
{
fprintf(stderr, "%s(): Error: Cannot create %s: %s\n", __FUNCTION__,
log_dir, strerror(errno));
return NULL;
}
}
free(log_dir);
fp = fopen(pathname, mode);
if ( fp == NULL )
{
fprintf(stderr, "Cannot open %s: %s\n", pathname, strerror(errno));
return NULL;
}
return fp;
}
int xt_create_pid_file(const char *pid_path, FILE *log_stream)
{
struct stat st;
FILE *fp;
if ( stat(pid_path, &st) == 0 )
{
fprintf(log_stream, "%s: %s already exists.\n", __FUNCTION__,
pid_path);
return EX_CANTCREAT;
}
if ( (fp = fopen(pid_path, "w")) == NULL )
{
fprintf(log_stream, "%s: fopen() failed on %s: %s.\n", __FUNCTION__,
pid_path, strerror(errno));
return EX_CANTCREAT;
}
if ( fprintf(fp, "%d\n", getpid()) < 0 )
{
fprintf(log_stream, "%s: fprintf() failed on %s: %s.\n", __FUNCTION__,
pid_path, strerror(errno));
return EX_IOERR;
}
fclose(fp);
return EX_OK;
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-02-21 Jason Bacon Begin
***************************************************************************/
#define TIME_STR_MAX 32
char *xt_str_localtime(const char *format)
{
time_t time_sec;
struct tm *tm;
static char str[TIME_STR_MAX + 1];
time(&time_sec);
tm = localtime(&time_sec);
strftime(str, TIME_STR_MAX + 1, format, tm);
return str;
}
/***************************************************************************
* 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-22 Jason Bacon Begin
***************************************************************************/
const char *xt_basename(const char *restrict str)
{
char *p;
if ( (p = strrchr(str, '/')) == NULL )
return str; // No '/' in path, path is a basename
else
return p + 1; // Next char after last '/'
}
ssize_t lpjs_load_script(const char *script_path,
char *script_buff, size_t buff_size)
{
ssize_t bytes;
int fd;
extern FILE *Log_stream;
if ( (fd = open(script_path, O_RDONLY)) == -1 )
{
lpjs_log("%s(): Error: Failed to open %s: %s\n", __FUNCTION__,
script_path, strerror(errno));
return -1;
}
bytes = read(fd, script_buff, buff_size + 1);
if ( bytes == buff_size + 1 )
{
lpjs_log("%s(): Error: Script exceeds %zd. Reduce script size or increase script_size_max.\n",
__FUNCTION__, buff_size);
close(fd);
return -1;
}
close(fd);
script_buff[bytes] = '\0';
return bytes;
}
/***************************************************************************
* Use auto-c2man to generate a man page from this comment
*
* Library:
* #include <>
* -l
*
* Description:
*
* Arguments:
*
* Returns:
*
* Examples:
*
* Files:
*
* Environment
*
* See also:
*
* History:
* Date Name Modification
* 2024-04-23 Jason Bacon Begin
***************************************************************************/
char *lpjs_get_marker_filename(char shared_fs_marker[], const char *hostname,
size_t array_size)
{
// FIXME: Check for errors
snprintf(shared_fs_marker, array_size + 1,
".lpjs-%s-shared-fs-marker", hostname);
return shared_fs_marker;
}
/***************************************************************************
* Description:
*
*
* History:
* Date Name Modification
* 2024-05-10 Jason Bacon Begin
***************************************************************************/
void lpjs_job_log_dir(const char *log_parent, unsigned long job_id,
char *log_dir, size_t array_size)
{
snprintf(log_dir, array_size, "%s/Job-%lu",
log_parent, job_id);
}
/***************************************************************************
* 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
* 2025-01-02 Jason Bacon Begin
***************************************************************************/
size_t lpjs_parse_phys_MiB(char *str)
{
size_t mem;
char *end;
mem = strtoul(str, &end, 10);
// Convert to MiB
// Careful with the integer arithmetic, to avoid overflows and 0 results
if ( strcmp(end, "MB") == 0 )
mem = mem * LPJS_MB / LPJS_MiB;
else if ( strcmp(end, "MiB") == 0 )
;
else if ( strcmp(end, "GB") == 0 )
mem = mem * LPJS_GB / LPJS_MiB;
else if ( strcmp(end, "GiB") == 0 )
mem = mem * LPJS_GiB / LPJS_MiB;
else
mem = 0; // Invalid input
return mem;
}