-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcancel.c
112 lines (87 loc) · 2.74 KB
/
cancel.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
/***************************************************************************
* Description:
* Cancel a pending or running job
*
* History:
* Date Name Modification
* 2024-05-03 Jason Bacon Begin
***************************************************************************/
// System headers
#include <stdio.h>
#include <sysexits.h>
#include <ctype.h>
#include <stdlib.h>
// Addons
#include <munge.h>
// Project headers
#include "config.h"
#include "network.h"
#include "misc.h"
#include "lpjs.h"
#include "cancel-protos.h"
int main (int argc, char *argv[])
{
int arg;
// Terminates process if malloc() fails, no check required
node_list_t *node_list = node_list_new();
unsigned long jobid, first_jobid, last_jobid;
char *end;
// Shared functions may use lpjs_log
extern FILE *Log_stream;
Log_stream = stderr;
if ( argc < 2 )
return usage(argv);
// Get hostname of head node
lpjs_load_config(node_list, LPJS_CONFIG_HEAD_ONLY, stderr);
for (arg=1; arg<argc; ++arg)
{
if ( !isdigit(argv[arg][0]) )
return usage(argv);
first_jobid = strtol(argv[arg], &end, 10);
if ( *end == '-' )
{
last_jobid = strtol(end + 1, &end, 10);
if ( *end != '\0' )
return usage(argv);
for (jobid = first_jobid; jobid <= last_jobid; ++jobid)
lpjs_request_cancel(node_list, jobid);
}
else
lpjs_request_cancel(node_list, first_jobid);
}
return EX_OK;
}
int lpjs_request_cancel(node_list_t *node_list, unsigned long jobid)
{
int msg_fd;
char outgoing_msg[LPJS_MSG_LEN_MAX + 1];
/*
* Connect to dispatchd before processing arguments, so that
* other events are on hold while we cancel jobs. We don't want
* anything to move from pending to running during a cancel operation.
*/
if ( (msg_fd = lpjs_connect_to_dispatchd(node_list)) == -1 )
{
perror("lpjs-cancel: Failed to connect to dispatch");
return EX_IOERR;
}
snprintf(outgoing_msg, LPJS_MSG_LEN_MAX + 1, "%c%lu",
LPJS_DISPATCHD_REQUEST_CANCEL, jobid);
lpjs_log("%s(): Canceling job %lu\n", __FUNCTION__, jobid);
// FIXME: Exiting here causes dispatchd to crash
if ( lpjs_send_munge(msg_fd, outgoing_msg, close) != LPJS_MSG_SENT )
{
perror("lpjs-cancel: Failed to send cancel request to dispatch");
close(msg_fd);
return EX_IOERR;
}
lpjs_print_response(msg_fd, "lpjs cancel");
close(msg_fd);
return 0; // FIXME: Define return codes
}
int usage(char *argv[])
{
fprintf(stderr, "Usage: %s jobid[-jobid] [jobid[-jobid] ...]\n", argv[0]);
fprintf(stderr, "Note: No whitespace between jobids in a range.\n");
return EX_USAGE;
}