26 #include <interface.h> 41 #include <sys/types.h> 48 #define FIELD_WIDTH 10 49 #define BUFFER_SIZE 1024 51 #define netw g_output_stream_write 53 #define PROXY_PROTOCOL "socks5" 54 #define PROXY_DEFAULT_PORT 1080 83 GSocketConnection*
conn, GThreadPool* threads)
88 inter->
istr = g_io_stream_get_input_stream((GIOStream*)inter->
conn);
89 inter->
ostr = g_io_stream_get_output_stream((GIOStream*)inter->
conn);
90 g_thread_pool_push(threads, inter, NULL);
105 g_object_unref(inter->
conn);
139 GMatchInfo* regex_match;
143 char* arg1, * arg2, * arg3;
148 memset(buffer,
'\0',
sizeof(buffer));
150 while(g_input_stream_read(conn->
istr, buffer,
sizeof(buffer), scheduler->
cancel, NULL) > 0)
152 V_INTERFACE(
"INTERFACE: received \"%s\"\n", buffer);
154 memcpy(org, buffer,
sizeof(buffer));
155 for(cmd = buffer; *cmd; cmd++)
156 *cmd = g_ascii_tolower(*cmd);
158 cmd = g_match_info_fetch(regex_match, 1);
162 g_output_stream_write(conn->
ostr,
"Invalid command: \"", 18, NULL, NULL);
163 g_output_stream_write(conn->
ostr, buffer, strlen(buffer), NULL, NULL);
164 g_output_stream_write(conn->
ostr,
"\"\n", 2, NULL, NULL);
165 g_match_info_free(regex_match);
166 WARNING(
"INTERFACE: invalid command: \"%s\"", buffer);
171 V_INTERFACE(
"INTERFACE: send \"received\"\n");
172 g_output_stream_write(conn->
ostr,
"received\n", 9, NULL, NULL);
179 if(strcmp(cmd,
"close") == 0)
181 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
182 V_INTERFACE(
"INTERFACE: closing connection to user interface\n");
184 g_match_info_free(regex_match);
195 else if(strcmp(cmd,
"stop") == 0)
197 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
198 V_INTERFACE(
"INTERFACE: shutting down scheduler gracefully\n");
201 g_match_info_free(regex_match);
212 else if(strcmp(cmd,
"die") == 0)
214 g_output_stream_write(conn->
ostr,
"CLOSE\n", 6, NULL, NULL);
215 V_INTERFACE(
"INTERFACE: killing the scheduler\n");
218 g_match_info_free(regex_match);
229 else if(strcmp(cmd,
"load") == 0)
243 else if(strcmp(cmd,
"kill") == 0)
245 arg1 = g_match_info_fetch(regex_match, 3);
246 arg2 = g_match_info_fetch(regex_match, 8);
250 if(arg1 == NULL || arg2 == NULL || strlen(arg1) == 0 || strlen(arg2) == 0)
253 cmd = g_strdup_printf(
"Invalid kill command: \"%s\"\n", buffer);
254 g_output_stream_write(conn->
ostr, cmd, strlen(cmd), NULL, NULL);
256 else if((job = g_tree_lookup(scheduler->
job_list, &i)) == NULL)
265 job->
message = strdup(((arg2 == NULL) ?
"no message" : arg2));
279 else if(strcmp(cmd,
"pause") == 0)
281 arg1 = g_match_info_fetch(regex_match, 3);
283 if(arg1 == NULL || strlen(arg1) == 0)
285 arg1 = g_strdup_printf(
"Invalid pause command: \"%s\"\n", buffer);
286 WARNING(
"received invalid pause command: %s", buffer);
287 g_output_stream_write(conn->
ostr, arg1, strlen(arg1), NULL, NULL);
293 params->second = atoi(arg1);
294 params->first = g_tree_lookup(scheduler->
job_list, ¶ms->second);
306 else if(strcmp(cmd,
"reload") == 0)
316 else if(strcmp(cmd,
"agents") == 0)
331 else if(strcmp(cmd,
"status") == 0)
333 arg1 = g_match_info_fetch(regex_match, 3);
336 params->first = conn->
ostr;
337 params->second = (arg1 == NULL) ? 0 : atoi(arg1);
349 else if(strcmp(cmd,
"restart") == 0)
351 arg1 = g_match_info_fetch(regex_match, 3);
355 arg1 = g_strdup(buffer);
356 WARNING(
"received invalid restart command: %s", buffer);
357 snprintf(buffer,
sizeof(buffer) - 1,
358 "ERROR: Invalid restart command: %s\n", arg1);
359 g_output_stream_write(conn->
ostr, buffer, strlen(buffer), NULL, NULL);
365 params->second = atoi(arg1);
366 params->first = g_tree_lookup(scheduler->
job_list, ¶ms->second);
383 else if(strcmp(cmd,
"verbose") == 0)
385 arg1 = g_match_info_fetch(regex_match, 3);
386 arg2 = g_match_info_fetch(regex_match, 5);
392 sprintf(buffer,
"level: %d\n",
verbose);
396 strcpy(buffer,
"mask: h d i e s a j\nmask: ");
397 for(i = 1; i < 0x10000; i <<= 1)
398 strcat(buffer, i &
verbose ?
"1 " :
"0 ");
399 strcat(buffer,
"\n");
401 g_output_stream_write(conn->
ostr, buffer, strlen(buffer), NULL, NULL);
403 else if(arg2 == NULL)
411 if((job = g_tree_lookup(scheduler->
job_list, &i)) == NULL)
414 cmd = g_strdup_printf(
"Invalid verbose command: \"%s\"\n", buffer);
415 g_output_stream_write(conn->
ostr, cmd, strlen(cmd), NULL, NULL);
434 else if(strcmp(cmd,
"priority") == 0)
436 arg1 = g_match_info_fetch(regex_match, 3);
437 arg2 = g_match_info_fetch(regex_match, 5);
439 if(arg1 != NULL && arg2 != NULL)
444 params->first = g_tree_lookup(scheduler->
job_list, &i);
445 params->second = atoi(arg2);
452 if(arg1) g_free(arg1);
453 if(arg2) g_free(arg2);
455 arg1 = g_strdup(buffer);
456 WARNING(
"Invalid priority command: %s\n", buffer);
457 snprintf(buffer,
sizeof(buffer) - 1,
458 "ERROR: Invalid priority command: %s\n", arg1);
459 g_output_stream_write(conn->
ostr, buffer, strlen(buffer), NULL, NULL);
469 else if(strcmp(cmd,
"database") == 0)
481 g_output_stream_write(conn->
ostr,
"Invalid command: \"", 18, NULL, NULL);
482 g_output_stream_write(conn->
ostr, buffer, strlen(buffer), NULL, NULL);
483 g_output_stream_write(conn->
ostr,
"\"\n", 2, NULL, NULL);
484 con_printf(
main_log,
"ERROR %s.%d: Interface received invalid command: %s\n", __FILE__, __LINE__, cmd);
487 g_match_info_free(regex_match);
489 memset(buffer,
'\0',
sizeof(buffer));
507 GSocketListener* server_socket;
508 GSocketConnection* new_connection;
509 GError* error = NULL;
514 ERROR(
"Could not create server socket thread\n");
519 server_socket = g_socket_listener_new();
520 if(server_socket == NULL)
521 FATAL(
"could not create the server socket");
523 g_socket_listener_add_inet_port(server_socket, scheduler->
i_port, NULL, &error);
525 FATAL(
"[port:%d]: %s", scheduler->
i_port, error->message);
526 scheduler->
cancel = g_cancellable_new();
528 V_INTERFACE(
"INTERFACE: listening port is %d\n", scheduler->
i_port);
533 new_connection = g_socket_listener_accept(server_socket, NULL,
534 scheduler->
cancel, &error);
538 V_INTERFACE(
"INTERFACE: new interface connection\n");
540 FATAL(
"INTERFACE closing for %s", error->message);
545 V_INTERFACE(
"INTERFACE: socket listening thread closing\n");
546 g_socket_listener_close(server_socket);
547 g_object_unref(server_socket);
574 scheduler, CONF_interface_nthreads, FALSE, NULL);
576 #if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 32 577 scheduler->
server = g_thread_new(
"interface",
580 scheduler->
server = g_thread_create((GThreadFunc)interface_listen_thread,
581 scheduler, TRUE, NULL);
584 while(scheduler->
cancel == NULL)
589 WARNING(
"Multiple attempts made to initialize the interface");
607 g_cancellable_cancel(scheduler->
cancel);
608 g_thread_join(scheduler->
server);
609 g_thread_pool_free(scheduler->
workers, FALSE, TRUE);
617 WARNING(
"Attempt to destroy the interface without initializing it");
void job_verbose_event(scheduler_t *scheduler, job_t *job)
GTree * host_list
List of all hosts available to the scheduler.
void interface_thread(interface_connection *conn, scheduler_t *scheduler)
Function that will run the thread associated with a particular interface instance.
void scheduler_close_event(scheduler_t *scheduler, void *killed)
Sets the closing flag and possibly kills all currently running agents.
void list_agents_event(scheduler_t *scheduler, GOutputStream *ostr)
Receive agent on interface.
GCancellable * cancel
Used to stop the listening thread when it is running.
void job_pause_event(scheduler_t *scheduler, arg_int *params)
Event to pause a job.
void interface_destroy(scheduler_t *scheduler)
Closes the server socket and thread pool that service UI connections.
GRegex * parse_interface_cmd
Parses the commands received by the interface.
static void interface_conn_destroy(interface_connection *inter)
Free the memory associated with an interface connection.
gboolean i_created
Has the interface been created.
void database_update_event(scheduler_t *scheduler, void *unused)
Checks the job queue for any new entries.
uint16_t i_port
The port that the scheduler is listening on.
GThread * server
Thread that is listening to the server socket.
int32_t verbose
The verbose level for all of the agents in this job.
GTree * job_list
List of jobs that have been created.
#define BUFFER_SIZE
Maximum buffer length.
void job_restart_event(scheduler_t *scheduler, arg_int *params)
int verbose
The verbose flag for the cli.
gboolean i_terminate
Has the interface been terminated.
void database_exec_event(scheduler_t *scheduler, char *sql)
void print_host_load(GTree *host_list, GOutputStream *ostr)
Prints the host information to ostr.
GSocketConnection * conn
The socket that is our connection.
char buffer[2048]
The last thing received from the scheduler.
Event handling operations.
GInputStream * istr
Stream to read from the interface.
const char * jobsql_failed
static interface_connection * interface_conn_init(GSocketConnection *conn, GThreadPool *threads)
Header file with agent related operations.
struct interface_connection interface_connection
gchar * message
Message that will be sent with job notification email.
void scheduler_config_event(scheduler_t *scheduler, void *unused)
Load both the fossology configuration and all the agent configurations.
void interface_init(scheduler_t *scheduler)
Create the interface thread and thread pool that handle UI connections.
void * interface_listen_thread(scheduler_t *scheduler)
Function that will listen for new connections to the server sockets.
void job_priority_event(scheduler_t *scheduler, arg_int *params)
void job_status_event(scheduler_t *scheduler, arg_int *params)
Event to get the status of the scheduler or a specific job.
GThreadPool * workers
Threads to handle incoming network communication.
GOutputStream * ostr
Stream to write to the interface.
Header file for the scheduler.
void job_fail_event(scheduler_t *scheduler, job_t *job)
Events that causes a job to be marked a failed.