File: sge_unistd.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-13.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 57,140 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,445; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,704; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (611 lines) | stat: -rw-r--r-- 15,553 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*___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 "sge_config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h> 

#include "uti/sge_rmon.h"
#include "uti/sge_unistd.h"
#include "uti/sge_log.h"
#include "uti/msg_utilib.h"
#include "uti/sge_prog.h"

#include "gdi/sge_gdi_ctx.h"

typedef enum {
   FILE_TYPE_NOT_EXISTING,
   FILE_TYPE_FILE,
   FILE_TYPE_DIRECTORY
} file_type_t; 

/* MT-NOTE: This module is MT safe */

static int sge_domkdir(const char *, int, bool, bool);

static file_type_t sge_get_file_type(const char *name);  

static file_type_t sge_get_file_type(const char *name)
{
   SGE_STRUCT_STAT stat_buffer;
   file_type_t ret = FILE_TYPE_NOT_EXISTING;
 
   if (SGE_STAT(name, &stat_buffer)) {
      ret = FILE_TYPE_NOT_EXISTING;
   } else {
      if (S_ISDIR(stat_buffer.st_mode)) {
         ret = FILE_TYPE_DIRECTORY;
      } else if (S_ISREG(stat_buffer.st_mode)) {
         ret = FILE_TYPE_FILE;
      } else {
         ret = FILE_TYPE_NOT_EXISTING;
      }
   }
   return ret;
}             

static int sge_domkdir(const char *path_, int fmode, bool exit_on_error, bool may_not_exist) 
{
   SGE_STRUCT_STAT statbuf;
 
   DENTER(TOP_LAYER, "sge_domkdir");
 
   if (mkdir(path_, (mode_t) fmode)) {
      if (errno == EEXIST) {
         if (may_not_exist) {
            DEXIT;
            return -1;
         } else {
            DEXIT;
            return 0;
         }
      }
 
      if (!SGE_STAT(path_, &statbuf) && S_ISDIR(statbuf.st_mode)) {
         /*
          * may be we do not have permission, 
          * but directory already exists 
          */
         DEXIT;
         return 0;
      }
 
      if (exit_on_error) {
         CRITICAL((SGE_EVENT, MSG_FILE_CREATEDIRFAILED_SS, path_, 
                   strerror(errno)));
         SGE_EXIT(NULL, 1);
      } else {
         ERROR((SGE_EVENT, MSG_FILE_CREATEDIRFAILED_SS, path_, 
                strerror(errno)));
         DEXIT;
         return -1;
      }
   }
#if defined( INTERIX )
   /*
    * mkdir is buggy, chown dir manually
    */
   /* Flawfinder: ignore */
   chown(path_, geteuid(), getegid());
#endif
 
   DEXIT;
   return 0;
}         

/****** uti/unistd/sge_unlink() ***********************************************
*  NAME
*     sge_unlink() -- delete a name and possibly the file it refers to
*
*  SYNOPSIS
*     bool sge_unlink(const char *prefix, const char *suffix) 
*
*  FUNCTION
*     Replacement for unlink(). 'prefix' and 'suffix' will be combined
*     to a filename. This file will be deleted. 'prefix' may be NULL.
*
*  INPUTS
*     const char *prefix - pathname or NULL
*     const char *suffix - filename 
*
*  RESULT
*     int - error state
*         true  - OK
*         false - Error
******************************************************************************/
bool sge_unlink(const char *prefix, const char *suffix) 
{
   int status;
   char file[SGE_PATH_MAX];
 
   DENTER(TOP_LAYER, "sge_unlink");
 
   if (!suffix) {
      ERROR((SGE_EVENT, SFNMAX, MSG_POINTER_SUFFIXISNULLINSGEUNLINK));
      DEXIT;
      return false;
   }
 
   if (prefix) {
      snprintf(file, sizeof(file), "%s/%s", prefix, suffix);
   } else {
      snprintf(file, sizeof(file), "%s", suffix);
   }
 
   DPRINTF(("file to unlink: \"%s\"\n", file));
   status = unlink(file);
 
   if (status) {
      ERROR((SGE_EVENT, MSG_FILE_UNLINKFAILED_SS, file, strerror(errno)));
      DEXIT;
      return false;
   } else {
      DEXIT;
      return true;
   }
}  

