source: trunk/pbs_drmaa/util.c @ 1

Revision 1, 7.9 KB checked in by mmamonski, 13 years ago (diff)

Torque/PBS DRMAA initial commit

Line 
1/* $Id: util.c 323 2010-09-21 21:31:29Z mmatloka $ */
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
39static char rcsid[]
40#       ifdef __GNUC__
41                __attribute__ ((unused))
42#       endif
43        = "$Id: util.c 323 2010-09-21 21:31:29Z mmatloka $";
44#endif
45
46
47void
48pbsdrmaa_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
63void
64pbsdrmaa_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
78void
79pbsdrmaa_exc_raise_pbs( const char *function )
80{
81        int _pbs_errno;
82        int fsd_errno;
83        const char *message = NULL;
84
85        _pbs_errno = pbs_errno;
86        /*
87         * Gathering error messages differ between PBS forks.
88         * - OpenPBS - ...
89         * - Torque - pbse_to_txt takes PBS error code (stored in pbs_errno)
90         *  and returns corresponding error message.
91         * - PBS Pro - stores errno of last operation inside pbs_errno variable;
92         *  pbse_to_txt always return NULL.
93         * All of them define pbs_geterrmsg which returns last error message
94         * for given connection.
95         */
96        /* XXX: PBSPro has some link problems with pbse_to_txt function */
97#if 1
98        message = pbse_to_txt( pbs_errno );
99#else
100        message = "pbs error";
101#endif
102
103
104        fsd_errno = pbsdrmaa_map_pbs_errno( _pbs_errno );
105        fsd_log_error((
106                                "call to %s returned with error %d:%s mapped to %d:%s",
107                                function,
108                                _pbs_errno, message,
109                                fsd_errno, fsd_strerror(fsd_errno)
110                                ));
111        fsd_exc_raise_fmt( fsd_errno, "%s: %s", function, message );
112}
113
114
115/** Maps PBS error code into DMRAA code. */
116int
117pbsdrmaa_map_pbs_errno( int _pbs_errno )
118{
119        fsd_log_enter(( "(pbs_errno=%d)", _pbs_errno ));
120        switch( _pbs_errno )
121         {
122                case PBSE_NONE:  /* no error */
123                        return FSD_ERRNO_SUCCESS;
124                case PBSE_UNKJOBID:      /* Unknown Job Identifier */
125                        return FSD_DRMAA_ERRNO_INVALID_JOB;
126                case PBSE_NOATTR: /* Undefined Attribute */
127                case PBSE_ATTRRO: /* attempt to set READ ONLY attribute */
128                case PBSE_IVALREQ:  /* Invalid request */
129                case PBSE_UNKREQ:  /* Unknown batch request */
130                        return FSD_ERRNO_INTERNAL_ERROR;
131                case PBSE_PERM:  /* No permission */
132                case PBSE_BADHOST:  /* access from host not allowed */
133                        return FSD_ERRNO_AUTHZ_FAILURE;
134                case PBSE_JOBEXIST:  /* job already exists */
135                case PBSE_SVRDOWN:  /* req rejected -server shutting down */
136                case PBSE_EXECTHERE:  /* cannot execute there */
137                case PBSE_NOSUP:  /* Feature/function not supported */
138                case PBSE_EXCQRESC:  /* Job exceeds Queue resource limits */
139                case PBSE_QUENODFLT:  /* No Default Queue Defined */
140                case PBSE_NOTSNODE:  /* no time-shared nodes */
141                        return FSD_ERRNO_DENIED_BY_DRM;
142                case PBSE_SYSTEM:  /* system error occurred */
143                case PBSE_INTERNAL:  /* internal server error occurred */
144                case PBSE_REGROUTE:  /* parent job of dependent in rte que */
145                case PBSE_UNKSIG:  /* unknown signal name */
146                        return FSD_ERRNO_INTERNAL_ERROR;
147                case PBSE_BADATVAL:  /* bad attribute value */
148                case PBSE_BADATLST:  /* Bad attribute list structure */
149                case PBSE_BADUSER:  /* Bad user - no password entry */
150                case PBSE_BADGRP:  /* Bad Group specified */
151                case PBSE_BADACCT:  /* Bad Account attribute value */
152                case PBSE_UNKQUE:  /* Unknown queue name */
153                case PBSE_UNKRESC:  /* Unknown resource */
154                case PBSE_UNKNODEATR:  /* node-attribute not recognized */
155                case PBSE_BADNDATVAL:  /* Bad node-attribute value */
156                case PBSE_BADDEPEND:  /* Invalid dependency */
157                case PBSE_DUPLIST:  /* Duplicate entry in List */
158                        return FSD_ERRNO_INVALID_VALUE;
159                case PBSE_MODATRRUN:  /* Cannot modify attrib in run state */
160                case PBSE_BADSTATE:  /* request invalid for job state */
161                case PBSE_BADCRED:  /* Invalid Credential in request */
162                case PBSE_EXPIRED:  /* Expired Credential in request */
163                case PBSE_QUNOENB:  /* Queue not enabled */
164                        return FSD_ERRNO_INTERNAL_ERROR;
165                case PBSE_QACESS:  /* No access permission for queue */
166                        return FSD_ERRNO_AUTHZ_FAILURE;
167                case PBSE_HOPCOUNT:  /* Max hop count exceeded */
168                case PBSE_QUEEXIST:  /* Queue already exists */
169                case PBSE_ATTRTYPE:  /* incompatable queue attribute type */
170                        return FSD_ERRNO_INTERNAL_ERROR;
171#               ifdef PBSE_QUEBUSY
172                case PBSE_QUEBUSY:  /* Queue Busy (not empty) */
173#               endif
174                case PBSE_MAXQUED:  /* Max number of jobs in queue */
175                case PBSE_NOCONNECTS:  /* No free connections */
176                case PBSE_TOOMANY:  /* Too many submit retries */
177                case PBSE_RESCUNAV:  /* Resources temporarily unavailable */
178                        return FSD_ERRNO_TRY_LATER;
179                case 111:
180                case PBSE_PROTOCOL:  /* Protocol (ASN.1) error */
181                case PBSE_DISPROTO:  /* Bad DIS based Request Protocol */
182                        return FSD_ERRNO_DRM_COMMUNICATION_FAILURE;
183#if 0
184                case PBSE_QUENBIG:  /* Queue name too long */
185                case PBSE_QUENOEN:  /* Cannot enable queue,needs add def */
186                case PBSE_NOSERVER:  /* No server to connect to */
187                case PBSE_NORERUN:  /* Job Not Rerunnable */
188                case PBSE_ROUTEREJ:  /* Route rejected by all destinations */
189                case PBSE_ROUTEEXPD:  /* Time in Route Queue Expired */
190                case PBSE_MOMREJECT:  /* Request to MOM failed */
191                case PBSE_BADSCRIPT:  /* (qsub) cannot access script file */
192                case PBSE_STAGEIN:  /* Stage In of files failed */
193                case PBSE_CKPBSY:  /* Checkpoint Busy, may be retries */
194                case PBSE_EXLIMIT:  /* Limit exceeds allowable */
195                case PBSE_ALRDYEXIT:  /* Job already in exit state */
196                case PBSE_NOCOPYFILE:  /* Job files not copied */
197                case PBSE_CLEANEDOUT:  /* unknown job id after clean init */
198                case PBSE_NOSYNCMSTR:  /* No Master in Sync Set */
199                case PBSE_SISREJECT:  /* sister rejected */
200                case PBSE_SISCOMM:  /* sister could not communicate */
201                case PBSE_CKPSHORT:  /* not all tasks could checkpoint */
202                case PBSE_UNKNODE:  /* Named node is not in the list */
203                case PBSE_NONODES:  /* Server has no node list */
204                case PBSE_NODENBIG:  /* Node name is too big */
205                case PBSE_NODEEXIST:  /* Node name already exists */
206                case PBSE_MUTUALEX:  /* State values are mutually exclusive */
207                case PBSE_GMODERR:  /* Error(s) during global modification of nodes */
208                case PBSE_NORELYMOM:  /* could not contact Mom */
209                        return FSD_ERRNO_INTERNAL_ERROR;
210#endif
211                default:
212                        return FSD_ERRNO_INTERNAL_ERROR;
213         }
214}
215
216
217char *
218pbsdrmaa_write_tmpfile( const char *content, size_t len )
219{
220        static const char *tmpfile_template = "/tmp/pbs_drmaa.XXXXXX";
221        char *volatile name = NULL;
222        volatile int fd = -1;
223
224        fsd_log_enter(( "" ));
225
226        TRY
227         {
228                name = fsd_strdup( tmpfile_template );
229                fd = mkstemp( name );
230                if( fd < 0 )
231                        fsd_exc_raise_sys(0);
232                while( len > 0 )
233                 {
234                        size_t written = write( fd, content, len );
235                        if( written != (size_t)-1 )
236                         {
237                                content += written;
238                                len -= written;
239                         }
240                        else
241                                fsd_exc_raise_sys(0);
242                 }
243         }
244        EXCEPT_DEFAULT
245         { fsd_free( name ); }
246        FINALLY
247         {
248                if( fd >= 0 )
249                 {
250                        if( close( fd ) )
251                                fsd_exc_raise_sys(0);
252                 }
253         }
254        END_TRY
255
256        fsd_log_return(( "=%s", name ));
257        return name;
258}
259
260
Note: See TracBrowser for help on using the repository browser.