int job_list_push(JOB* item) { int pos; JOB_LIST* list = job_list(JL_GET); if (list == NULL || item == NULL) return -1; pos = list->last; if (list->v[pos] != NULL) pos++; if (pos >= list->capacity) { JOB** newv; newv = (JOB**) realloc(list->v, sizeof(JOB*)*(2*list->capacity)); error(newv == NULL, -1); list->v = newv; list->capacity *= 2; } list->v[pos] = item; list->last = pos; list->jobcount++; return 0; }
static void DumpStdStreams(const CArgValue& arg, CNSInfoCollector* info_collector, CNcbiOstream* out, bool use_stderr) { CCmdLineArgList job_list( CCmdLineArgList::CreateFrom(arg.AsString())); std::string job_id; while (job_list.GetNextArg(job_id)) { CNSJobInfo job_info(job_id, *info_collector); NcbiStreamCopy(*out, use_stderr ? job_info.GetStdErr() : job_info.GetStdOut()); } }
void job_list_erase(const JOB* item) { int idx = 0; JOB_LIST* list = job_list(JL_GET); if (list == NULL || item == NULL) return; while (idx <= list->last && list->v[idx] != item) idx++; if (idx > list->last) return; list->v[idx] = NULL; list->jobcount--; if (list->jobcount == 0) list->last = -1; }
int job_list_find_lastmodified_id() { int idx, ret = -1; time_t last = -1; JOB_LIST* list = job_list(JL_GET); if (list == NULL) return -1; for (idx = 0; idx <= list->last; idx++) { JOB* job = list->v[idx]; if (job != NULL && job->lastmodified > last) { ret = idx; last = job->lastmodified; } } return ret; }
JOB* job_list_find_by_pid(pid_t pid) { int idx, i; JOB_LIST* list = job_list(JL_GET); if (list == NULL) return NULL; for (idx = 0; idx <= list->last; idx++) { JOB* job = list->v[idx]; if (job != NULL) { for (i = 0; i < job->ncmd; i++) { if (job->pid[i] == pid) return job; } } } return NULL; }
static void client_old_parse_messages(GebrCommProtocolSocket * socket, struct client *client) { GList *link; struct gebr_comm_message *message; while ((link = g_list_last(client->socket->protocol->messages)) != NULL) { message = (struct gebr_comm_message *)link->data; /* check login */ if (message->hash == gebr_comm_protocol_defs.ini_def.code_hash) { GList *arguments; GString *accounts_list = g_string_new(""); GString *queue_list = g_string_new(""); GString *display_port = g_string_new(""); /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 2)) == NULL) goto err; GString *version = g_list_nth_data(arguments, 0); GString *hostname = g_list_nth_data(arguments, 1); g_debug("Current protocol version is: %s", gebr_comm_protocol_get_version()); g_debug("Received protocol version: %s", version->str); if (strcmp(version->str, gebr_comm_protocol_get_version())) { gebr_comm_protocol_socket_oldmsg_send(client->socket, TRUE, gebr_comm_protocol_defs.err_def, 2, "protocol", gebr_comm_protocol_get_version()); goto err; } /* set client info */ client->socket->protocol->logged = TRUE; g_string_assign(client->socket->protocol->hostname, hostname->str); client->server_location = GEBR_COMM_SERVER_LOCATION_REMOTE; const gchar *server_type; if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_MOAB) { /* Get info from the MOAB cluster */ server_moab_read_credentials(accounts_list, queue_list); server_type = "moab"; } else server_type = "regular"; GString *mpi_flavors = g_string_new(""); g_list_foreach(gebrd->mpi_flavors, (GFunc) get_mpi_flavors, mpi_flavors); if (mpi_flavors->len > 0) mpi_flavors = g_string_erase(mpi_flavors, mpi_flavors->len-1, 1); g_debug("------------on daemon, Sending %s", mpi_flavors->str) ; const gchar *model_name; const gchar *cpu_clock; const gchar *total_memory; GebrdCpuInfo *cpuinfo = gebrd_cpu_info_new(); GebrdMemInfo *meminfo = gebrd_mem_info_new(); model_name = gebrd_cpu_info_get (cpuinfo, 0, "model name"); cpu_clock = gebrd_cpu_info_get (cpuinfo, 0, "cpu MHz"); total_memory = gebrd_mem_info_get (meminfo, "MemTotal"); gchar *ncores = g_strdup_printf("%d", gebrd_cpu_info_n_procs(cpuinfo)); gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE, gebr_comm_protocol_defs.ret_def, 11, gebrd->hostname, server_type, accounts_list->str, model_name, total_memory, gebrd->fs_lock->str, ncores, cpu_clock, gebrd_user_get_daemon_id(gebrd->user), g_get_home_dir(), mpi_flavors->str); gebrd_cpu_info_free(cpuinfo); gebrd_mem_info_free(meminfo); gebr_comm_protocol_socket_oldmsg_split_free(arguments); g_string_free(accounts_list, TRUE); g_string_free(queue_list, TRUE); g_string_free(display_port, TRUE); g_free(ncores); } else if (message->hash == gebr_comm_protocol_defs.gid_def.code_hash) { GList *arguments; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL) goto err; GString *gid = g_list_nth_data(arguments, 0); GString *cookie = g_list_nth_data(arguments, 1); GString *disp_str = g_list_nth_data(arguments, 2); guint display; if (g_strcmp0(disp_str->str, "0") == 0) display = gebr_comm_get_available_port(6010); else display = atoi(disp_str->str); display = MAX(display-6000, 0); g_hash_table_insert(gebrd->display_ports, g_strdup(gid->str), GUINT_TO_POINTER(display)); g_debug("Received gid %s with cookie %s", gid->str, cookie->str); if (cookie->len && gebrd_get_server_type() != GEBR_COMM_SERVER_TYPE_MOAB) { gebrd_message(GEBR_LOG_DEBUG, "Authorizing with system(xauth)"); gchar *tmp = g_strdup_printf(":%d", display); if (!run_xauth_command("add", tmp, cookie->str)) display = 0; g_free(tmp); } gchar *display_str = g_strdup_printf("%d", display ? display + 6000 : 0); gebrd_message(GEBR_LOG_INFO, "Sending port %s to client %s!", display_str, gid->str); gebr_comm_protocol_socket_oldmsg_send(client->socket, FALSE, gebr_comm_protocol_defs.ret_def, 2, gid->str, display_str); g_free(display_str); gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (client->socket->protocol->logged == FALSE) { /* not logged! */ goto err; } else if (message->hash == gebr_comm_protocol_defs.qut_def.code_hash) { client_free(client); gebr_comm_message_free(message); return; } else if (message->hash == gebr_comm_protocol_defs.lst_def.code_hash) { job_list(client); } else if (message->hash == gebr_comm_protocol_defs.run_def.code_hash) { GList *arguments; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 9)) == NULL) goto err; GString *gid = g_list_nth_data(arguments, 0); GString *id = g_list_nth_data(arguments, 1); GString *frac = g_list_nth_data(arguments, 2); GString *numproc = g_list_nth_data(arguments, 3); GString *nice = g_list_nth_data(arguments, 4); GString *flow_xml = g_list_nth_data(arguments, 5); GString *paths = g_list_nth_data(arguments, 6); /* Moab & MPI settings */ GString *account = g_list_nth_data(arguments, 7); GString *servers_mpi = g_list_nth_data(arguments, 8); g_debug("SERVERS MPI %s", servers_mpi->str); /* try to run and send return */ job_new(&job, client, gid, id, frac, numproc, nice, flow_xml, account, paths, servers_mpi); #ifdef DEBUG gchar *env_delay = getenv("GEBRD_RUN_DELAY_SEC"); if (env_delay != NULL) sleep(atoi(env_delay)); #endif if (gebrd_get_server_type() == GEBR_COMM_SERVER_TYPE_REGULAR) { /* send job message (job is created -promoted from waiting server response- at the client) */ g_debug("RUN_DEF: run task with rid %s", id->str); job_send_clients_job_notify(job); job_run_flow(job); } else { /* ask moab to run */ job_run_flow(job); /* send job message (job is created -promoted from waiting server response- at the client) * at moab we must run the process before sending the JOB message, because at * job_run_flow moab_jid is acquired. */ job_send_clients_job_notify(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.rnq_def.code_hash) { } else if (message->hash == gebr_comm_protocol_defs.flw_def.code_hash) { } else if (message->hash == gebr_comm_protocol_defs.clr_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); job = job_find(rid); if (job != NULL) job_clear(job); /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.end_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); /* try to run and send return */ job = job_find(rid); if (job != NULL) { job_end(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.kil_def.code_hash) { GList *arguments; GString *rid; GebrdJob *job; /* organize message data */ if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 1)) == NULL) goto err; rid = g_list_nth_data(arguments, 0); /* try to run and send return */ job = job_find(rid); if (job != NULL) { job_kill(job); } /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); } else if (message->hash == gebr_comm_protocol_defs.path_def.code_hash) { GList *arguments; if ((arguments = gebr_comm_protocol_socket_oldmsg_split(message->argument, 3)) == NULL) goto err; GString *new_path = g_list_nth_data(arguments, 0); GString *old_path = g_list_nth_data(arguments, 1); GString *opt = g_list_nth_data(arguments, 2); g_debug("new_path:%s, old_path:%s, opt:%s", new_path->str, old_path->str, opt->str); GList *new_paths= parse_comma_separated_string(new_path->str); gint option = gebr_comm_protocol_path_str_to_enum(opt->str); gint status_id = -1; gboolean flag_exists = FALSE; gboolean flag_error = FALSE; switch (option) { case GEBR_COMM_PROTOCOL_PATH_CREATE: for (GList *j = new_paths; j; j = j->next) { GString *path = j->data; if (g_file_test(path->str, G_FILE_TEST_IS_DIR)){ flag_exists = TRUE; } else if (*(path->str) && g_mkdir_with_parents(path->str, 0700)) { flag_error = TRUE; break; } if (g_access(path->str, W_OK)==-1){ flag_error = TRUE; break; } } if (flag_error) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else if (flag_exists) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS; else status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; break; case GEBR_COMM_PROTOCOL_PATH_RENAME: g_debug("Renaming %s to %s", old_path->str, new_path->str); gboolean dir_exist = g_file_test(new_path->str, G_FILE_TEST_IS_DIR); gboolean create_dir = g_rename(old_path->str, new_path->str) == 0; if (!dir_exist && create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; else if (dir_exist && !create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_EXISTS; else if (!dir_exist && !create_dir) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else status_id = -1; break; case GEBR_COMM_PROTOCOL_PATH_DELETE: for (GList *j = new_paths; j; j = j->next) { GString *path = j->data; if (g_rmdir(path->str)) status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_ERROR; else status_id = GEBR_COMM_PROTOCOL_STATUS_PATH_OK; } break; default: g_warn_if_reached(); break; } g_debug("on %s, new_path:'%s', old_path:'%s', status_id: '%d'", __func__, new_path->str, old_path->str, status_id); /* frees */ gebr_comm_protocol_socket_oldmsg_split_free(arguments); gebr_comm_protocol_socket_oldmsg_send(socket, FALSE, gebr_comm_protocol_defs.ret_def, 2, gebrd->hostname, g_strdup_printf("%d", status_id)); } else if (message->hash == gebr_comm_protocol_defs.harakiri_def.code_hash) { /* Maestro wants me killed */ g_debug("Harakiri"); gebrd_quit(); } else { /* unknown message! */ goto err; } gebr_comm_message_free(message); client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link); } return; err: gebr_comm_message_free(message); client->socket->protocol->messages = g_list_delete_link(client->socket->protocol->messages, link); client_disconnected(socket, client); }
int main(int argc, char* argv[]) { char* str, dir[SLSH_MAX_PATH]; char opt_ver[] = "version", opt_comm[] = "command"; char shortopts[] = "c:"; JOB* job = NULL; struct sigaction chld; struct option longopts[3]; int opt, is_script = 0; char doc[] = "SoloSH 1.0 (beta)\nCopyright (C) 2016 Rodrigo Weigert <*****@*****.**>\n" "This program comes WITHOUT ANY WARRANTY, without even the implied\n" "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" "See the GNU General Public License for more details.\n\n" "This is free software, and you are welcome to redistribute it and/or\n" "modify it under the terms of the GNU General Public License; either\n" "version 3 of the License, or (at your option) any later version.\n\n"; longopts[0].name = opt_ver; longopts[0].has_arg = no_argument; longopts[0].flag = NULL; longopts[0].val = 1; longopts[1].name = opt_comm; longopts[1].has_arg = required_argument; longopts[1].flag = NULL; longopts[1].val = 'c'; memset(longopts+2, 0, sizeof(struct option)); setpgid(0, 0); memset(&chld, 0, sizeof(struct sigaction)); chld.sa_flags |= SA_SIGINFO; chld.sa_sigaction = sigchld_handler; error(sigaction(SIGCHLD, &chld, NULL) < 0, -1); signal (SIGINT, SIG_IGN); signal (SIGQUIT, SIG_IGN); signal (SIGTSTP, SIG_IGN); signal (SIGTTIN, SIG_IGN); signal (SIGTTOU, SIG_IGN); opt = getopt_long(argc, argv, shortopts, longopts, NULL); if (opt == 'c') { job = create_job(optarg); run_job(job); job_list(JL_DESTROY); return 0; } if (opt == '?') return -1; if (opt == 1) { printf("%s", doc); return 0; } if (argc > 2) { printf("Too many arguments.\n"); return -1; } if (argc == 2) { int in = open(argv[1], O_RDONLY); fatal_error(in < 0, -1); close(0); fatal_error(dup(in) < 0, -1); close(in); is_script = 1; } if (!is_script) printf("%s", doc); while (!exit_flag) { if (!is_script) { char *raux = getcwd(dir, SLSH_MAX_PATH*sizeof(char)); fatal_error (raux == NULL, -1); printf("@ %s: ", dir); } while (str = read_line(), str == NULL) { if (feof(stdin)) { exit_flag = 1; printf("\n"); break; } else if (!is_script) printf("@ %s: ", dir); } if (!exit_flag) { job = create_job(str); run_job(job); } free(str); str = NULL; } job_list(JL_DESTROY); return 0; }
int run_builtin_cmd(char* cmd[]) /* TODO: make these work correctly inside pipes (io redirection?) */ { int id, i, jobid; JOB_LIST* list; char path[SLSH_MAX_PATH]; char *raux; if (cmd == NULL) return -1; id = get_builtin_cmd(cmd[0]); switch(id) { case CMD_BG: list = job_list(JL_GET); if (cmd[1] != NULL) jobid = atoi(cmd[1]); else jobid = job_list_find_lastmodified_id(); if (jobid >= 0 && jobid <= list->last && list->v[jobid] != NULL) { list->v[jobid]->lastmodified = time(NULL); kill(-list->v[jobid]->pgid, SIGCONT); } else printf("No such job.\n"); break; case CMD_CD: error(chdir(cmd[1]) < 0, -1); raux = getcwd(path, SLSH_MAX_PATH*sizeof(char)); error(raux == NULL, -1); error(path == NULL, -1); error(setenv("PWD", path, 1) < 0, -1); break; case CMD_EXIT: case CMD_QUIT: exit_flag = 1; break; case CMD_FG: list = job_list(JL_GET); if (cmd[1] != NULL) jobid = atoi(cmd[1]); else jobid = job_list_find_lastmodified_id(); if (jobid >= 0 && jobid <= list->last && list->v[jobid] != NULL) { list->v[jobid]->lastmodified = time(NULL); list->v[jobid]->blocking = 1; kill(-list->v[jobid]->pgid, SIGCONT); fg_wait(list->v[jobid]); } else printf("No such job.\n"); break; case CMD_JOBS: list = job_list(JL_GET); for (i = 0; i <= list->last; i++) if (list->v[i] != NULL) printf("[%d] %s\n", i, list->v[i]->name); break; default: return -1; } return 0; }