/****** uti/unistd/sge_sleep() ************************************************
*  NAME
*     sge_sleep() -- sleep for x microseconds 
*
*  SYNOPSIS
*     void sge_sleep(int sec, int usec) 
*
*  FUNCTION
*     Delays the calling application for 'sec' seconds and 'usec'
*     microseconds 
*
*  INPUTS
*     int sec  - seconds 
*     int usec - microseconds 
******************************************************************************/
void sge_sleep(int sec, int usec) 
{
   struct timeval timeout;
 
   timeout.tv_sec = sec;
   timeout.tv_usec = usec;
 
   select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &timeout);
}       

/****** uti/unistd/sge_chdir_exit() *******************************************
*  NAME
*     sge_chdir_exit() -- Replacement for chdir() 
*
*  SYNOPSIS
*     int sge_chdir_exit(const char *path, int exit_on_error) 
*
*  FUNCTION
*     Change working directory 
*
*  INPUTS
*     const char *path  - pathname 
*     int exit_on_error - exit in case of errors 
*
*  RESULT
*     int - error state
*         0 - OK
*        -1 - ERROR ('exit_on_error'==1 the function may not return)
*
*  SEE ALSO
*     uti/unistd/sge_chdir()
******************************************************************************/
int sge_chdir_exit(const char *path, int exit_on_error) 
{
   DENTER(BASIS_LAYER, "sge_chdir");
 
   if (chdir(path)) {
      if (exit_on_error) {
         CRITICAL((SGE_EVENT, MSG_FILE_NOCDTODIRECTORY_S , path));
         SGE_EXIT(NULL, 1);
      } else {
         ERROR((SGE_EVENT, MSG_FILE_NOCDTODIRECTORY_S , path));
         return -1;
      }
   }
 
   DEXIT;
   return 0;
}           

/****** uti/unistd/sge_chdir() ************************************************
*  NAME
*     sge_chdir() --  Replacement for chdir()
*
*  SYNOPSIS
*     int sge_chdir(const char *dir) 
*
*  FUNCTION
*     Change working directory 
*
*  INPUTS
*     const char *dir - pathname 
*
*  RESULT
*     int - error state
*        0 - success
*        != 0 - error 
*
*  NOTE
*     Might be used in shepherd because it does not use CRITICAL/ERROR.
*     TODO: pass a dstring for the return of error messages.
*
*  SEE ALSO
*     uti/unistd/sge_chdir_exit()
******************************************************************************/
int sge_chdir(const char *dir)
{
   if (dir != NULL) {
      SGE_STRUCT_STAT statbuf;

      /*
       * force automount
       */
      SGE_STAT(dir, &statbuf);
      return chdir(dir);
   }

   /* on error return -1 */
   return -1;
}

/****** uti/unistd/sge_exit() *************************************************
*  NAME
*     sge_exit() -- Wrapped exit Function 
*
*  SYNOPSIS
*     void sge_exit(int i) 
*
*  FUNCTION
*     Calls 'exit_func' if installed. Stops monitoring with DCLOSE 
*
*  INPUTS
*     sge_gdi_ctx_class_t **ref_ctx - address of the context, the context is freed in exit_func
*     int i          - exit state 
*
*  SEE ALSO
*     uti/unistd/sge_install_exit_func()
******************************************************************************/
void sge_exit(void **ref_ctx, int i) 
{
   sge_exit_func_t exit_func = NULL;

   if (ref_ctx && *ref_ctx) {
      sge_gdi_ctx_class_t *ctx = *ref_ctx;
      exit_func = ctx->get_exit_func(ctx);
   }   
   if (exit_func) {
      exit_func(ref_ctx, i);
   }
   exit(i);
}
 

