batch_template.c

Go to the documentation of this file.
00001 
00007 /* $Id: batch_template.c,v 1.11 2008/03/24 19:53:11 seymour Exp $ */
00008 /* $UTK_Copyright: $ */
00009 
00010 #include <stdio.h>
00011 #include <time.h>
00012 #include <string.h>
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <sys/wait.h>
00016 #include <fcntl.h>
00017 #include <errno.h>
00018 #include <unistd.h>
00019 #include <sys/param.h>
00020 
00021 #include "utility.h"
00022 #include "problem.h"
00023 #include "comm_protocol.h"
00024 #include "comm_basics.h"
00025 #include "comm_data.h"
00026 #include "comm_encode.h"
00027 
00028 int gs_problem_service(gs_problem_t *);
00029 
00037 int
00038 service_template(int argc, char *argv[])
00039 {
00040   gs_problem_t *problem;
00041   char *reqdir;
00042   int i;
00043 
00044 #ifdef __CYGWIN__
00045   reqdir = argv[1];
00046 #else
00047   reqdir = strdup(argv[0]);
00048   if(!reqdir)
00049     exit(GS_SVC_ERR_MALLOC);
00050   for(i=strlen(reqdir);i>=0;i--) {
00051     if(reqdir[i] == '/') {
00052       reqdir[i] = 0;
00053       break;
00054     }
00055   }
00056 #endif
00057   fprintf(stderr, "CWD (reqdir) = '%s'\n", reqdir);
00058 
00059   if(chdir(reqdir) < 0) {
00060     fprintf(stderr, "Failed to chdir to reqdir: '%s'\n", reqdir);
00061     exit(GS_SVC_ERR_CHDIR);
00062   }
00063 
00064   problem = (gs_problem_t *) malloc(sizeof(gs_problem_t));
00065 
00066   if(!problem)
00067     exit(GS_SVC_ERR_MALLOC);
00068 
00069   /* Look for the service corresponding to the requested problem */
00070   if(gs_read_problem_from_file(GS_BATCH_XML, problem) < 0) {
00071     ERRPRINTF("Error loading service: '%s'.\n", GS_BATCH_XML);
00072     exit(GS_SVC_ERR_MISSING_XML);
00073   }
00074   else {
00075     char *problemstr = NULL;
00076     int my_dsig, fd = 0;
00077     double service_et;
00078     FILE *xmlfile;
00079 
00080     my_dsig = pvmgetdsig();
00081 
00082     if(gs_restore_input_args_from_file("input", problem, my_dsig, my_dsig) < 0) {
00083       ERRPRINTF("Error receiving input args.\n");
00084       exit(GS_SVC_ERR_RESTORE_ARGS);
00085     }
00086 
00087     service_et = gs_problem_service(problem);
00088 
00089     if(chdir(reqdir) < 0) {
00090       ERRPRINTF("Could not cd back to request directory '%s'.\n", reqdir);
00091       exit(GS_SVC_ERR_CHDIR);
00092     }
00093 
00094     xmlfile = fopen("problem.xml", "w");
00095 
00096     if(!xmlfile) {
00097       ERRPRINTF("Could not create xml file.\n");
00098       exit(GS_SVC_ERR_CREATE_XML);
00099     }
00100 
00101     if(gs_encode_problem(&problemstr, problem) < 0) {
00102       ERRPRINTF("Could not encode problem.\n");
00103       exit(GS_SVC_ERR_PROBLEM_ENC);
00104     }
00105 
00106     fprintf(xmlfile, "%s\n", problemstr);
00107 
00108     fclose(xmlfile);
00109 
00110     fd = open("data", O_WRONLY | O_CREAT, 0600);
00111 
00112     if(fd < 0) {
00113       ERRPRINTF("Could not create data file.\n");
00114       exit(GS_SVC_ERR_CREAT_DATA_FILE);
00115     }
00116 
00117     if(gs_save_output_args_to_file(fd, problem, my_dsig) < 0) {
00118       ERRPRINTF("Error sending output args.\n");
00119       exit(GS_SVC_ERR_IO);
00120     }
00121 
00122     close(fd);
00123 
00124     if(gs_create_timestamp_file(".", "done", service_et) < 0) {
00125       ERRPRINTF("Could not create completion file.\n");
00126       exit(GS_SVC_ERR_COMPLETION_FILE);
00127     }
00128 
00129     exit(0);
00130   }
00131 
00132   ERRPRINTF("Service terminating abnormally\n");
00133   exit(GS_SVC_ERR_UNSPECIFIED);
00134 }