File: sge_stdlib.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 (399 lines) | stat: -rw-r--r-- 10,089 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
/*___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 <string.h>
#include <limits.h>
#include <unistd.h>
#if __linux__
#include <errno.h>
#include <sys/prctl.h>
#endif

#include "uti/sge_rmon.h"
#include "uti/sge_stdlib.h"
#include "uti/sge_dstring.h"
#include "uti/sge_log.h" 
#include "uti/msg_utilib.h"
#include "uti/sge_uidgid.h"

bool sge_dumpable = false;

/****** uti/stdlib/sge_malloc() ***********************************************
*  NAME
*     sge_malloc() -- replacement for malloc()
*
*  SYNOPSIS
*     void* sge_malloc(size_t size)
*
*  FUNCTION
*     Allocates a memory block. Aborts in case of error.
*
*  INPUTS
*     size_t size - size in bytes
*
*  RESULT
*     void* - pointer to memory block
*
*  NOTES
*     MT-NOTE: sge_malloc() is MT safe
******************************************************************************/
void *sge_malloc(size_t size)
{
   void *cp = NULL;

   DENTER_(BASIS_LAYER, "sge_malloc");

   if (!size) {
      DRETURN_(NULL);
   }

   cp = malloc(size);
   if (!cp) {
      CRITICAL((SGE_EVENT, SFNMAX, MSG_MEMORY_MALLOCFAILED));
      DEXIT_;
      abort();
   }

   DRETURN_(cp);
}

/****** uti/stdlib/sge_realloc() **********************************************
*  NAME
*     sge_realloc() -- replacement for realloc 
*
*  SYNOPSIS
*     char* sge_realloc(char *ptr, int size, int abort) 
*
*  FUNCTION
*     Reallocates a memory block. Aborts in case of an error. 
*
*  INPUTS
*     char *ptr - pointer to a memory block
*     int size  - new size
*     int abort - do abort when realloc fails?
*
*  RESULT
*     char* - pointer to the (new) memory block
*
*  NOTES
*     MT-NOTE: sge_realloc() is MT safe
******************************************************************************/
void *sge_realloc(void *ptr, int size, int do_abort)
{
   void *cp = NULL;

   DENTER_(BASIS_LAYER, "sge_realloc");

   /* if new size is 0, just free the currently allocated memory */
   if (size == 0) {
      sge_free(&ptr);
      DRETURN_(NULL);
   }

   cp = realloc(ptr, size);
   if (cp == NULL) {
      CRITICAL((SGE_EVENT, SFNMAX, MSG_MEMORY_REALLOCFAILED));
      if (do_abort) {
         DEXIT_;
         abort();
      } else {
         sge_free(&ptr);
      }
   }

   DRETURN_(cp);
}

/****** uti/stdlib/sge_free() *************************************************
*  NAME
*     sge_free() -- replacement for free 
*
*  SYNOPSIS
*     void sge_free(void *cp)
*
*  FUNCTION
*     Replacement for free function. Accepts NULL pointers.
*
*  INPUTS
*     void *cp - pointer to a pointer of a memory block
*
*  NOTES
*     MT-NOTE: sge_free() is MT safe
******************************************************************************/
void sge_free(void *cp) 
{
   void **mem = (void **)cp;

   if (mem != NULL && *mem != NULL) {
      free(*mem);
      *mem = NULL;
   }
}  

/****** uti/stdlib/sge_putenv() ***********************************************
*  NAME
*     sge_putenv() -- put an environment variable to environment
*
*  SYNOPSIS
*     static int sge_putenv(const char *var) 
*
*  FUNCTION
*     Duplicates the given environment variable and calls the system call
*     putenv.
*
*  INPUTS
*     const char *var - variable to put in the form <name>=<value>
*
*  RESULT
*     static int - 1 on success, else 0
*
*  SEE ALSO
*     uti/stdlib/sge_setenv() 
*     uti/stdlib/sge_getenv()
*
*  NOTES
*     MT-NOTE: sge_putenv() is MT safe
*******************************************************************************/
int sge_putenv(const char *var)
{
   char *duplicate;

   if(var == NULL) {
      return 0;
   }

   duplicate = strdup(var);

   if(duplicate == NULL) {
      return 0;
   }

   if(putenv(duplicate) != 0) {
      return 0;
   }

   return 1;
}

/****** uti/stdlib/sge_setenv() ***********************************************
*  NAME
*     sge_setenv() -- Change or add an environment variable 
*
*  SYNOPSIS
*     int sge_setenv(const char *name, const char *value) 
*
*  FUNCTION
*     Change or add an environment variable 
*
*  INPUTS
*     const char *name  - variable name 
*     const char *value - new value 
*
*  RESULT
*     int - error state
*         1 - success
*         0 - error 
*
*  SEE ALSO
*     uti/stdlib/sge_putenv() 
*     uti/stdlib/sge_getenv()
*     uti/stdio/addenv()
*
*  NOTES
*     MT-NOTE: sge_setenv() is MT safe
*******************************************************************************/
int sge_setenv(const char *name, const char *value)
{
   int ret = 0;

   if (name != NULL && value != NULL) {
      dstring variable = DSTRING_INIT;

      sge_dstring_sprintf(&variable, "%s=%s", name, value);
      ret = sge_putenv(sge_dstring_get_string(&variable));
      sge_dstring_free(&variable);
   }
   return ret;
}