/****** uti/unistd/sge_mkdir() ************************************************
*  NAME
*     sge_mkdir() -- Create a directory (and necessary parents)
*
*  SYNOPSIS
*     int sge_mkdir(const char *path, int fmode, bool exit_on_error, bool may_not_exist) 
*
*  FUNCTION
*     Create a directory 
*
*  INPUTS
*     const char *path  - path 
*     int fmode         - file mode 
*     bool exit_on_error - as it says 
*     bool may_not_exist - if true an error is returned if the last component
*                         of the path exists
*
*  RESULT
*     int - error state
*         0 - OK
*        -1 - Error (The function may never return)
******************************************************************************/
int sge_mkdir(const char *path, int fmode, bool exit_on_error, bool may_not_exist) 
{
   int i = 0, res=0;
   char path_[SGE_PATH_MAX];
 
   DENTER(TOP_LAYER, "sge_mkdir");
   if (!path) {
      if (exit_on_error) {
         CRITICAL((SGE_EVENT, SFNMAX, MSG_VAR_PATHISNULLINSGEMKDIR));
         DCLOSE;
         SGE_EXIT(NULL, 1);
      } else {
         ERROR((SGE_EVENT, SFNMAX, MSG_VAR_PATHISNULLINSGEMKDIR));
         DEXIT;
         return -1;
      }
   }

   DPRINTF(("Making dir \"%s\"\n", path));

   memset(path_, 0, sizeof(path_));
   while ((unsigned char) path[i]) {
      path_[i] = path[i];
      if ((path[i] == '/') && (i != 0)) {
         path_[i] = (unsigned char) 0;
         res = sge_domkdir(path_, fmode, exit_on_error, false);
         if (res) {
            DPRINTF(("retval = %d\n", res));
            DEXIT;
            return res;
         }
      }
      path_[i] = path[i];
      i++;
   }
 
   i = sge_domkdir(path_, fmode, exit_on_error, may_not_exist);
 
   DPRINTF(("retval = %d\n", i));
   DEXIT;
   return i;
}   

int sge_mkdir2(const char *base_dir, const char *name, int fmode, bool exit_on_error)
{
   dstring path = DSTRING_INIT;
   int ret;

   DENTER(TOP_LAYER, "sge_mkdir2");
   
   if (base_dir == NULL || name == NULL) {
      if (exit_on_error) {
         CRITICAL((SGE_EVENT, SFNMAX, MSG_VAR_PATHISNULLINSGEMKDIR));
         DCLOSE;
         SGE_EXIT(NULL, 1);
      } else {
         ERROR((SGE_EVENT, SFNMAX, MSG_VAR_PATHISNULLINSGEMKDIR));
         DRETURN(-1);
      }
   }
  
   sge_dstring_sprintf(&path, "%s/%s", base_dir, name);

   ret = sge_mkdir(sge_dstring_get_string(&path), fmode, exit_on_error, false); 
   sge_dstring_free(&path);

   DRETURN(ret);
}

