source: trunk/pbs_drmaa/util.c @ 85

Revision 85, 8.3 KB checked in by mmamonski, 11 years ago (diff)

PBS DRMAA autoclose connection

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