21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
34 #define vprintf(...) if(verbose) printf(__VA_ARGS__);
57 struct addrinfo hints;
58 struct addrinfo* servs, * curr = NULL;
60 memset(&hints, 0,
sizeof(hints));
61 hints.ai_family = AF_UNSPEC;
62 hints.ai_socktype = SOCK_STREAM;
63 if(getaddrinfo(host, port, &hints, &servs) == -1)
65 fprintf(stderr,
"ERROR: %s.%d: unable to connect to %s port: %s\n",
66 __FILE__, __LINE__, host, port);
67 fprintf(stderr,
"ERROR: errno: %s\n", strerror(errno));
71 for(curr = servs; curr != NULL; curr = curr->ai_next)
73 if((fd = socket(curr->ai_family, hints.ai_socktype, curr->ai_protocol)) < 0)
76 if(connect(fd, curr->ai_addr, curr->ai_addrlen) == -1)
84 fprintf(stderr,
"ERROR: %s.%d: unable to connect to %s port: %s\n",
85 __FILE__, __LINE__, host, port);
121 printf(
"ERROR: connection to scheduler closed\nERROR: closing cli\n");
130 poss = strtok(NULL,
"\n"))
132 if(strncmp(poss,
"received", 8) == 0)
135 printf(
"Command received\n");
137 else if(strncmp(poss,
"CLOSE", 5) == 0)
141 else if(strcmp(poss,
"end") != 0)
143 printf(
"%s\n", poss);
164 printf(
"FOSSology scheduler command line interface\n");
165 printf(
"+-----------------------------------------------------------------------------+\n");
166 printf(
"|%*s: EFFECT |\n", P_WIDTH,
"CMD [optional] <required>");
167 printf(
"+-----------------------------------------------------------------------------+\n");
168 printf(
"|%*s: prints this usage statement |\n", P_WIDTH,
"help");
169 printf(
"|%*s: close the connection and exit cli |\n", P_WIDTH,
"close");
170 printf(
"|%*s: shutdown will wait for agents be stopping |\n", P_WIDTH,
"stop");
171 printf(
"|%*s: shutdown will shutdown immediately |\n", P_WIDTH,
"die");
172 printf(
"|%*s: get load information for host machines |\n", P_WIDTH,
"load");
173 printf(
"|%*s: kills a currently running job (ungraceful) |\n", P_WIDTH,
"kill <jq_pk> <\"message\">");
174 printf(
"|%*s: pauses a job indefinitely |\n", P_WIDTH,
"pause <jq_pk>");
175 printf(
"|%*s: reload the configuration information |\n", P_WIDTH,
"reload");
176 printf(
"|%*s: prints a list of valid agents |\n", P_WIDTH,
"agents");
177 printf(
"|%*s: scheduler responds with status information |\n", P_WIDTH,
"status [jq_pk]");
178 printf(
"|%*s: restart a paused job |\n", P_WIDTH,
"restart <jq_pk>");
179 printf(
"|%*s: query/change the scheduler/job verbosity |\n", P_WIDTH,
"verbose [jq_pk] [level]");
180 printf(
"|%*s: change priority for job that this jq_pk is in |\n", P_WIDTH,
"priority <jq_pk> <level>");
181 printf(
"|%*s: causes the scheduler to check the job queue |\n", P_WIDTH,
"database");
182 printf(
"+-----------------------------------------------------------------------------+\n");
190 int main(
int argc,
char** argv)
200 GOptionContext* options;
201 GError* error = NULL;
208 uint8_t c_reload = 0;
209 uint8_t c_status = 0;
210 uint8_t c_agents = 0;
211 uint8_t c_restart = 0;
212 uint8_t c_verbose = 0;
213 uint8_t c_database = 0;
218 config = DEFAULT_SETUP;
223 GOptionEntry entries[] =
225 {
"config",
'c', 0, G_OPTION_ARG_STRING, &config,
226 "Set the directory for the system configuration",
"string"},
227 {
"host",
'H', 0, G_OPTION_ARG_STRING, &host,
228 "Set the host that the scheduler is on",
"string"},
229 {
"port",
'p', 0, G_OPTION_ARG_STRING, &port,
230 "Set the port that the scheduler is listening on",
"integer"},
231 {
"quiet",
'q', 0, G_OPTION_ARG_NONE, &
verbose,
232 "Cause the CLI to not print usage hints", NULL},
233 {
"load",
'l', 0, G_OPTION_ARG_NONE, &c_load,
234 "CLI will send a load command and close"},
235 {
"agents",
'a', 0, G_OPTION_ARG_NONE, &c_agents,
236 "CLI will send an agents command and close"},
237 {
"status",
'S', 0, G_OPTION_ARG_NONE, &c_status,
238 "CLI will send a status command and close"},
239 {
"stop",
's', 0, G_OPTION_ARG_NONE, &c_stop,
240 "CLI will send stop command and close", NULL},
241 {
"die",
'D', 0, G_OPTION_ARG_NONE, &c_die,
242 "CLI will send a die command and close"},
243 {
"pause",
'P', 0, G_OPTION_ARG_INT, &c_pause,
244 "CLI will send a pause command and close",
"integer"},
245 {
"reload",
'r', 0, G_OPTION_ARG_NONE, &c_reload,
246 "CLI will send a reload command and close", NULL},
247 {
"restart",
'R', 0, G_OPTION_ARG_INT, &c_restart,
248 "CLI will send a restart command and close",
"integer"},
249 {
"verbose",
'v', 0, G_OPTION_ARG_INT, &c_verbose,
250 "CLI will change the scheduler's verbose level",
"integer"},
251 {
"database",
'd', 0, G_OPTION_ARG_NONE, &c_database,
252 "CLI will send a database command to scheduler", NULL},
256 options = g_option_context_new(
"- command line tool for FOSSology scheduler");
257 g_option_context_add_main_entries(options, entries, NULL);
258 g_option_context_set_ignore_unknown_options(options, FALSE);
259 g_option_context_parse(options, &argc, &argv, &error);
263 config = g_option_context_get_help(options, FALSE, NULL);
264 fprintf(stderr,
"ERROR: %s\n%s", error->message, config);
269 g_option_context_free(options);
275 snprintf(
buffer,
sizeof(
buffer),
"%s/fossology.conf", config);
279 fprintf(stderr,
"ERROR: %s.%d: error loading config: %s\n",
280 __FILE__, __LINE__, error->message);
287 if(!error && host == NULL)
295 if(c_die || c_stop || c_load || c_pause || c_reload || c_status || c_agents
296 || c_restart || c_verbose || c_database)
302 bytes = write(
s,
"reload", 6);
304 bytes = write(
s,
"database", 8);
306 bytes = write(
s,
"stop", 4);
308 bytes = write(
s,
"die", 3);
313 snprintf(
buffer,
sizeof(
buffer) - 1,
"verbose %d", c_verbose);
319 snprintf(
buffer,
sizeof(
buffer) - 1,
"pause %d", c_pause);
325 snprintf(
buffer,
sizeof(
buffer) - 1,
"restart %d", c_restart);
332 bytes = write(
s,
"load", 4);
338 bytes = write(
s,
"status", 6);
344 bytes = write(
s,
"agents", 6);
359 FD_SET(fileno(stdin), &fds);
361 select(
s + 1, &fds, NULL, NULL, NULL);
364 if(FD_ISSET(
s, &fds))
368 if(FD_ISSET(fileno(stdin), &fds))
373 if(strcmp(
buffer,
"help\n") == 0)
380 strncmp(
buffer,
"status", 6) == 0 ||
381 strcmp (
buffer,
"verbose\n" ) == 0 ||
382 strcmp (
buffer,
"load\n" ) == 0) ?
387 printf(
"ERROR: couldn't write %lu bytes to socket\n", bytes);
uint8_t receive(int s, char *buffer, size_t max, uint8_t end)
Performs the actions necessary to receive from the scheduler.
int verbose
The verbose flag for the cli.
int response
Is a response expected from the scheduler.
int s
The socket that the CLI will use to communicate.
fo_conf * conf
The loaded configuration data.
void interface_usage()
Interface usage print.
int socket_connect(char *host, char *port)
Create a socket connection.
fo_conf * fo_config_load(char *rawname, GError **error)
Load the configuration information from the provided file.
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,...
FUNCTION int max(int permGroup, int permPublic)
Get the maximum group privilege.
The main FOSSology C library.
char buffer[2048]
The last thing received from the scheduler.
int closing
Set if scheduler is shutting down.