~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/batch_job_cluster.c

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2012-03-30 12:40:01 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120330124001-ze0lhxm5uwq2e3mo
Tags: 3.4.2-2
Added patch to handle a missing CFLAGS variable in Python's sysconfig
report (Closes: #661658).

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include "debug.h"
4
4
#include "stringtools.h"
5
5
#include "process.h"
 
6
#include "xmalloc.h"
6
7
 
7
8
#include <stdlib.h>
8
9
#include <stdio.h>
71
72
 
72
73
static int setup_batch_wrapper(const char *sysname)
73
74
{
74
 
        FILE *file;
75
 
        char wrapperfile[BATCH_JOB_LINE_MAX];
76
 
        
77
 
        sprintf(wrapperfile, "%s.wrapper", sysname);
 
75
        char *wrapperfile = string_format("%s.wrapper", sysname);
78
76
 
79
77
        if(access(wrapperfile, R_OK | X_OK) == 0)
80
78
                return 0;
81
79
 
82
 
        file = fopen(wrapperfile, "w");
83
 
        if(!file)
 
80
        FILE *file = fopen(wrapperfile, "w");
 
81
        if(!file) {
 
82
                free(wrapperfile);
84
83
                return -1;
 
84
        }
85
85
 
86
86
        fprintf(file, "#!/bin/sh\n");
87
87
        fprintf(file, "logfile=%s.status.${JOB_ID}\n", sysname);
99
99
 
100
100
        chmod(wrapperfile, 0755);
101
101
 
 
102
        free(wrapperfile);
 
103
 
102
104
        return 0;
103
105
}
104
106
 
105
107
 
106
108
batch_job_id_t batch_job_submit_simple_cluster(struct batch_queue * q, const char *cmd, const char *extra_input_files, const char *extra_output_files)
107
109
{
108
 
        char line[BATCH_JOB_LINE_MAX];
109
 
        char name[BATCH_JOB_LINE_MAX];
110
110
        batch_job_id_t jobid;
111
111
        struct batch_job_info *info;
112
112
 
113
 
        FILE *file;
114
 
 
115
113
        if(setup_batch_wrapper(cluster_name) < 0)
116
114
                return -1;
117
115
 
118
 
        strcpy(name, cmd);
119
 
        char *s = strchr(name, ' ');
120
 
        if(s)
121
 
                *s = 0;
122
 
 
123
 
        sprintf(line, "%s %s '%s' %s %s.wrapper \"%s\"", cluster_submit_cmd, cluster_options, string_basename(name), q->options_text ? q->options_text : "", cluster_name, cmd);
124
 
 
125
 
        debug(D_DEBUG, "%s", line);
126
 
 
127
 
        file = popen(line, "r");
 
116
        char *name = xstrdup(cmd);
 
117
        {
 
118
                char *s = strchr(name, ' ');
 
119
                if(s)
 
120
                        *s = 0;
 
121
        }
 
122
        char *command = string_format("%s %s '%s' %s %s.wrapper \"%s\"", cluster_submit_cmd, cluster_options, string_basename(name), q->options_text ? q->options_text : "", cluster_name, cmd);
 
123
        free(name);
 
124
 
 
125
        debug(D_DEBUG, "%s", command);
 
126
 
 
127
        FILE *file = popen(command, "r");
 
128
        free(command);
128
129
        if(!file) {
129
130
                debug(D_DEBUG, "couldn't submit job: %s", strerror(errno));
130
131
                return -1;
131
132
        }
132
133
 
133
 
        line[0] = 0;
 
134
        char line[BATCH_JOB_LINE_MAX] = "";
134
135
        while(fgets(line, sizeof(line), file)) {
135
136
                if(sscanf(line, "Your job %d", &jobid) == 1) {
136
137
                        debug(D_DEBUG, "job %d submitted", jobid);
154
155
 
155
156
batch_job_id_t batch_job_submit_cluster(struct batch_queue * q, const char *cmd, const char *args, const char *infile, const char *outfile, const char *errfile, const char *extra_input_files, const char *extra_output_files)
156
157
{
157
 
        char command[BATCH_JOB_LINE_MAX];
158
 
 
159
 
        sprintf(command, "%s %s", cmd, args);
160
 
 
161
 
        if(infile)
162
 
                sprintf(&command[strlen(command)], " <%s", infile);
163
 
        if(outfile)
164
 
                sprintf(&command[strlen(command)], " >%s", outfile);
165
 
        if(errfile)
166
 
                sprintf(&command[strlen(command)], " 2>%s", errfile);
167
 
 
168
 
        return batch_job_submit_simple_cluster(q, command, extra_input_files, extra_output_files);
 
158
        char *command = string_format("%s %s", cmd, args);
 
159
        if (infile) {
 
160
                char *new = string_format("%s <%s", command, infile);
 
161
                free(command);
 
162
                command = new;
 
163
        }
 
164
        if (outfile) {
 
165
                char *new = string_format("%s >%s", command, outfile);
 
166
                free(command);
 
167
                command = new;
 
168
        }
 
169
        if (errfile) {
 
170
                char *new = string_format("%s 2>%s", command, errfile);
 
171
                free(command);
 
172
                command = new;
 
173
        }
 
174
 
 
175
        batch_job_id_t status = batch_job_submit_simple_cluster(q, command, extra_input_files, extra_output_files);
 
176
        free(command);
 
177
        return status;
169
178
}
170
179
 
171
180
batch_job_id_t batch_job_wait_cluster(struct batch_queue * q, struct batch_job_info * info_out, time_t stoptime)
172
181
{
173
182
        struct batch_job_info *info;
174
183
        batch_job_id_t jobid;
175
 
        FILE *file;
176
 
        char statusfile[BATCH_JOB_LINE_MAX];
177
 
        char line[BATCH_JOB_LINE_MAX];
178
184
        int t, c;
179
185
 
180
186
        while(1) {
182
188
                itable_firstkey(q->job_table);
183
189
                while(itable_nextkey(q->job_table, &ujobid, (void **) &info)) {
184
190
                        jobid = ujobid;
185
 
                        sprintf(statusfile, "%s.status.%d", cluster_name, jobid);
186
 
                        file = fopen(statusfile, "r");
 
191
                        char *statusfile = string_format("%s.status.%d", cluster_name, jobid);
 
192
                        FILE *file = fopen(statusfile, "r");
187
193
                        if(file) {
 
194
                                char line[BATCH_JOB_LINE_MAX];
188
195
                                while(fgets(line, sizeof(line), file)) {
189
196
                                        if(sscanf(line, "start %d", &t)) {
190
197
                                                info->started = t;
204
211
                                        info = itable_remove(q->job_table, jobid);
205
212
                                        *info_out = *info;
206
213
                                        free(info);
 
214
                                        free(statusfile);
207
215
                                        return jobid;
208
216
                                }
209
217
                        }
 
218
                        free(statusfile);
210
219
                }
211
220
 
212
221
                if(itable_size(q->job_table) <= 0)
226
235
 
227
236
int batch_job_remove_cluster(struct batch_queue *q, batch_job_id_t jobid)
228
237
{
229
 
        char line[BATCH_JOB_LINE_MAX];
230
238
        struct batch_job_info *info;
231
239
 
232
240
        info = itable_lookup(q->job_table, jobid);
240
248
        info->exited_normally = 0;
241
249
        info->exit_signal = 1;
242
250
 
243
 
        sprintf(line, "%s %d", cluster_remove_cmd, jobid);
244
 
        system(line);
 
251
        char *command = string_format("%s %d", cluster_remove_cmd, jobid);
 
252
        system(command);
 
253
        free(command);
245
254
 
246
255
        return 1;
247
256
}