source: trunk/pbs_drmaa/util.c @ 16

Revision 16, 8.8 KB checked in by mmamonski, 13 years ago (diff)

painfull copy/paste bug

  • 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
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$";
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#ifndef PBS_PROFESSIONAL_NO_LOG
88        message = pbse_to_txt( pbs_errno );
89#else
90        message = "pbs error";
91#endif
92
93        fsd_errno = pbsdrmaa_map_pbs_errno( _pbs_errno );
94        fsd_log_error((
95                                "call to %s returned with error %d:%s mapped to %d:%s",
96                                function,
97                                _pbs_errno, message,
98                                fsd_errno, fsd_strerror(fsd_errno)
99                                ));
100        fsd_exc_raise_fmt( fsd_errno, "%s: %s", function, message );
101}
102
103
104/** Maps PBS error code into DMRAA code. */
105int
106pbsdrmaa_map_pbs_errno( int _pbs_errno )
107{
108        fsd_log_enter(( "(pbs_errno=%d)", _pbs_errno ));
109        switch( _pbs_errno )
110         {
111                case PBSE_NONE:  /* no error */
112                        return FSD_ERRNO_SUCCESS;
113                case PBSE_UNKJOBID:      /* Unknown Job Identifier */
114                        return FSD_DRMAA_ERRNO_INVALID_JOB;
115                case PBSE_NOATTR: /* Undefined Attribute */
116                case PBSE_ATTRRO: /* attempt to set READ ONLY attribute */
117                case PBSE_IVALREQ:  /* Invalid request */
118                case PBSE_UNKREQ:  /* Unknown batch request */
119                        return FSD_ERRNO_INTERNAL_ERROR;
120                case PBSE_PERM:  /* No permission */
121                case PBSE_BADHOST:  /* access from host not allowed */
122                        return FSD_ERRNO_AUTHZ_FAILURE;
123                case PBSE_JOBEXIST:  /* job already exists */
124                case PBSE_SVRDOWN:  /* req rejected -server shutting down */
125                case PBSE_EXECTHERE:  /* cannot execute there */
126                case PBSE_NOSUP:  /* Feature/function not supported */
127                case PBSE_EXCQRESC:  /* Job exceeds Queue resource limits */
128                case PBSE_QUENODFLT:  /* No Default Queue Defined */
129                case PBSE_NOTSNODE:  /* no time-shared nodes */
130                        return FSD_ERRNO_DENIED_BY_DRM;
131                case PBSE_SYSTEM:  /* system error occurred */
132                case PBSE_INTERNAL:  /* internal server error occurred */
133                case PBSE_REGROUTE:  /* parent job of dependent in rte que */
134                case PBSE_UNKSIG:  /* unknown signal name */
135                        return FSD_ERRNO_INTERNAL_ERROR;
136                case PBSE_BADATVAL:  /* bad attribute value */
137                case PBSE_BADATLST:  /* Bad attribute list structure */
138                case PBSE_BADUSER:  /* Bad user - no password entry */
139                case PBSE_BADGRP:  /* Bad Group specified */
140                case PBSE_BADACCT:  /* Bad Account attribute value */
141                case PBSE_UNKQUE:  /* Unknown queue name */
142                case PBSE_UNKRESC:  /* Unknown resource */
143                case PBSE_UNKNODEATR:  /* node-attribute not recognized */
144                case PBSE_BADNDATVAL:  /* Bad node-attribute value */
145                case PBSE_BADDEPEND:  /* Invalid dependency */
146                case PBSE_DUPLIST:  /* Duplicate entry in List */
147                        return FSD_ERRNO_INVALID_VALUE;
148                case PBSE_MODATRRUN:  /* Cannot modify attrib in run state */
149                case PBSE_BADSTATE:  /* request invalid for job state */
150                case PBSE_BADCRED:  /* Invalid Credential in request */
151                case PBSE_EXPIRED:  /* Expired Credential in request */
152                case PBSE_QUNOENB:  /* Queue not enabled */
153                        return FSD_ERRNO_INTERNAL_ERROR;
154                case PBSE_QACESS:  /* No access permission for queue */
155                        return FSD_ERRNO_AUTHZ_FAILURE;
156                case PBSE_HOPCOUNT:  /* Max hop count exceeded */
157                case PBSE_QUEEXIST:  /* Queue already exists */
158                case PBSE_ATTRTYPE:  /* incompatable queue attribute type */
159                        return FSD_ERRNO_INTERNAL_ERROR;
160#               ifdef PBSE_QUEBUSY
161                case PBSE_QUEBUSY:  /* Queue Busy (not empty) */
162#               endif
163                case PBSE_MAXQUED:  /* Max number of jobs in queue */
164                case PBSE_NOCONNECTS:  /* No free connections */
165                case PBSE_TOOMANY:  /* Too many submit retries */
166                case PBSE_RESCUNAV:  /* Resources temporarily unavailable */
167                        return FSD_ERRNO_TRY_LATER;
168                case 111:
169                case PBSE_PROTOCOL:  /* Protocol (ASN.1) error */
170                case PBSE_DISPROTO:  /* Bad DIS based Request Protocol */
171                        return FSD_ERRNO_DRM_COMMUNICATION_FAILURE;
172#if 0
173                case PBSE_QUENBIG:  /* Queue name too long */
174                case PBSE_QUENOEN:  /* Cannot enable queue,needs add def */
175                case PBSE_NOSERVER:  /* No server to connect to */
176                case PBSE_NORERUN:  /* Job Not Rerunnable */
177                case PBSE_ROUTEREJ:  /* Route rejected by all destinations */
178                case PBSE_ROUTEEXPD:  /* Time in Route Queue Expired */
179                case PBSE_MOMREJECT:  /* Request to MOM failed */
180                case PBSE_BADSCRIPT:  /* (qsub) cannot access script file */
181                case PBSE_STAGEIN:  /* Stage In of files failed */
182                case PBSE_CKPBSY:  /* Checkpoint Busy, may be retries */
183                case PBSE_EXLIMIT:  /* Limit exceeds allowable */
184                case PBSE_ALRDYEXIT:  /* Job already in exit state */
185                case PBSE_NOCOPYFILE:  /* Job files not copied */
186                case PBSE_CLEANEDOUT:  /* unknown job id after clean init */
187                case PBSE_NOSYNCMSTR:  /* No Master in Sync Set */
188                case PBSE_SISREJECT:  /* sister rejected */
189                case PBSE_SISCOMM:  /* sister could not communicate */
190                case PBSE_CKPSHORT:  /* not all tasks could checkpoint */
191                case PBSE_UNKNODE:  /* Named node is not in the list */
192                case PBSE_NONODES:  /* Server has no node list */
193                case PBSE_NODENBIG:  /* Node name is too big */
194                case PBSE_NODEEXIST:  /* Node name already exists */
195                case PBSE_MUTUALEX:  /* State values are mutually exclusive */
196                case PBSE_GMODERR:  /* Error(s) during global modification of nodes */
197                case PBSE_NORELYMOM:  /* could not contact Mom */
198                        return FSD_ERRNO_INTERNAL_ERROR;
199#endif
200                default:
201                        return FSD_ERRNO_INTERNAL_ERROR;
202         }
203}
204
205
206char *
207pbsdrmaa_write_tmpfile( const char *content, size_t len )
208{
209        static const char *tmpfile_template = "/tmp/pbs_drmaa.XXXXXX";
210        char *volatile name = NULL;
211        volatile int fd = -1;
212
213        fsd_log_enter(( "" ));
214
215        TRY
216         {
217                name = fsd_strdup( tmpfile_template );
218                fd = mkstemp( name );
219                if( fd < 0 )
220                        fsd_exc_raise_sys(0);
221                while( len > 0 )
222                 {
223                        size_t written = write( fd, content, len );
224                        if( written != (size_t)-1 )
225                         {
226                                content += written;
227                                len -= written;
228                         }
229                        else
230                                fsd_exc_raise_sys(0);
231                 }
232         }
233        EXCEPT_DEFAULT
234         { fsd_free( name ); }
235        FINALLY
236         {
237                if( fd >= 0 )
238                 {
239                        if( close( fd ) )
240                                fsd_exc_raise_sys(0);
241                 }
242         }
243        END_TRY
244
245        fsd_log_return(( "=%s", name ));
246        return name;
247}
248
249ssize_t fsd_getline(char * line,ssize_t size, int fd)
250{
251        char buf;
252        char * ptr = NULL;
253        ssize_t n = 0, rc;
254        ptr = line;
255        for(n = 1; n< size; n++)
256        {               
257                if( (rc = read(fd,&buf,1 )) == 1) {
258                        *ptr++ = buf;
259                        if(buf == '\n')
260                        {
261                                break;
262                        }
263                }
264                else if (rc == 0) {
265                        if (n == 1)
266                                return 0;
267                        else
268                                break;
269                }               
270                else
271                        return -1;
272        }
273
274        return n;
275}
276
277ssize_t fsd_getline_buffered(char * line,char * buf, ssize_t size, int fd, int * idx, int * end_idx, int * line_idx)
278{
279        int i = -1;
280        int rc = -1;
281
282        memset(line,0,size);
283       
284start:
285        /* idx - start of data to parse (in buffer)
286           end_idx - end of data read from log (in buffer)
287           line_idx - place to write data in output line */
288        if(*idx < *end_idx)
289        {
290                /* take line from buffer */
291                for(i = *idx; i<= *end_idx;i++)
292                {               
293                        if(buf[i] == '\n')
294                        {
295                                int tmp = i - *idx;
296                                strncpy(line + *line_idx,buf + *idx,tmp);                               
297                                *idx = i + 1;
298                       
299                                tmp+= *line_idx;
300                                *line_idx = 0;
301                               
302                                return tmp;
303                        }
304                }
305               
306                /* there was no '\n' so next part of log needs to be read. save lines beginning */
307                if(*line_idx + i - *idx > size )
308                        fsd_exc_raise_fmt(FSD_ERRNO_INTERNAL_ERROR,"Line longer than %d unsupported",size);
309               
310                strncpy(line + *line_idx,buf + *idx,i - *idx);
311                *line_idx += i - *idx;
312                *idx = 0;
313                *end_idx = 0;
314                goto start;
315        }
316        else
317        {               
318                /* read log */
319                if((rc = read(fd,buf,size)) > 0)
320                {               
321                        *end_idx = rc - 1;
322                        *idx = 0;
323                        goto start;
324                }
325                else if (rc == 0)
326                        return 0;
327                else
328                        return -1;
329        }
330}
331
Note: See TracBrowser for help on using the repository browser.