1 | /* $Id$ */ |
---|
2 | /* |
---|
3 | * FedStage DRMAA for PBS Pro |
---|
4 | * Copyright (C) 2006-2007 FedStage Systems |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @file pbs_drmaa/util.c |
---|
22 | * PBS DRMAA utilities. |
---|
23 | */ |
---|
24 | |
---|
25 | #ifdef HAVE_CONFIG_H |
---|
26 | # include <config.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdlib.h> |
---|
30 | #include <string.h> |
---|
31 | #include <unistd.h> |
---|
32 | |
---|
33 | #include <drmaa_utils/common.h> |
---|
34 | #include <pbs_drmaa/util.h> |
---|
35 | #include <pbs_error.h> |
---|
36 | #include <pbs_ifl.h> |
---|
37 | |
---|
38 | #ifndef lint |
---|
39 | static char rcsid[] |
---|
40 | # ifdef __GNUC__ |
---|
41 | __attribute__ ((unused)) |
---|
42 | # endif |
---|
43 | = "$Id$"; |
---|
44 | #endif |
---|
45 | |
---|
46 | |
---|
47 | void |
---|
48 | pbsdrmaa_dump_attrl( const struct attrl *attribute_list, const char *prefix ) |
---|
49 | { |
---|
50 | const struct attrl *i; |
---|
51 | |
---|
52 | if( prefix == NULL ) |
---|
53 | prefix = ""; |
---|
54 | for( i = attribute_list; i != NULL; i = i->next ) |
---|
55 | fsd_log_debug(( "\n %s %s%s%s=%s", |
---|
56 | prefix, i->name, |
---|
57 | i->resource ? "." : "", i->resource ? i->resource : "", |
---|
58 | i->value |
---|
59 | )); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | void |
---|
64 | pbsdrmaa_free_attrl( struct attrl *attr ) |
---|
65 | { |
---|
66 | while( attr != NULL ) |
---|
67 | { |
---|
68 | struct attrl *p = attr; |
---|
69 | attr = attr->next; |
---|
70 | fsd_free( p->name ); |
---|
71 | fsd_free( p->value ); |
---|
72 | fsd_free( p->resource ); |
---|
73 | fsd_free( p ); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | struct attrl * |
---|
78 | pbsdrmaa_add_attr( struct attrl *head, const char *name, const char *value) |
---|
79 | { |
---|
80 | struct attrl *p = NULL; |
---|
81 | char *resource = NULL; |
---|
82 | |
---|
83 | fsd_malloc( p, struct attrl ); |
---|
84 | memset( p, 0, sizeof(struct attrl) ); |
---|
85 | |
---|
86 | resource = strchr( name, '.' ); |
---|
87 | |
---|
88 | if( resource ) |
---|
89 | { |
---|
90 | p->name = fsd_strndup( name, resource - name ); |
---|
91 | p->resource = fsd_strdup( resource+1 ); |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | p->name = fsd_strdup( name ); |
---|
96 | } |
---|
97 | |
---|
98 | p->value = fsd_strdup(value); |
---|
99 | p->op = SET; |
---|
100 | |
---|
101 | fsd_log_debug(("set attr: %s = %s", name, value)); |
---|
102 | |
---|
103 | if (head) |
---|
104 | p->next = head; |
---|
105 | else |
---|
106 | p->next = NULL; |
---|
107 | |
---|
108 | return p; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void |
---|
113 | pbsdrmaa_exc_raise_pbs( const char *function ) |
---|
114 | { |
---|
115 | int _pbs_errno; |
---|
116 | int fsd_errno; |
---|
117 | const char *message = NULL; |
---|
118 | |
---|
119 | _pbs_errno = pbs_errno; |
---|
120 | |
---|
121 | #ifndef PBS_PROFESSIONAL_NO_LOG |
---|
122 | message = pbse_to_txt( pbs_errno ); |
---|
123 | #else |
---|
124 | message = "pbs error"; |
---|
125 | #endif |
---|
126 | |
---|
127 | fsd_errno = pbsdrmaa_map_pbs_errno( _pbs_errno ); |
---|
128 | fsd_log_error(( |
---|
129 | "call to %s returned with error %d:%s mapped to %d:%s", |
---|
130 | function, |
---|
131 | _pbs_errno, message, |
---|
132 | fsd_errno, fsd_strerror(fsd_errno) |
---|
133 | )); |
---|
134 | fsd_exc_raise_fmt( fsd_errno, "%s: %s", function, message ); |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | /** Maps PBS error code into DMRAA code. */ |
---|
139 | int |
---|
140 | pbsdrmaa_map_pbs_errno( int _pbs_errno ) |
---|
141 | { |
---|
142 | fsd_log_enter(( "(pbs_errno=%d)", _pbs_errno )); |
---|
143 | switch( _pbs_errno ) |
---|
144 | { |
---|
145 | case PBSE_NONE: /* no error */ |
---|
146 | return FSD_ERRNO_SUCCESS; |
---|
147 | case PBSE_UNKJOBID: /* Unknown Job Identifier */ |
---|
148 | return FSD_DRMAA_ERRNO_INVALID_JOB; |
---|
149 | case PBSE_NOATTR: /* Undefined Attribute */ |
---|
150 | case PBSE_ATTRRO: /* attempt to set READ ONLY attribute */ |
---|
151 | case PBSE_IVALREQ: /* Invalid request */ |
---|
152 | case PBSE_UNKREQ: /* Unknown batch request */ |
---|
153 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
154 | case PBSE_PERM: /* No permission */ |
---|
155 | case PBSE_BADHOST: /* access from host not allowed */ |
---|
156 | return FSD_ERRNO_AUTHZ_FAILURE; |
---|
157 | case PBSE_JOBEXIST: /* job already exists */ |
---|
158 | case PBSE_SVRDOWN: /* req rejected -server shutting down */ |
---|
159 | case PBSE_EXECTHERE: /* cannot execute there */ |
---|
160 | case PBSE_NOSUP: /* Feature/function not supported */ |
---|
161 | case PBSE_EXCQRESC: /* Job exceeds Queue resource limits */ |
---|
162 | case PBSE_QUENODFLT: /* No Default Queue Defined */ |
---|
163 | case PBSE_NOTSNODE: /* no time-shared nodes */ |
---|
164 | return FSD_ERRNO_DENIED_BY_DRM; |
---|
165 | case PBSE_SYSTEM: /* system error occurred */ |
---|
166 | case PBSE_INTERNAL: /* internal server error occurred */ |
---|
167 | case PBSE_REGROUTE: /* parent job of dependent in rte que */ |
---|
168 | case PBSE_UNKSIG: /* unknown signal name */ |
---|
169 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
170 | case PBSE_BADATVAL: /* bad attribute value */ |
---|
171 | case PBSE_BADATLST: /* Bad attribute list structure */ |
---|
172 | case PBSE_BADUSER: /* Bad user - no password entry */ |
---|
173 | case PBSE_BADGRP: /* Bad Group specified */ |
---|
174 | case PBSE_BADACCT: /* Bad Account attribute value */ |
---|
175 | case PBSE_UNKQUE: /* Unknown queue name */ |
---|
176 | case PBSE_UNKRESC: /* Unknown resource */ |
---|
177 | case PBSE_UNKNODEATR: /* node-attribute not recognized */ |
---|
178 | case PBSE_BADNDATVAL: /* Bad node-attribute value */ |
---|
179 | case PBSE_BADDEPEND: /* Invalid dependency */ |
---|
180 | case PBSE_DUPLIST: /* Duplicate entry in List */ |
---|
181 | return FSD_ERRNO_INVALID_VALUE; |
---|
182 | case PBSE_MODATRRUN: /* Cannot modify attrib in run state */ |
---|
183 | case PBSE_BADSTATE: /* request invalid for job state */ |
---|
184 | case PBSE_BADCRED: /* Invalid Credential in request */ |
---|
185 | case PBSE_EXPIRED: /* Expired Credential in request */ |
---|
186 | case PBSE_QUNOENB: /* Queue not enabled */ |
---|
187 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
188 | case PBSE_QACESS: /* No access permission for queue */ |
---|
189 | return FSD_ERRNO_AUTHZ_FAILURE; |
---|
190 | case PBSE_HOPCOUNT: /* Max hop count exceeded */ |
---|
191 | case PBSE_QUEEXIST: /* Queue already exists */ |
---|
192 | case PBSE_ATTRTYPE: /* incompatable queue attribute type */ |
---|
193 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
194 | # ifdef PBSE_QUEBUSY |
---|
195 | case PBSE_QUEBUSY: /* Queue Busy (not empty) */ |
---|
196 | # endif |
---|
197 | case PBSE_MAXQUED: /* Max number of jobs in queue */ |
---|
198 | case PBSE_NOCONNECTS: /* No free connections */ |
---|
199 | case PBSE_TOOMANY: /* Too many submit retries */ |
---|
200 | case PBSE_RESCUNAV: /* Resources temporarily unavailable */ |
---|
201 | return FSD_ERRNO_TRY_LATER; |
---|
202 | case 111: |
---|
203 | case PBSE_PROTOCOL: /* Protocol (ASN.1) error */ |
---|
204 | case PBSE_DISPROTO: /* Bad DIS based Request Protocol */ |
---|
205 | return FSD_ERRNO_DRM_COMMUNICATION_FAILURE; |
---|
206 | #if 0 |
---|
207 | case PBSE_QUENBIG: /* Queue name too long */ |
---|
208 | case PBSE_QUENOEN: /* Cannot enable queue,needs add def */ |
---|
209 | case PBSE_NOSERVER: /* No server to connect to */ |
---|
210 | case PBSE_NORERUN: /* Job Not Rerunnable */ |
---|
211 | case PBSE_ROUTEREJ: /* Route rejected by all destinations */ |
---|
212 | case PBSE_ROUTEEXPD: /* Time in Route Queue Expired */ |
---|
213 | case PBSE_MOMREJECT: /* Request to MOM failed */ |
---|
214 | case PBSE_BADSCRIPT: /* (qsub) cannot access script file */ |
---|
215 | case PBSE_STAGEIN: /* Stage In of files failed */ |
---|
216 | case PBSE_CKPBSY: /* Checkpoint Busy, may be retries */ |
---|
217 | case PBSE_EXLIMIT: /* Limit exceeds allowable */ |
---|
218 | case PBSE_ALRDYEXIT: /* Job already in exit state */ |
---|
219 | case PBSE_NOCOPYFILE: /* Job files not copied */ |
---|
220 | case PBSE_CLEANEDOUT: /* unknown job id after clean init */ |
---|
221 | case PBSE_NOSYNCMSTR: /* No Master in Sync Set */ |
---|
222 | case PBSE_SISREJECT: /* sister rejected */ |
---|
223 | case PBSE_SISCOMM: /* sister could not communicate */ |
---|
224 | case PBSE_CKPSHORT: /* not all tasks could checkpoint */ |
---|
225 | case PBSE_UNKNODE: /* Named node is not in the list */ |
---|
226 | case PBSE_NONODES: /* Server has no node list */ |
---|
227 | case PBSE_NODENBIG: /* Node name is too big */ |
---|
228 | case PBSE_NODEEXIST: /* Node name already exists */ |
---|
229 | case PBSE_MUTUALEX: /* State values are mutually exclusive */ |
---|
230 | case PBSE_GMODERR: /* Error(s) during global modification of nodes */ |
---|
231 | case PBSE_NORELYMOM: /* could not contact Mom */ |
---|
232 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
233 | #endif |
---|
234 | default: |
---|
235 | return FSD_ERRNO_INTERNAL_ERROR; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | |
---|
240 | char * |
---|
241 | pbsdrmaa_write_tmpfile( const char *content, size_t len ) |
---|
242 | { |
---|
243 | static const char *tmpfile_template = "/tmp/pbs_drmaa.XXXXXX"; |
---|
244 | char *volatile name = NULL; |
---|
245 | volatile int fd = -1; |
---|
246 | |
---|
247 | fsd_log_enter(( "" )); |
---|
248 | |
---|
249 | TRY |
---|
250 | { |
---|
251 | name = fsd_strdup( tmpfile_template ); |
---|
252 | fd = mkstemp( name ); |
---|
253 | if( fd < 0 ) |
---|
254 | fsd_exc_raise_sys(0); |
---|
255 | while( len > 0 ) |
---|
256 | { |
---|
257 | size_t written = write( fd, content, len ); |
---|
258 | if( written != (size_t)-1 ) |
---|
259 | { |
---|
260 | content += written; |
---|
261 | len -= written; |
---|
262 | } |
---|
263 | else |
---|
264 | fsd_exc_raise_sys(0); |
---|
265 | } |
---|
266 | } |
---|
267 | EXCEPT_DEFAULT |
---|
268 | { fsd_free( name ); } |
---|
269 | FINALLY |
---|
270 | { |
---|
271 | if( fd >= 0 ) |
---|
272 | { |
---|
273 | if( close( fd ) ) |
---|
274 | fsd_exc_raise_sys(0); |
---|
275 | } |
---|
276 | } |
---|
277 | END_TRY |
---|
278 | |
---|
279 | fsd_log_return(( "=%s", name )); |
---|
280 | return name; |
---|
281 | } |
---|
282 | |
---|
283 | |
---|