/****** uti/unistd/sge_rmdir() ************************************************
*  NAME
*     sge_rmdir() -- Recursive rmdir
*
*  SYNOPSIS
*     int sge_rmdir(const char *cp, dstring *error)  
*
*  FUNCTION
*     Remove a directory tree. In case of errors a message may be found
*     in 'error' afterwards.
*
*  INPUTS
*     const char *cp  - path 
*     dstring *error  - destination for error message if non-NULL
*
*  RESULT
*     int - error state
*         0 - OK
*        -1 - Error 
******************************************************************************/
int sge_rmdir(const char *cp, dstring *error) 
{
   SGE_STRUCT_STAT statbuf;
   SGE_STRUCT_DIRENT *dent;
   DIR *dir;
   char dirent[SGE_PATH_MAX*2];
   char fname[SGE_PATH_MAX];

   DENTER(TOP_LAYER, "sge_rmdir");
 
   if (!cp) {
      sge_dstring_sprintf(error, "%s", MSG_POINTER_NULLPARAMETER);
      DEXIT;
      return -1;
   }
 
   if (!(dir = opendir(cp))) {
      sge_dstring_sprintf(error, MSG_FILE_OPENDIRFAILED_SS , cp, strerror(errno));
      DEXIT;
      return -1;
   }
 
   while (SGE_READDIR_R(dir, (SGE_STRUCT_DIRENT *)dirent, &dent)==0 && dent!=NULL) {
      if (strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
 
         snprintf(fname, sizeof(fname), "%s/%s", cp, dent->d_name);

#ifdef HAVE_STRUCT_DIRENT_D_TYPE
	 switch (dent->d_type) {
	 case DT_UNKNOWN:
	    break;
	 case DT_DIR:
	    if (sge_rmdir(fname, error)) {
	       sge_dstring_sprintf(error, "%s", MSG_FILE_RECURSIVERMDIRFAILED );
	       closedir(dir);
	       DEXIT;
	       return -1;
	    }
	    continue;
	 default:
	    if (unlink(fname)) {
	       sge_dstring_sprintf(error, MSG_FILE_UNLINKFAILED_SS,
				   fname, strerror(errno));
	       closedir(dir);
	       DEXIT;
	       return -1;
	    }
	    continue;
	 }
#endif /* HAVE_STRUCT_DIRENT_D_TYPE */

#ifndef WIN32 /* lstat not called */
         if (SGE_LSTAT(fname, &statbuf)) {
            sge_dstring_sprintf(error, MSG_FILE_STATFAILED_SS , fname, strerror(errno));
            closedir(dir);
            DEXIT;
            return -1;
         }
#else
         /* so symbolic links under Windows */
         if (SGE_STAT(fname, &statbuf)) {
            sge_dstring_sprintf(error, MSG_FILE_STATFAILED_SS , fname, strerror(errno));
            closedir(dir);
            DEXIT;
            return -1;
         }
#endif /* WIN32 */
 
    if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) 
    {
    if (sge_rmdir(fname, error)) {
               sge_dstring_sprintf(error, "%s", MSG_FILE_RECURSIVERMDIRFAILED );
               closedir(dir);
               DEXIT;
               return -1;
            }
         }
         else {                                                
#ifdef TEST
            printf("unlink %s\n", fname);
#else
            if (unlink(fname)) {
               sge_dstring_sprintf(error, MSG_FILE_UNLINKFAILED_SS,
                      fname, strerror(errno));
               closedir(dir);
               DEXIT;
               return -1;
            }
#endif
         }
      }
   }
 
   closedir(dir);
 
#ifdef TEST
   printf("rmdir %s\n", cp);
#else
   if (rmdir(cp)) {
      sge_dstring_sprintf(error, MSG_FILE_RMDIRFAILED_SS , cp, strerror(errno));
      DEXIT;
      return -1;
   }
#endif
 
   return 0;
}
 
int sge_is_executable(const char *name) 
{
   SGE_STRUCT_STAT stat_buffer;
   int ret = SGE_STAT(name, &stat_buffer);
   if (!ret) { 
      return (int)(stat_buffer.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH));
   } else {
      return 0;
   }
}


/****** uti/unistd/sge_is_directory() *****************************************
*  NAME
*     sge_is_directory() -- Does 'name' exist and is it a directory? 
*
*  SYNOPSIS
*     int sge_is_directory(const char *name) 
*
*  FUNCTION
*     Does 'name' exist and is it a directory?  
*
*  INPUTS
*     const char *name  - directory name 
*
*  RESULT
*     int - error state
*         0 - No
*         1 - Yes 
******************************************************************************/
int sge_is_directory(const char *name)
{
   return (sge_get_file_type(name) == FILE_TYPE_DIRECTORY);
}
 
/****** uti/unistd/sge_is_file() **********************************************
*  NAME
*     sge_is_file() -- Does 'name' exist and is it a file? 
*
*  SYNOPSIS
*     int sge_is_file(const char *name) 
*
*  FUNCTION
*     Does 'name' exist and is it a file?  
*
*  INPUTS
*     const char *name  - filename 
*
*  RESULT
*     int - error state
*         0 - No
*         1 - Yes 
******************************************************************************/
int sge_is_file(const char *name)
{
   return (sge_get_file_type(name) == FILE_TYPE_FILE);
}                         
 
#ifdef TEST
int main(int argc, char **argv)
{
   char err_str[1024];
 
   if (argc!=2) {
      fprintf(stderr, "usage: rmdir <dir>\n");
      exit(1);
   }
   if (sge_rmdir(argv[1], err_str)) {
      fprintf(stderr, "%s", err_str);
      return 1;
   }
   return 0;
}
#endif