File: sge_afsutil.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (242 lines) | stat: -rw-r--r-- 6,941 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#include "basis_types.h"

#include "uti/sge_rmon.h"
#include "uti/sge_afsutil.h"
#include "uti/sge_io.h"
#include "uti/sge_unistd.h"
#include "uti/sge_stdio.h"
#include "uti/msg_utilib.h"
#include "uti/sge_string.h"

/****** uti/afsutil/sge_read_token() ******************************************
*  NAME
*     sge_read_token() -- read token from file 
*
*  SYNOPSIS
*     char* sge_read_token(const char *file) 
*
*  FUNCTION
*     Read token from file, malloc buffer, add '\0' byte and
*     return pointer to buffer.
*
*  INPUTS
*     const char *file - filename 
*
*  NOTES
*     MT-NOTE: sge_read_token() is MT safe
*
*  RESULT
*     char* - pointer to a malloced buffer or 
*             NULL if error occured
******************************************************************************/
char *sge_read_token(const char *file) 
{
   SGE_STRUCT_STAT sb;
   int fd;
   char *tokenbuf;
   size_t size;

   DENTER(TOP_LAYER, "sge_read_token");

   if (SGE_STAT(file, &sb)) {
      DTRACE;
      return NULL;
   }

   size = sb.st_size + 1;
   if (((off_t)size != sb.st_size + 1)
       || (tokenbuf = (char *) malloc(size)) == NULL) {
      DTRACE;
      return NULL;
   }

   if ((fd = SGE_OPEN2(file, O_RDONLY)) == -1) {
      DTRACE;
      return NULL;
   }

   if (read(fd, tokenbuf, sb.st_size) != sb.st_size) {
      DTRACE;
      close(fd);
      return NULL;
   }

   tokenbuf[sb.st_size] = '\0';

   close(fd);
   DEXIT;
   return tokenbuf;
}

/****** uti/afsutil/sge_afs_extend_token() ************************************
*  NAME
*     sge_afs_extend_token() -- Extend an AFS token
*
*  SYNOPSIS
*     int sge_afs_extend_token(const char *command, char *tokenbuf, 
*                              const char *user, int token_extend_time, 
*                              char *err_str, size_t lstr)
*
*  FUNCTION
*     Call 'command', pipe content of 'tokenbuf' to 'command', using
*     'user' as arg1 and 'token_extend_time' as arg2 
*
*  INPUTS
*     const char *command   - command 
*     char *tokenbuf        - input for command 
*     const char *user      - 1st argument for command 
*     int token_extend_time - 2nd argument for command 
*     char *err_str         - error message
*     size_t lstr           - length of err_str
*
*  NOTES
*     MT-NOTE: sge_afs_extend_token() is not MT safe because it uses MT unsafe 
*     MT-NOTE: sge_peopen()
*
*  RESULT
*     int - error state
*         0 - OK
*        -1 - Error 
******************************************************************************/
int sge_afs_extend_token(const char *command, char *tokenbuf, const char *user,
                         int token_extend_time, char *err_str, size_t lstr) 
{
   pid_t command_pid;
   FILE *fp_in, *fp_out, *fp_err;
   int ret;
   char cmdbuf[SGE_PATH_MAX+128];

   DENTER(TOP_LAYER, "sge_afs_extend_token");

   snprintf(cmdbuf, sizeof(cmdbuf), "%s %s %d", command, user, token_extend_time);
   if (err_str) {
      sge_strlcpy(err_str, cmdbuf, lstr);
   }

   command_pid = sge_peopen("/bin/sh", 0, cmdbuf, NULL, NULL, 
                        &fp_in, &fp_out, &fp_err, false);
   if (command_pid == -1) {
      if (err_str) {
         snprintf(err_str, lstr, MSG_TOKEN_NOSTART_S , cmdbuf);
      }
      DEXIT;
      return -1;
   }
   if (sge_string2bin(fp_in, tokenbuf) == -1) {
      if (err_str) {
         snprintf(err_str, lstr, MSG_TOKEN_NOWRITEAFS_S , cmdbuf);
      }
      DEXIT;
      return -1;
   }

   if ((ret = sge_peclose(command_pid, fp_in, fp_out, fp_err, NULL)) != 0) {
      if (err_str) {
         snprintf(err_str, lstr, MSG_TOKEN_NOSETAFS_SI , cmdbuf, ret);
      }
      DEXIT;
      return -1;
   }
    
   return 0;
}

/****** uti/afsutil/sge_get_token_cmd() ***************************************
*  NAME
*     sge_get_token_cmd() -- Check if 'tokencmdname' is executable 
*
*  SYNOPSIS
*     int sge_get_token_cmd(const char *tokencmdname, char *buf, size_t lbuf)
*
*  FUNCTION
*     Check if 'tokencmdname' exists and is executable. If an error
*     occures write a error message into 'buf' if not NULL. 
*     Otherwise write error messages to stderr.
*
*  INPUTS
*     const char *tokencmdname - command 
*     char *buf                - NULL or buffer for error message
*     size_t lbuf              - size of buf
*
*  NOTES
*     MT-NOTE: sge_get_token_cmd() is MT safe 
*
*  RESULT
*     int - error state
*         0 - OK
*         1 - Error
******************************************************************************/
int sge_get_token_cmd(const char *tokencmdname, char *buf, size_t lbuf)
{
    SGE_STRUCT_STAT sb;

    if (!tokencmdname || !strlen(tokencmdname)) {
       if (!buf) {
          fprintf(stderr, "%s\n", MSG_COMMAND_NOPATHFORTOKEN);
       } else {   
          sge_strlcpy(buf, MSG_COMMAND_NOPATHFORTOKEN, lbuf);
       }
       return 1;
    }   
    
    if (SGE_STAT(tokencmdname, &sb) == -1) {
       if (!buf) {
          fprintf(stderr, MSG_COMMAND_NOFILESTATUS_S , tokencmdname);
          fprintf(stderr, "\n");
       } else {
          snprintf(buf, lbuf, MSG_COMMAND_NOFILESTATUS_S , tokencmdname);
       }
       return 1;
    }   
    
    if (!(sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) {
       if (!buf) {
          fprintf(stderr, MSG_COMMAND_NOTEXECUTABLE_S , tokencmdname);
          fprintf(stderr, "\n");
       } else {
          snprintf(buf, lbuf, MSG_COMMAND_NOTEXECUTABLE_S , tokencmdname);
       }
       return 1;
    }
    
    return 0;
}