-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubmit.c
141 lines (116 loc) · 3.78 KB
/
submit.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
/***************************************************************************
* Description:
* Submit a job to lpjs_dispatchd.
*
* History:
* Date Name Modification
* 2021-09-28 Jason Bacon Begin
***************************************************************************/
// System headers
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h> // open()
#include <errno.h>
// Addons
#include <munge.h>
#include <xtend/string.h>
#include <xtend/file.h> // xt_rmkdir()
// Project headers
#include "node-list.h"
#include "config.h"
#include "network.h"
#include "misc.h"
#include "lpjs.h"
int main (int argc, char *argv[])
{
int msg_fd,
fd;
char outgoing_msg[LPJS_JOB_MSG_MAX + 3],
*script_name,
*ext,
job_string[JOB_STR_MAX_LEN + 1],
hostname[sysconf(_SC_HOST_NAME_MAX) + 1],
shared_fs_marker[PATH_MAX + 1],
script_text[LPJS_SCRIPT_SIZE_MAX + 1];
ssize_t script_size;
// Terminates process if malloc() fails, no check required
node_list_t *node_list = node_list_new();
job_t *job;
// Shared functions may use lpjs_log
extern FILE *Log_stream;
Log_stream = stderr;
if ( (getuid() == 0) || (geteuid() == 0) )
{
fprintf(stderr, "Cannot run jobs as root.\n");
return EX_USAGE;
}
if ( argc != 2 )
{
fprintf (stderr, "Usage: %s script.lpjs\n", argv[0]);
return EX_USAGE;
}
script_name = argv[1];
if ( ((ext = strchr(script_name, '.')) == NULL) ||
(strcmp(ext, ".lpjs") != 0 ) )
fprintf(stderr, "Warning: Script name %s should end in \".lpjs\"\n",
script_name);
script_size = lpjs_load_script(script_name, script_text,
LPJS_SCRIPT_SIZE_MAX + 1);
// FIXME: Compare shebang line to extension and warn about mismatches
if ( script_size > LPJS_PAYLOAD_MAX )
{
fprintf(stderr, "Error: Script size cannot exceed LPJS_PAYLOAD_MAX = %u\n",
LPJS_PAYLOAD_MAX);
return EX_DATAERR;
}
if ( script_size < LPJS_SCRIPT_MIN_SIZE )
{
lpjs_log("%s(): Error: Script %s < %d chars.\n",
__FUNCTION__, script_name, LPJS_SCRIPT_MIN_SIZE);
return EX_DATAERR;
}
/*
* Create marker file in working directory on submit host.
* Submit scripts can check for this file to determine if they
* are running in a shared directory. If not, they might have to
* use a file transfer tool to send back results.
*/
gethostname(hostname, sysconf(_SC_HOST_NAME_MAX));
lpjs_get_marker_filename(shared_fs_marker, hostname, PATH_MAX + 1);
if ( (fd = open(shared_fs_marker, O_WRONLY|O_CREAT, 0644)) != -1 )
close(fd);
else
{
fprintf(stderr, "Error: Could not create %s: %s\n",
shared_fs_marker, strerror(errno));
return EX_CANTCREAT;
}
// Get hostname of head node
lpjs_load_config(node_list, LPJS_CONFIG_HEAD_ONLY, stderr);
// Terminates process if malloc() fails, no check required
job = job_new();
job_parse_script(job, script_name);
script_name = job_get_script_name(job);
if ( (msg_fd = lpjs_connect_to_dispatchd(node_list)) == -1 )
{
perror("lpjs-submit: Failed to connect to dispatch");
return EX_IOERR;
}
job_print_to_string(job, job_string, LPJS_PAYLOAD_MAX + 1);
snprintf(outgoing_msg, LPJS_JOB_MSG_MAX + 3, "%c%s\n%s",
LPJS_DISPATCHD_REQUEST_SUBMIT, job_string, script_text);
// FIXME: Exiting here causes dispatchd to crash
if ( lpjs_send_munge(msg_fd, outgoing_msg, close) != LPJS_MSG_SENT )
{
perror("lpjs-submit: Failed to send submit request to dispatch");
close(msg_fd);
return EX_IOERR;
}
lpjs_print_response(msg_fd, "lpjs submit");
return EX_OK;
}