static pid_t common_execute(command_handle cCommand) { if (!cCommand) return -1; command_reference sent_command = send_command(cCommand); destroy_command(cCommand); if (!sent_command) return -1; command_event outcome = wait_command_event(sent_command, event_register, local_default_timeout()); clear_command_status(sent_command); if (!(outcome & event_register)) return -1; const struct message_info *message = rotate_response(sent_command); if (!message) return -1; int value = -1; if (RSERVR_IS_RESPONSE_SINGLE(message)) parse_integer10(RSERVR_TO_RESPONSE_SINGLE(message), &value); remove_responses(sent_command); return value; //NOTE: bad parse return will leave 'value' as -1 }
bool netlink_cancel(struct netlink_info *netlink, unsigned int id) { struct command *command; if (!netlink || id == 0) return false; if (!netlink->command_queue || !netlink->command_pending || !netlink->command_lookup) return false; command = g_hash_table_lookup(netlink->command_lookup, GUINT_TO_POINTER(id)); if (!command) return false; g_hash_table_remove(netlink->command_lookup, GUINT_TO_POINTER(id)); if (!g_queue_remove(netlink->command_queue, command)) { g_hash_table_remove(netlink->command_pending, GUINT_TO_POINTER(command->seq)); } destroy_command(command); return true; }
static gboolean cleanup_command(gpointer key, gpointer value, gpointer user_data) { struct command *command = value; destroy_command(command); return TRUE; }
void destroy_pipeline(Pipeline* ppl) { int i; for(i = 0; ppl->cmds[i] != NULL; i++) { destroy_command(ppl->cmds[i]); } safe_free(ppl->cmdline); free(ppl); }
int main() { char input[CMD_MAX_LENGTH]; Command cmd; int exit_code = 0; char prompt[1024]; char hostname[256]; char cwd[256]; char *fgetsret = NULL; signal(SIGCHLD, sigchld_handler); gethostname(hostname, 256); getcwd(cwd, 256); setup_prompt(prompt, hostname, cwd); while(1) { printf("%s", prompt); fgetsret = fgets(input, CMD_MAX_LENGTH, stdin); if(fgetsret == NULL) return 0; delete_ending_newline(input); if(strlen(input) != 0) { if(cmd_exit(input, &exit_code)) return exit_code; init_command(&cmd); if(parse_members(input, &cmd) == 0 && parse_args(&cmd) == 0) { /* aff_members(&cmd); aff_args(&cmd); aff_redirect(&cmd); printf("----------\n"); */ exec_command(&cmd); } destroy_command(&cmd); } *input = '\0'; } return 0; }
char* get_command_proposal(char* name) { static char ans[100]; command* c; if(strcmp(name, "MCA") == 0) { c = init_command(name, "PWMD", "PWMI"); } else if(strcmp(name, "MCA") == 0) { c = init_command(name, "PWMF", "PWMB"); } else { c = init_command(name, "", ""); } strcpy(ans, command_string(c)); destroy_command(c); return ans; }
static void *thread_alteration(void *cCopy) { if (!cCopy) return NULL; copied_info *const current_info = static_cast <copied_info*> (cCopy); command_event outcome = __rsvp_dataref_hook_alteration(¤t_info->info, current_info->reference, current_info->offset, current_info->size); if (current_info->info.respond && outcome != event_none) { command_handle new_response = short_response(current_info->info.respond, outcome); if (new_response) send_command_no_status(new_response); destroy_command(new_response); } destroy_info(current_info); return NULL; }
static void message_queue_hook(int eEvent) { /*NOTE: the queue will hang while this function is processing*/ static int processing = 0; command_handle new_command = NULL; const struct message_info *message = NULL; if (eEvent == RSERVR_QUEUE_MESSAGE) { if (processing) return; processing = 1; while ((message = current_message())) { new_command = NULL; if (RSERVR_IS_REQUEST(message) && !RSERVR_IS_BINARY(message)) new_command = client_response(RSERVR_RESPOND(message), event_complete, RSERVR_TO_REQUEST_SINGLE(message)); else if (RSERVR_IS_INFO(message) && !RSERVR_IS_BINARY(message)) { fprintf(stderr, "%s\n", RSERVR_TO_INFO_MESSAGE(message)); new_command = short_response(RSERVR_RESPOND(message), event_complete); } else if (!RSERVR_IS_RESPONSE(message)) new_command = short_response(RSERVR_RESPOND(message), event_error); if (new_command) { send_command_no_status(new_command); destroy_command(new_command); } remove_current_message(); } processing = 0; } }
static void process_multi(struct netlink_info *netlink, struct nlmsghdr *nlmsg) { const void *data = nlmsg; struct command *command; command = g_hash_table_lookup(netlink->command_pending, GUINT_TO_POINTER(nlmsg->nlmsg_seq)); if (!command) return; if (!command->handler) goto done; if (nlmsg->nlmsg_type < NLMSG_MIN_TYPE) { const struct nlmsgerr *err; switch (nlmsg->nlmsg_type) { case NLMSG_DONE: case NLMSG_ERROR: err = data + NLMSG_HDRLEN; command->handler(-err->error, 0, NULL, 0, command->user_data); break; } } else { command->handler(0, nlmsg->nlmsg_type, data + NLMSG_HDRLEN, nlmsg->nlmsg_len - NLMSG_HDRLEN, command->user_data); return; } done: g_hash_table_remove(netlink->command_pending, GUINT_TO_POINTER(nlmsg->nlmsg_seq)); g_hash_table_remove(netlink->command_lookup, GUINT_TO_POINTER(command->id)); destroy_command(command); }
static gboolean can_write_data(GIOChannel *chan, GIOCondition cond, gpointer user_data) { struct netlink_info *netlink = user_data; struct command *command; struct sockaddr_nl addr; const void *data; ssize_t written; int sk; command = g_queue_pop_head(netlink->command_queue); if (!command) return FALSE; sk = g_io_channel_unix_get_fd(chan); memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_pid = 0; data = ((void *) command) + NLMSG_ALIGN(sizeof(struct command)); written = sendto(sk, data, command->len, 0, (struct sockaddr *) &addr, sizeof(addr)); if (written < 0 || (uint32_t) written != command->len) { g_hash_table_remove(netlink->command_lookup, GUINT_TO_POINTER(command->id)); destroy_command(command); return FALSE; } util_hexdump('<', data, command->len, netlink->debug_handler, netlink->debug_data); g_hash_table_replace(netlink->command_pending, GUINT_TO_POINTER(command->seq), command); return g_queue_get_length(netlink->command_queue) > 0; }
int main(int argc, char *argv[]) { if (argc != 2 || !strlen(argv[1])) { fprintf(stderr, "%s [client name]\n", argv[0]); return 1; } if (!set_program_name(argv[0]) || !initialize_client()) { fprintf(stderr, "%s: could not initialize client\n", argv[0]); client_cleanup(); return 1; } load_internal_plugins(); if (!start_message_queue()) { fprintf(stderr, "%s: could not start message queue\n", argv[0]); client_cleanup(); return 1; } if (!register_resource_client(NULL)) { fprintf(stderr, "%s: could not register client\n", argv[0]); stop_message_queue(); client_cleanup(); return 1; } command_handle new_service = register_service(argv[1], PARAM_ECHO_TYPE); command_reference service_status = 0; if ( !new_service || !(service_status = send_command(new_service)) || !(wait_command_event(service_status, event_complete, local_default_timeout()) & event_complete) ) { fprintf(stderr, "%s: could not register service\n", argv[0]); if (new_service) destroy_command(new_service); if (service_status) clear_command_status(service_status); stop_message_queue(); client_cleanup(); return 1; } destroy_command(new_service); clear_command_status(service_status); set_log_client_name(argv[1]); if (!stop_message_queue()) { client_cleanup(); return 1; } set_queue_event_hook(&message_queue_hook); result outcome = inline_message_queue(); client_cleanup(); return outcome? 0 : 1; }
int main(int argc, char *argv[]) { if (argc < 3 || !strlen(argv[1])) { fprintf(stderr, "%s [client name] [config file] (...)\n", argv[0]); return 1; } if (!set_program_name(argv[0]) || !initialize_client()) { fprintf(stderr, "%s: could not initialize client\n", argv[0]); client_cleanup(); return 1; } block_messages(); if (!start_message_queue()) { fprintf(stderr, "%s: could not start message queue\n", argv[0]); client_cleanup(); return 1; } if (!register_admin_client(argv[1])) { fprintf(stderr, "%s: could not register client\n", argv[0]); stop_message_queue(); client_cleanup(); return 1; } command_handle new_monitor = set_monitor_flags(monitor_attached_clients); command_reference monitor_status = 0; if ( !new_monitor || !(monitor_status = send_command(new_monitor)) || !(wait_command_event(monitor_status, event_complete, local_default_timeout()) & event_complete) ) { fprintf(stderr, "%s: could not monitor server\n", argv[0]); if (new_monitor) destroy_command(new_monitor); if (monitor_status) clear_command_status(monitor_status); stop_message_queue(); client_cleanup(); return 1; } destroy_command(new_monitor); clear_command_status(monitor_status); int outcome = 0, I; for (I = 2; I < argc && outcome >= 0; I++) { int protocol_pid = -1; int config_fd = open_protocol_file(argv[I], &protocol_pid); if (config_fd < 0) { if (config_fd == RSERVR_FILE_ERROR) fprintf(stderr, "%s: can't open configuration file '%s': %s\n", argv[0], argv[I], strerror(errno)); if (config_fd == RSERVR_PROTOCOL_ERROR) /*TODO: get rid of hard-coded message*/ fprintf(stderr, "%s: can't open configuration file '%s': %s\n", argv[0], argv[I], "protocol error"); if (config_fd == RSERVR_BAD_PROTOCOL) /*TODO: get rid of hard-coded message*/ fprintf(stderr, "%s: can't open configuration file '%s': %s\n", argv[0], argv[I], "bad protocol"); stop_message_queue(); client_cleanup(); return 1; } FILE *next_file = fdopen(config_fd, "r"); if (!next_file) { fprintf(stderr, "%s: can't open configuration file '%s': %s\n", argv[0], argv[I], strerror(errno)); if (protocol_pid >= 0) close_protocol_process(protocol_pid); stop_message_queue(); client_cleanup(); return 1; } else { char *directory = try_protocol_filename(argv[I]); if ( (outcome = parse_file(next_file, argv[0], directory? dirname(directory) : NULL)) < 0 ) { fprintf(stderr, "%s: parsing error in configuration file '%s'\n", argv[0], argv[I]); fclose(next_file); if (protocol_pid >= 0) close_protocol_process(protocol_pid); if (directory) free(directory); stop_message_queue(); client_cleanup(); return 1; } fclose(next_file); if (directory) free(directory); if (protocol_pid >= 0) { if (close_protocol_process(protocol_pid) == RSERVR_PROTOCOL_ERROR) { /*TODO: get rid of hard-coded message*/ fprintf(stderr, "%s: can't open configuration file '%s': %s\n", argv[0], argv[I], "protocol error"); stop_message_queue(); client_cleanup(); return 1; } } } } if (outcome == 1) fprintf(stderr, "%s: missing continuation\n", argv[0]); set_queue_event_hook(&message_queue_hook); while (block_for_respawn()); if (message_queue_status()) stop_message_queue(); client_cleanup(); return 0; }