/* Enable daemon core dumps after setuid etc. calls in routines below,
   which typically disable dumps.  Currently only done for Linux, but
   may be usefully extended for other systems.  */
static void
make_dumpable(void)
{
#if __linux__
   DENTER(TOP_LAYER, "make_dumpable");
   if (true == sge_dumpable) {
      errno = 0;
      int ret = prctl(PR_SET_DUMPABLE, 1, 42, 42, 42);
      if (-1 == ret) {
         ERROR((SGE_EVENT, MSG_PRCTL_FAILED, strerror(errno)));
      }
   }
   DRETURN_VOID;
#endif
}

/****** uti/stdlib/sge_setuid() *************************************************
*  NAME
*     sge_setuid() -- set user id
*
*  SYNOPSIS
*     int sge_setuid(uid_t uid)
*
*  FUNCTION
*     Call setuid and maybe call prctl, or similar, afterwards.
*     prctl is called under Linux to allow core dumps subsequently.
*     That is only done if global sge_dumpable is non-zero.
*
*  INPUTS
*     uid_t uid - uid to set
*
*  RESULT
*     Per setuid
*
*  NOTES
*     MT-NOTE: MT safe as long as sge_dumpable is only set before
*     threads are started.
*******************************************************************************/
int sge_setuid(uid_t uid) {
   int ret = setuid(uid);
   make_dumpable();
   return ret;
}

/****** uti/stdlib/sge_seteuid() *************************************************
*  NAME
*     sge_seteuid() -- set effective user id
*
*  SYNOPSIS
*     int sge_seteuid(uid_t uid)
*
*  FUNCTION
*     Call seteuid and maybe call prctl, or similar, afterwards.
*     prctl is called under Linux to allow core dumps subsequently.
*     That is only done if global sge_dumpable is non-zero.
*
*  INPUTS
*     uid_t uid - uid to set
*
*  RESULT
*     Per seteuid
*
*  NOTES
*     MT-NOTE: MT safe as long as sge_dumpable is only set before
*     threads are started.
*******************************************************************************/
int sge_seteuid(uid_t uid) {
   int ret = seteuid(uid);
   make_dumpable();
   return ret;
}

/****** uti/stdlib/sge_setgid() *************************************************
*  NAME
*     sge_setgid() -- set group id
*
*  SYNOPSIS
*     int sge_setgid(gid_t gid)
*
*  FUNCTION
*     Call setgid and maybe call prctl, or similar, afterwards.
*     prctl is called under Linux to allow core dumps subsequently.
*     That is only done if global sge_dumpable is non-zero.
*
*  INPUTS
*     gid_t gid - gid to set
*
*  RESULT
*     Per setuid
*
*  NOTES
*     MT-NOTE: MT safe as long as sge_dumpable is only set before
*     threads are started.
*******************************************************************************/
int sge_setgid(gid_t gid) {
   int ret = setgid(gid);
   make_dumpable();
   return ret;
}

/****** uti/stdlib/sge_setegid() *************************************************
*  NAME
*     sge_setegid() -- set effective group id
*
*  SYNOPSIS
*     int sge_setegid(gid_t gid)
*
*  FUNCTION
*     Call setegid and maybe call prctl, or similar, afterwards.
*     prctl is called under Linux to allow core dumps subsequently.
*     That is only done if global sge_dumpable is non-zero.
*
*  INPUTS
*     gid_t gid - gid to set
*
*  RESULT
*     Per setegid
*
*  NOTES
*     MT-NOTE: MT safe as long as sge_dumpable is only set before
*     threads are started.
*******************************************************************************/
int sge_setegid(gid_t gid) {
   int ret = setegid(gid);
   make_dumpable();
   return ret;
}

/****** uti/stdlib/sge_maybe_set_dumpable() ************************************
*  NAME
*     sge_maybe_set_dumpable() -- maybe allow core dumps after sge_setuid & al
*
*  SYNOPSIS
*     void sge_maybe_set_dumpable(void gid)
*
*  FUNCTION
*     Conditionally allows dumping core after calls to sge_setuid & al.
*     The condition is that SGE_ENABLE_COREDUMP is set in the environment.
*
*  NOTES
*     MT-NOTE: sge_maybe_set_dumpable() is not MT safe
*/
void sge_maybe_set_dumpable(void) {
  if (getenv("SGE_ENABLE_COREDUMP") != NULL)
     sge_dumpable = true;
  return;
}