32 #include <sys/socket.h> 33 #include <sys/types.h> 34 #include <netinet/in.h> 45 #define vprintf(...) if(verbose) printf(__VA_ARGS__); 68 struct addrinfo hints;
69 struct addrinfo* servs, * curr = NULL;
71 memset(&hints, 0,
sizeof(hints));
72 hints.ai_family = AF_UNSPEC;
73 hints.ai_socktype = SOCK_STREAM;
74 if(getaddrinfo(host, port, &hints, &servs) == -1)
76 fprintf(stderr,
"ERROR: %s.%d: unable to connect to %s port: %s\n",
77 __FILE__, __LINE__, host, port);
78 fprintf(stderr,
"ERROR: errno: %s\n", strerror(errno));
82 for(curr = servs; curr != NULL; curr = curr->ai_next)
84 if((fd = socket(curr->ai_family, hints.ai_socktype, curr->ai_protocol)) < 0)
87 if(connect(fd, curr->ai_addr, curr->ai_addrlen) == -1)
95 fprintf(stderr,
"ERROR: %s.%d: unable to connect to %s port: %s\n",
96 __FILE__, __LINE__, host, port);
122 memset(buffer,
'\0', max);
128 bytes = read(s, buffer + bytes, max - bytes);
132 printf(
"ERROR: connection to scheduler closed\nERROR: closing cli\n");
136 bytes = strlen(buffer);
137 }
while(!closing && buffer[bytes - 1] !=
'\n');
140 for(poss = strtok(buffer,
"\n"); !closing && poss != NULL;
141 poss = strtok(NULL,
"\n"))
143 if(strncmp(poss,
"received", 8) == 0)
146 printf(
"Command received\n");
148 else if(strncmp(poss,
"CLOSE", 5) == 0)
152 else if(strcmp(poss,
"end") != 0)
154 printf(
"%s\n", poss);
175 printf(
"FOSSology scheduler command line interface\n");
176 printf(
"+-----------------------------------------------------------------------------+\n");
177 printf(
"|%*s: EFFECT |\n", P_WIDTH,
"CMD [optional] <required>");
178 printf(
"+-----------------------------------------------------------------------------+\n");
179 printf(
"|%*s: prints this usage statement |\n", P_WIDTH,
"help");
180 printf(
"|%*s: close the connection and exit cli |\n", P_WIDTH,
"close");
181 printf(
"|%*s: shutdown will wait for agents be stopping |\n", P_WIDTH,
"stop");
182 printf(
"|%*s: shutdown will shutdown immediately |\n", P_WIDTH,
"die");
183 printf(
"|%*s: get load information for host machines |\n", P_WIDTH,
"load");
184 printf(
"|%*s: kills a currently running job (ungraceful) |\n", P_WIDTH,
"kill <jq_pk> <\"message\">");
185 printf(
"|%*s: pauses a job indefinitely |\n", P_WIDTH,
"pause <jq_pk>");
186 printf(
"|%*s: reload the configuration information |\n", P_WIDTH,
"reload");
187 printf(
"|%*s: prints a list of valid agents |\n", P_WIDTH,
"agents");
188 printf(
"|%*s: scheduler responds with status information |\n", P_WIDTH,
"status [jq_pk]");
189 printf(
"|%*s: restart a paused job |\n", P_WIDTH,
"restart <jq_pk>");
190 printf(
"|%*s: query/change the scheduler/job verbosity |\n", P_WIDTH,
"verbose [jq_pk] [level]");
191 printf(
"|%*s: change priority for job that this jq_pk is in |\n", P_WIDTH,
"priority <jq_pk> <level>");
192 printf(
"|%*s: causes the scheduler to check the job queue |\n", P_WIDTH,
"database");
193 printf(
"+-----------------------------------------------------------------------------+\n");
201 int main(
int argc,
char** argv)
211 GOptionContext* options;
212 GError* error = NULL;
219 uint8_t c_reload = 0;
220 uint8_t c_status = 0;
221 uint8_t c_agents = 0;
222 uint8_t c_restart = 0;
223 uint8_t c_verbose = 0;
224 uint8_t c_database = 0;
229 config = DEFAULT_SETUP;
230 memset(buffer,
'\0',
sizeof(buffer));
234 GOptionEntry entries[] =
236 {
"config",
'c', 0, G_OPTION_ARG_STRING, &config,
237 "Set the directory for the system configuration",
"string"},
238 {
"host",
'H', 0, G_OPTION_ARG_STRING, &host,
239 "Set the host that the scheduler is on",
"string"},
240 {
"port",
'p', 0, G_OPTION_ARG_STRING, &port,
241 "Set the port that the scheduler is listening on",
"integer"},
242 {
"quiet",
'q', 0, G_OPTION_ARG_NONE, &
verbose,
243 "Cause the CLI to not print usage hints", NULL},
244 {
"load",
'l', 0, G_OPTION_ARG_NONE, &c_load,
245 "CLI will send a load command and close"},
246 {
"agents",
'a', 0, G_OPTION_ARG_NONE, &c_agents,
247 "CLI will send an agents command and close"},
248 {
"status",
'S', 0, G_OPTION_ARG_NONE, &c_status,
249 "CLI will send a status command and close"},
250 {
"stop",
's', 0, G_OPTION_ARG_NONE, &c_stop,
251 "CLI will send stop command and close", NULL},
252 {
"die",
'D', 0, G_OPTION_ARG_NONE, &c_die,
253 "CLI will send a die command and close"},
254 {
"pause",
'P', 0, G_OPTION_ARG_INT, &c_pause,
255 "CLI will send a pause command and close",
"integer"},
256 {
"reload",
'r', 0, G_OPTION_ARG_NONE, &c_reload,
257 "CLI will send a reload command and close", NULL},
258 {
"restart",
'R', 0, G_OPTION_ARG_INT, &c_restart,
259 "CLI will send a restart command and close",
"integer"},
260 {
"verbose",
'v', 0, G_OPTION_ARG_INT, &c_verbose,
261 "CLI will change the scheduler's verbose level",
"integer"},
262 {
"database",
'd', 0, G_OPTION_ARG_NONE, &c_database,
263 "CLI will send a database command to scheduler", NULL},
267 options = g_option_context_new(
"- command line tool for FOSSology scheduler");
268 g_option_context_add_main_entries(options, entries, NULL);
269 g_option_context_set_ignore_unknown_options(options, FALSE);
270 g_option_context_parse(options, &argc, &argv, &error);
274 config = g_option_context_get_help(options, FALSE, NULL);
275 fprintf(stderr,
"ERROR: %s\n%s", error->message, config);
280 g_option_context_free(options);
286 snprintf(buffer,
sizeof(buffer),
"%s/fossology.conf", config);
290 fprintf(stderr,
"ERROR: %s.%d: error loading config: %s\n",
291 __FILE__, __LINE__, error->message);
298 if(!error && host == NULL)
306 if(c_die || c_stop || c_load || c_pause || c_reload || c_status || c_agents
307 || c_restart || c_verbose || c_database)
313 bytes = write(
s,
"reload", 6);
315 bytes = write(
s,
"database", 8);
317 bytes = write(
s,
"stop", 4);
319 bytes = write(
s,
"die", 3);
324 snprintf(buffer,
sizeof(buffer) - 1,
"verbose %d", c_verbose);
325 bytes = write(
s, buffer, strlen(buffer));
330 snprintf(buffer,
sizeof(buffer) - 1,
"pause %d", c_pause);
331 bytes = write(
s, buffer, strlen(buffer));
336 snprintf(buffer,
sizeof(buffer) - 1,
"restart %d", c_restart);
337 bytes = write(
s, buffer, strlen(buffer));
343 bytes = write(
s,
"load", 4);
344 receive(
s, buffer,
sizeof(buffer), TRUE);
349 bytes = write(
s,
"status", 6);
350 receive(
s, buffer,
sizeof(buffer), TRUE);
355 bytes = write(
s,
"agents", 6);
356 receive(
s, buffer,
sizeof(buffer), TRUE);
370 FD_SET(fileno(stdin), &fds);
371 memset(buffer,
'\0',
sizeof(buffer));
372 select(
s + 1, &fds, NULL, NULL, NULL);
375 if(FD_ISSET(
s, &fds))
376 closing =
receive(
s, buffer,
sizeof(buffer), FALSE);
379 if(FD_ISSET(fileno(stdin), &fds))
381 if(read(fileno(stdin), buffer,
sizeof(buffer)) == 0)
384 if(strcmp(buffer,
"help\n") == 0)
390 response = (strncmp(buffer,
"agents", 6) == 0 ||
391 strncmp(buffer,
"status", 6) == 0 ||
392 strcmp (buffer,
"verbose\n" ) == 0 ||
393 strcmp (buffer,
"load\n" ) == 0) ?
396 if((bytes = write(
s, buffer, strlen(buffer) - 1)) != strlen(buffer) - 1)
398 printf(
"ERROR: couldn't write %lu bytes to socket\n", bytes);
int closing
Set if scheduler is shutting down.
int s
The socket that the CLI will use to communicate.
void interface_usage()
Interface usage print.
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found, the error object is set and NULL is returned.
fo_conf * conf
The loaded configuration data.
int verbose
The verbose flag for the cli.
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
int socket_connect(char *host, char *port)
Create a socket connection.
The main FOSSology C library.
char buffer[2048]
The last thing received from the scheduler.
uint8_t receive(int s, char *buffer, size_t max, uint8_t end)
Performs the actions necessary to receive from the scheduler.
int response
Is a response expected from the scheduler.
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.