void gw_user_destroy(gw_user_t *user) { int i; if ( user == NULL ) return; gw_log_print("UM",'I',"Removing MADs for user %s (%s).\n", GWNSTR(user->name), GWNSTR(user->dn)); if (user->name != NULL ) free(user->name); if (user->proxy_path != NULL ) free(user->proxy_path); if (user->dn != NULL ) free(user->dn); for (i = 0; i< user->em_mads; i++) gw_em_mad_finalize(&(user->em_mad[i])); for (i = 0; i< user->tm_mads; i++) gw_tm_mad_finalize(&(user->tm_mad[i])); }
int gw_user_init(gw_user_t *user, const char *name, const char *proxy_path) { int i,j; int rc; int proxy_var; FILE *file; char proxy_command[GW_MSG_STRING_LONG], *globus_location; char dn[GW_MSG_STRING_LONG], *pline; if ( user == NULL) return -1; user->name = strdup(name); if (user->name == NULL) return -1; if (strcmp(proxy_path, "") == 0) { #ifdef GWUSERDEBUG gw_log_print("UM",'I',"Unsetting X509_USER_PROXY variable.\n"); #endif unsetenv("X509_USER_PROXY"); proxy_var=0; } else { #ifdef GWUSERDEBUG gw_log_print("UM",'I',"Setting X509_USER_PROXY variable to %s.\n", proxy_path); #endif proxy_var=1; setenv("X509_USER_PROXY", proxy_path, 1); } user->proxy_path = strdup(proxy_path); globus_location = getenv("GLOBUS_LOCATION"); if (gw_conf.multiuser == GW_TRUE) sprintf(proxy_command, "sudo -H -u %s %s/bin/grid-proxy-info -exists", name, globus_location); else sprintf(proxy_command, "grid-proxy-info -exists"); if (system(proxy_command) == 0) { if (gw_conf.multiuser == GW_TRUE) sprintf(proxy_command, "sudo -H -u %s %s/bin/grid-proxy-info -identity", name, globus_location); else sprintf(proxy_command, "grid-proxy-info -identity"); gw_log_print("UM",'I',"Executing command %s\n", proxy_command); file = popen(proxy_command, "r"); if (file != NULL) { while( fgets(dn, sizeof(dn), file) != NULL ) { // Keep looping even if we just expect one line } pclose(file); if (dn != NULL){ pline = strchr(dn, '\n'); if (pline != NULL) *pline = '\0'; user->dn = strdup(dn); gw_log_print("UM",'I',"User proxy info, %s\n", user->dn); } else { gw_log_print("UM",'I',"Error getting identity of user %s.\n", GWNSTR(name)); user->dn = strdup("Unknown"); } } else { gw_log_print("UM",'I',"Error executing grid-proxy-info -identity for user %s.\n", GWNSTR(name)); user->dn = strdup("Unknown"); } } else { gw_log_print("UM",'E',"Error executing grid-proxy-info for user %s. Check sudoers.\n", GWNSTR(name)); user->dn = strdup("Unknown"); } user->active_jobs = 0; user->running_jobs = 0; user->idle = 0; user->em_mads = 0; user->tm_mads = 0; gw_log_print("UM",'I',"Loading execution MADs for user %s (%s).\n", GWNSTR(name), GWNSTR(user->dn)); i = 0; while ( ( i < GW_MAX_MADS ) && (gw_conf.em_mads[i][0] != NULL) ) { rc = gw_em_register_mad(user, gw_conf.em_mads[i][GW_MAD_EM_PATH_INDEX], gw_conf.em_mads[i][GW_MAD_EM_NAME_INDEX], gw_conf.em_mads[i][GW_MAD_EM_ARGS_INDEX], gw_conf.em_mads[i][GW_MAD_EM_RSL_INDEX]); if (rc != 0) { gw_log_print("UM",'E',"Could not load execution MAD %s.\n", gw_conf.em_mads[i][GW_MAD_EM_NAME_INDEX]); for (j = 0; j< user->em_mads ; j++) { gw_log_print("UM",'E',"Removing execution MAD %s.\n", user->em_mad[j].name); gw_em_mad_finalize (&(user->em_mad[j])); } free(user->name); return -1; } i++; } gw_log_print("UM",'I',"Loading transfer MADs for user %s (%s).\n", GWNSTR(name), GWNSTR(user->dn)); i = 0; while ( ( i < GW_MAX_MADS ) && (gw_conf.tm_mads[i][0] != NULL) ) { rc = gw_tm_register_mad(user, gw_conf.tm_mads[i][GW_MAD_TM_PATH_INDEX], gw_conf.tm_mads[i][GW_MAD_TM_NAME_INDEX], gw_conf.tm_mads[i][GW_MAD_TM_ARGS_INDEX]); if ( rc != 0) { gw_log_print("UM",'E',"Could not load transfer MAD %s.\n", gw_conf.tm_mads[i][GW_MAD_TM_NAME_INDEX]); for (j=0; j< user->tm_mads ; j++) { gw_log_print("UM",'E',"Removing transfer MAD %s.\n", user->tm_mad[j].name); gw_tm_mad_finalize (&(user->tm_mad[j])); } for (j=0; j< user->em_mads ; j++) { gw_log_print("UM",'E',"Removing execution MAD %s.\n", user->em_mad[j].name); gw_em_mad_finalize (&(user->em_mad[j])); } free(user->name); return -1; } i++; } gw_log_print("UM",'I',"User %s (%s) registered.\n", GWNSTR(name), GWNSTR(user->dn)); return 0; }
int main (int argc, char **argv ) { int rc; char action[20]; char jid_s[20]; int jid = 0; char contact[500]; char rsl_file[1024]; int status = -1; char info[500]; int end = 0; fd_set in_pipes; int j; char c; char str[4096]; struct timeval tv; int timer_interval = 300; time_t last_timer = 0; time_t the_time; struct timeval t1,t2; double waited; setbuf(stdout,NULL); rc = globus_module_activate(GLOBUS_COMMON_MODULE); if ( rc != GLOBUS_SUCCESS ) return -1; waited = 0; while (!end) { FD_ZERO(&in_pipes); FD_SET (0,&in_pipes); tv.tv_sec = 0; tv.tv_usec = 1000; gettimeofday(&t1, NULL); rc = select(1, &in_pipes, NULL, NULL, &tv); gettimeofday(&t2, NULL); waited += ((t2.tv_sec - t1.tv_sec)*1000000) + (t2.tv_usec - t1.tv_usec); if ( waited > 999 ) { globus_poll(); waited = 0; } if (rc == -1) { exit(-1); } else if (rc == 1) { j = 0; do { rc = read(0, (void *) &c, sizeof(char)); str[j++] = c; } while ( rc > 0 && c != '\n' ); str[j] = '\0'; if (rc <= 0) exit(-1); rc = sscanf(str, "%s %s %s %[^\n]", action, jid_s, contact, rsl_file); if (rc != 4 ) { printf("FAILURE Not all four arguments defined\n"); continue; } jid = atoi(jid_s); if (strcmp(action, "INIT") == 0 ) { status = gw_em_mad_init(jid, info); } else if (strcmp(action, "SUBMIT") == 0 ) { status = gw_em_mad_submit(jid, contact, rsl_file, info); } else if (strcmp(action, "RECOVER") == 0 ) { status = gw_em_mad_recover(jid, contact, info); } else if (strcmp(action, "CANCEL") == 0 ) { status = gw_em_mad_cancel(jid, info); } else if (strcmp(action, "POLL") == 0 ) { status = gw_em_mad_poll(jid, info); } else if (strcmp(action, "FINALIZE") == 0 ) { status = gw_em_mad_finalize(info); end = 1; return 0; } if (status != 0) printf("%s %d FAILURE %s\n", action, jid, info); } the_time = time(NULL); if (the_time - last_timer >= timer_interval) { last_timer = the_time; if (mad.initialized == 1) { status = gw_em_mad_check_credentials(info); if (status != 0) printf("%s %d FAILURE %s\n", action, jid, info); } } } return 0; }
int gw_em_mad_init(gw_em_mad_t * em_mad, const char * exe, const char * name, const char * args, const char * mode, const char * owner) { int length,rc; if ((name == NULL) || (exe == NULL) || (owner == NULL) || (args==NULL)) return -1; length = strlen(gw_conf.gw_location) + strlen(exe) + 6; em_mad->executable = (char *) malloc(sizeof(char)*length); em_mad->args = strdup(args); em_mad->name = strdup(name); em_mad->owner = strdup(owner); em_mad->wrapper_rsl = NULL; em_mad->pre_wrapper_rsl = NULL; if ( mode != NULL ) { em_mad->mode = strdup(mode); if (strcmp(mode, "rsl") == 0) { em_mad->wrapper_rsl = gw_generate_wrapper_rsl; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl; } else if (strcmp(mode, "rsl_nsh") == 0) { em_mad->wrapper_rsl = gw_generate_wrapper_rsl_nsh; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl; } else if (strcmp(mode, "rsl2") == 0) { em_mad->wrapper_rsl = gw_generate_rsl2; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl2; } else if (strcmp(mode, "rsl2_wrapper") == 0) { em_mad->wrapper_rsl = gw_generate_wrapper_rsl2; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl2; } else if (strcmp(mode, "rsl2_nowrapper") == 0) { em_mad->wrapper_rsl = gw_generate_nowrapper_rsl2; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl2; } else if (strcmp(mode, "xrsl") == 0) { em_mad->wrapper_rsl = gw_generate_wrapper_xrsl; em_mad->pre_wrapper_rsl = gw_generate_wrapper_xrsl; } } if (em_mad->wrapper_rsl == NULL ) { gw_log_print("EM",'W',"\tMode %s for execution MAD %s not supported, using rsl2.\n", GWNSTR(mode), GWNSTR(name)); em_mad->wrapper_rsl = gw_generate_rsl2; em_mad->pre_wrapper_rsl = gw_generate_pre_wrapper_rsl2; } sprintf(em_mad->executable, "%s/bin/%s", gw_conf.gw_location, exe); rc = gw_em_mad_start(em_mad); if ( rc == -1) gw_em_mad_finalize(em_mad); return rc; }
int gw_em_mad_init(gw_em_mad_t * em_mad, const char * exe, const char * name, const char * owner) { char buf[50]; char str[GW_EM_MAX_STRING], c; char info[GW_EM_MAX_INFO]; char s_job_id[GW_EM_MAX_JOB_ID]; char result[GW_EM_MAX_RESULT]; char action[GW_EM_MAX_ACTION]; int em_mad_pipe[2], mad_em_pipe[2]; int i, rc; int length; if ((name == NULL) || (exe == NULL) || (owner == NULL)) return -1; length = strlen(gw_conf.gw_location) + strlen(exe) + 6; em_mad->executable = (char *) malloc(sizeof(char)*length); em_mad->name = strdup(name); em_mad->wrapper_rsl = NULL; em_mad->pre_wrapper_rsl = NULL; em_mad->mode = NULL; sprintf(em_mad->executable, "%s/bin/%s", gw_conf.gw_location, exe); if (pipe(em_mad_pipe) == -1 || pipe(mad_em_pipe) == -1) { gw_log_print("EM",'E',"Could not create communication pipes: %s.\n", strerror(errno)); return -1; } em_mad->pid = fork(); switch (em_mad->pid) { case -1: /* Error */ gw_log_print("EM",'E',"Could not fork to start execution MAD %s.\n", name); return -1; case 0: /* Child process (MAD) */ close(em_mad_pipe[1]); close(mad_em_pipe[0]); /* stdin and stdout redirection */ if (dup2(em_mad_pipe[0], 0) != 0 || dup2(mad_em_pipe[1], 1) != 1) { gw_log_print("EM",'E',"Could not duplicate communication pipes: %s\n", strerror(errno)); exit(-1); } close(em_mad_pipe[0]); close(mad_em_pipe[1]); if (gw_conf.multiuser == GW_TRUE) execlp("sudo", "sudo", "-u", owner, em_mad->executable, NULL); else execlp(em_mad->executable, em_mad->executable, NULL); gw_log_print("EM",'E',"Could not execute MAD %s (exec/sudo).\n", em_mad->executable); exit(-1); break; default: /* Parent process (GWD) */ close(em_mad_pipe[0]); close(mad_em_pipe[1]); em_mad->em_mad_pipe = em_mad_pipe[1]; em_mad->mad_em_pipe = mad_em_pipe[0]; fcntl(em_mad->em_mad_pipe, F_SETFD, FD_CLOEXEC); /* Close pipes in other MADs*/ fcntl(em_mad->mad_em_pipe, F_SETFD, FD_CLOEXEC); sprintf(buf, "INIT %i - -\n",gw_conf.number_of_jobs); write(em_mad->em_mad_pipe, buf, strlen(buf)); i = 0; do { rc = read(em_mad->mad_em_pipe, (void *) &c, sizeof(char)); str[i++] = c; } while ( rc > 0 && c != '\n' && c != '\0'); str[i] = '\0'; if (rc <= 0) { gw_log_print("EM",'E',"\tInitialization failure, reading from MAD %s.\n", em_mad->name); gw_em_mad_finalize(em_mad); return -1; } sscanf(str,"%s %s %s %[^\n]", action, s_job_id, result, info); #ifdef GWEMDEBUG gw_log_print("EM",'D',"MAD message received:\"%s %s %s %s\".\n", action, s_job_id, result, info); #endif if (strcmp(action, "INIT") == 0) { if (strcmp(result, "FAILURE") == 0) { gw_log_print("EM",'E',"\tInitialization failure of MAD %s.\n", em_mad->name); gw_em_mad_finalize(em_mad); return -1; } } else { gw_log_print("EM",'E',"\tInitialization failure, bad response from MAD %s.\n", em_mad->name); gw_em_mad_finalize(em_mad); return -1; } break; } return 0; }