ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/tools.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/tools.c (file contents):
Revision 1.14 by pajs, Thu Nov 13 17:02:46 2003 UTC vs.
Revision 1.59 by ats, Sat Nov 6 14:55:53 2004 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library 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.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser 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, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 25 | Line 28
28   #include <stdio.h>
29   #include <string.h>
30   #include <stdlib.h>
31 + #include <unistd.h>
32   #include <sys/types.h>
29 #include <regex.h>
33   #ifdef ALLBSD
34   #include <fcntl.h>
35 + #endif
36 + #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
37   #include <kvm.h>
38 + #include <paths.h>
39   #endif
40 < #ifdef NETBSD
40 > #if defined(NETBSD) || defined(OPENBSD)
41   #include <uvm/uvm_extern.h>
42 + #include <sys/param.h>
43 + #include <sys/sysctl.h>
44   #endif
45 + #ifdef HPUX
46 + #include <sys/param.h>
47 + #include <sys/pstat.h>
48 + #endif
49  
50   #include "tools.h"
51 + #include "statgrab.h"
52  
53   #ifdef SOLARIS
54 + #ifdef HAVE_LIBDEVINFO_H
55   #include <libdevinfo.h>
56 + #endif
57   #include <kstat.h>
58   #include <unistd.h>
59   #include <ctype.h>
# Line 50 | Line 65
65   #include <dirent.h>
66   #endif
67  
68 < #ifdef SOLARIS
68 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
69   struct map{
70          char *bsd;
71          char *svr;
# Line 60 | Line 75 | struct map{
75   typedef struct map mapping_t;
76  
77   static mapping_t *mapping = NULL;
78 + #endif
79  
80 < char *get_svr_from_bsd(char *bsd){
80 > #ifdef SOLARIS
81 > const char *sg_get_svr_from_bsd(const char *bsd){
82 > #ifdef HAVE_LIBDEVINFO_H
83          mapping_t *map_ptr;
84          for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next)
85                  if(!strcmp(map_ptr->bsd, bsd)) return map_ptr->svr;
86 <
86 > #endif
87          return bsd;
88   }
89 + #endif
90  
91 < void add_mapping(char *bsd, char *svr){
91 > #if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H)
92 > static void add_mapping(char *bsd, char *svr){
93          mapping_t *map_ptr;
94          mapping_t *map_end_ptr;
95  
76        bsd = strdup(bsd);
77        svr = strdup(svr);
78
96          if (mapping == NULL){
97 <                printf("New malloc\n");
81 <                mapping = malloc(sizeof(mapping_t));
97 >                mapping = sg_malloc(sizeof(mapping_t));
98                  if (mapping == NULL) return;
99                  map_ptr = mapping;
100          }else{
101                  /* See if its already been added */
102                  for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next){
103                          if( (!strcmp(map_ptr->bsd, bsd)) || (!strcmp(map_ptr->svr, svr)) ){
88                                printf("%s matches %s\n", map_ptr->bsd, bsd);
104                                  return;
105                          }
106                          map_end_ptr = map_ptr;
# Line 94 | Line 109 | void add_mapping(char *bsd, char *svr){
109                  /* We've reached end of list and not found the entry.. So we need to malloc
110                   * new mapping_t
111                   */
112 <                map_end_ptr->next = malloc(sizeof(mapping_t));
98 <                printf("Second malloc\n");
112 >                map_end_ptr->next = sg_malloc(sizeof(mapping_t));
113                  if (map_end_ptr->next == NULL) return;
114                  map_ptr = map_end_ptr->next;
115          }
116  
103        printf("Adding %s\n", bsd);
117          map_ptr->next = NULL;
118 <        map_ptr->bsd = bsd;
119 <        map_ptr->svr = svr;
118 >        map_ptr->bsd = NULL;
119 >        map_ptr->svr = NULL;
120 >        if (sg_update_string(&map_ptr->bsd, bsd) < 0
121 >            || sg_update_string(&map_ptr->svr, svr) < 0) {
122 >                return;
123 >        }
124  
125          return;
126   }
127  
128 < char *read_dir(char *disk_path){
128 >
129 > static char *read_dir(char *disk_path){
130          DIR *dirp;
131 <        struct dirent *dp;
132 <        struct stat stbuf;
133 <        char *svr_name;
134 <        char current_dir[MAXPATHLEN];
135 <        char file_name[MAXPATHLEN];
136 <        char temp_name[MAXPATHLEN];
137 <        char dir_dname[MAXPATHLEN];
131 >        struct dirent *dp;
132 >        struct stat stbuf;
133 >        char *svr_name = NULL;
134 >        char current_dir[MAXPATHLEN];
135 >        char file_name[MAXPATHLEN];
136 >        char temp_name[MAXPATHLEN];
137 >        char dir_dname[MAXPATHLEN];
138          char *dsk_dir;
139          int x;
140  
141          dsk_dir = "/dev/osa/dev/dsk";
142 <        strncpy(current_dir, dsk_dir, sizeof current_dir);
143 <        if ((dirp = opendir(current_dir)) == NULL){
142 >        strncpy(current_dir, dsk_dir, sizeof current_dir);
143 >        if ((dirp = opendir(current_dir)) == NULL){
144                  dsk_dir = "/dev/dsk";
145                  snprintf(current_dir, sizeof current_dir, "%s", dsk_dir);
146                  if ((dirp = opendir(current_dir)) == NULL){
# Line 130 | Line 148 | char *read_dir(char *disk_path){
148                  }
149          }
150  
151 <        while ((dp = readdir(dirp)) != NULL){
151 >        while ((dp = readdir(dirp)) != NULL){
152                  snprintf(temp_name, sizeof temp_name, "../..%s", disk_path);
153                  snprintf(dir_dname, sizeof dir_dname, "%s/%s", dsk_dir, dp->d_name);
154                  stat(dir_dname,&stbuf);
# Line 139 | Line 157 | char *read_dir(char *disk_path){
157                          x = readlink(dir_dname, file_name, sizeof(file_name));
158                          file_name[x] = '\0';
159                          if (strcmp(file_name, temp_name) == 0) {
160 <                                svr_name = strdup(dp->d_name);
160 >                                if (sg_update_string(&svr_name,
161 >                                                     dp->d_name) < 0) {
162 >                                        return NULL;
163 >                                }
164                                  closedir(dirp);
165                                  return svr_name;
166                          }
167                  }
147                closedir(dirp);
168          }
169 +        closedir(dirp);
170          return NULL;
171   }
172  
173  
174 <
154 < int get_alias(char *alias){
174 > static int get_alias(char *alias){
175          char file[MAXPATHLEN];
176          di_node_t root_node;
177          di_node_t node;
# Line 162 | Line 182 | int get_alias(char *alias){
182          char *value;
183          int instance;
184          if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
185 <                fprintf(stderr, "di_init() failed\n");
166 <                exit(1);
185 >                return -1;
186          }
187          node = di_drv_first_node(alias, root_node);
188          while (node != DI_NODE_NIL) {
# Line 173 | Line 192 | int get_alias(char *alias){
192                          minor_name = di_minor_name(minor);
193                          strcpy(tmpnode, alias);
194                          sprintf(tmpnode, "%s%d", tmpnode, instance);
195 <                        strcpy(file, "/devices");
196 <                        strcat(file, phys_path);
197 <                        strcat(file, ":");
198 <                        strcat(file, minor_name);
195 >                        sg_strlcpy(file, "/devices", sizeof file);
196 >                        sg_strlcat(file, phys_path, sizeof file);
197 >                        sg_strlcat(file, ":", sizeof file);
198 >                        sg_strlcat(file, minor_name, sizeof file);
199                          value = read_dir(file);
200                          if (value != NULL){
201                                  add_mapping(tmpnode, value);
# Line 186 | Line 205 | int get_alias(char *alias){
205                  }else{
206                          node = di_drv_next_node(node);
207                  }
208 <        }                           /* End of the while loop */
208 >        }
209          di_fini(root_node);
210 <        return (-1);
210 >        return 0;
211   }
212  
213 < void build_mapping(){
214 <        char device_name[512];
213 >
214 > #define BIG_ENOUGH 512
215 > static int build_mapping(){
216 >        char device_name[BIG_ENOUGH];
217          int x;
218 <        kstat_ctl_t *kc;
219 <        kstat_t *ksp;
220 <        kstat_io_t kios;
218 >        kstat_ctl_t *kc;
219 >        kstat_t *ksp;
220 >        kstat_io_t kios;
221  
222 +        char driver_list[BIG_ENOUGH][BIG_ENOUGH];
223 +        int list_entries = 0;
224 +        int found;
225 +
226          if ((kc = kstat_open()) == NULL) {
227 <                return NULL;
228 <        }
227 >                return -1;
228 >        }
229  
230 <        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
231 <                if (!strcmp(ksp->ks_class, "disk")) {
232 <
233 <                        if(ksp->ks_type != KSTAT_TYPE_IO) continue;
234 <                        /* We dont want metadevices appearins as num_diskio */
235 <                        if(strcmp(ksp->ks_module, "md")==0) continue;
211 <                        if((kstat_read(kc, ksp, &kios))==-1) continue;
230 >        for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
231 >                if (!strcmp(ksp->ks_class, "disk")) {
232 >                        if(ksp->ks_type != KSTAT_TYPE_IO) continue;
233 >                        /* We dont want metadevices appearing as num_diskio */
234 >                        if(strcmp(ksp->ks_module, "md")==0) continue;
235 >                        if((kstat_read(kc, ksp, &kios))==-1) continue;
236                          strncpy(device_name, ksp->ks_name, sizeof device_name);
237                          for(x=0;x<(sizeof device_name);x++){
238                                  if( isdigit((int)device_name[x]) ) break;
239                          }
240                          if(x == sizeof device_name) x--;
241                          device_name[x] = '\0';
242 <                        get_alias(device_name);
243 <                 }
242 >
243 >                        /* Check if we've not already looked it up */
244 >                        found = 0;
245 >                        for(x=0;x<list_entries;x++){
246 >                                if (x>=BIG_ENOUGH){
247 >                                        /* We've got bigger than we thought was massive */
248 >                                        /* If we hit this.. Make big enough bigger */
249 >                                        return -1;
250 >                                }
251 >                                if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){
252 >                                        found = 1;
253 >                                        break;
254 >                                }
255 >                        }
256 >
257 >                        if(!found){
258 >                                if((get_alias(device_name)) != 0){
259 >                                        return -1;
260 >                                }
261 >                                strncpy(driver_list[x], device_name, BIG_ENOUGH);
262 >                                list_entries++;
263 >                        }
264 >                }
265          }
266 <        
267 <        return;
266 >
267 >        return 0;
268   }
269  
270   #endif
271  
272 <
273 <
229 < char *f_read_line(FILE *f, const char *string){
272 > #if defined(LINUX) || defined(CYGWIN)
273 > char *sg_f_read_line(FILE *f, const char *string){
274          /* Max line length. 8k should be more than enough */
275          static char line[8192];
276  
# Line 236 | Line 280 | char *f_read_line(FILE *f, const char *string){
280                  }
281          }
282  
283 +        sg_set_error(SG_ERROR_PARSE, NULL);
284          return NULL;
285   }
286  
287 < char *get_string_match(char *line, regmatch_t *match){
287 > char *sg_get_string_match(char *line, regmatch_t *match){
288          int len=match->rm_eo - match->rm_so;
289 <        char *match_string=malloc(len+1);
289 >        char *match_string=sg_malloc(len+1);
290  
291          match_string=strncpy(match_string, line+match->rm_so, len);
292          match_string[len]='\0';
# Line 249 | Line 294 | char *get_string_match(char *line, regmatch_t *match){
294          return match_string;
295   }
296  
297 + long long sg_get_ll_match(char *line, regmatch_t *match){
298 +        char *ptr;
299 +        long long num;
300  
301 +        ptr=line+match->rm_so;
302 +        num=atoll(ptr);
303  
304 +        return num;
305 + }
306 + #endif
307 +
308   #ifndef HAVE_ATOLL
309   static long long atoll(const char *s) {
310          long long value = 0;
# Line 271 | Line 325 | static long long atoll(const char *s) {
325   }
326   #endif
327  
328 < long long get_ll_match(char *line, regmatch_t *match){
275 <        char *ptr;
276 <        long long num;
328 > /*      $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $     */
329  
330 <        ptr=line+match->rm_so;
331 <        num=atoll(ptr);
330 > /*
331 > * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
332 > *
333 > * Permission to use, copy, modify, and distribute this software for any
334 > * purpose with or without fee is hereby granted, provided that the above
335 > * copyright notice and this permission notice appear in all copies.
336 > *
337 > * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
338 > * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
339 > * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
340 > * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
341 > * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
342 > * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
343 > * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
344 > */
345  
346 <        return num;
346 > /*
347 > * Copy src to string dst of size siz.  At most siz-1 characters
348 > * will be copied.  Always NUL terminates (unless siz == 0).
349 > * Returns strlen(src); if retval >= siz, truncation occurred.
350 > */
351 > size_t sg_strlcpy(char *dst, const char *src, size_t siz){
352 >        register char *d = dst;
353 >        register const char *s = src;
354 >        register size_t n = siz;
355 >
356 >        /* Copy as many bytes as will fit */
357 >        if (n != 0 && --n != 0) {
358 >                do {
359 >                        if ((*d++ = *s++) == 0)
360 >                                break;
361 >                } while (--n != 0);
362 >        }
363 >
364 >        /* Not enough room in dst, add NUL and traverse rest of src */
365 >        if (n == 0) {
366 >                if (siz != 0)
367 >                        *d = '\0';            /* NUL-terminate dst */
368 >                while (*s++)
369 >                        ;
370 >        }
371 >
372 >        return(s - src - 1);    /* count does not include NUL */
373   }
374  
375 < #ifdef ALLBSD
376 < kvm_t *get_kvm() {
375 > /*      $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $    */
376 >
377 > /*
378 > * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
379 > *
380 > * Permission to use, copy, modify, and distribute this software for any
381 > * purpose with or without fee is hereby granted, provided that the above
382 > * copyright notice and this permission notice appear in all copies.
383 > *
384 > * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
385 > * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
386 > * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
387 > * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
388 > * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
389 > * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
390 > * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
391 > */
392 >
393 > /*
394 > * Appends src to string dst of size siz (unlike strncat, siz is the
395 > * full size of dst, not space left).  At most siz-1 characters
396 > * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
397 > * Returns strlen(src) + MIN(siz, strlen(initial dst)).
398 > * If retval >= siz, truncation occurred.
399 > */
400 > size_t sg_strlcat(char *dst, const char *src, size_t siz){
401 >        register char *d = dst;
402 >        register const char *s = src;
403 >        register size_t n = siz;
404 >        size_t dlen;
405 >
406 >        /* Find the end of dst and adjust bytes left but don't go past end */
407 >        while (n-- != 0 && *d != '\0')
408 >                d++;
409 >        dlen = d - dst;
410 >        n = siz - dlen;
411 >
412 >        if (n == 0)
413 >                return(dlen + strlen(s));
414 >        while (*s != '\0') {
415 >                if (n != 1) {
416 >                        *d++ = *s;
417 >                        n--;
418 >                }
419 >                s++;
420 >        }
421 >        *d = '\0';
422 >
423 >        return(dlen + (s - src));       /* count does not include NUL */
424 > }
425 >
426 > int sg_update_string(char **dest, const char *src) {
427 >        char *new;
428 >
429 >        if (src == NULL) {
430 >                /* We're being told to set it to NULL. */
431 >                free(*dest);
432 >                *dest = NULL;
433 >                return 0;
434 >        }
435 >
436 >        new = sg_realloc(*dest, strlen(src) + 1);
437 >        if (new == NULL) {
438 >                return -1;
439 >        }
440 >
441 >        strcpy(new, src);
442 >        *dest = new;
443 >        return 0;
444 > }
445 >
446 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
447 > kvm_t *sg_get_kvm() {
448          static kvm_t *kvmd = NULL;
449  
450          if (kvmd != NULL) {
# Line 290 | Line 452 | kvm_t *get_kvm() {
452          }
453  
454          kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL);
455 +        if(kvmd == NULL) {
456 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
457 +        }
458          return kvmd;
459   }
460 +
461 + /* Can't think of a better name for this function */
462 + kvm_t *sg_get_kvm2() {
463 +        static kvm_t *kvmd2 = NULL;
464 +
465 +        if (kvmd2 != NULL) {
466 +                return kvmd2;
467 +        }
468 +
469 +        kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
470 +        if(kvmd2 == NULL) {
471 +                sg_set_error(SG_ERROR_KVM_OPENFILES, NULL);
472 +        }
473 +        return kvmd2;
474 + }
475   #endif
476  
477 < #ifdef NETBSD
478 < struct uvmexp *get_uvmexp() {
479 <        static u_long addr = 0;
477 > #if defined(NETBSD) || defined(OPENBSD)
478 > struct uvmexp *sg_get_uvmexp() {
479 >        int mib[2];
480 >        size_t size = sizeof(struct uvmexp);
481          static struct uvmexp uvm;
482 <        kvm_t *kvmd = get_kvm();
482 >        struct uvmexp *new;
483  
484 <        if (kvmd == NULL) {
484 >        mib[0] = CTL_VM;
485 >        mib[1] = VM_UVMEXP;
486 >
487 >        if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) {
488 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP");
489                  return NULL;
490          }
491  
492 <        if (addr == 0) {
493 <                struct nlist symbols[] = {
494 <                        { "_uvmexp" },
310 <                        { NULL }
311 <                };
492 >        return &uvm;
493 > }
494 > #endif
495  
496 <                if (kvm_nlist(kvmd, symbols) != 0) {
496 > #ifdef HPUX
497 > struct pst_static *sg_get_pstat_static() {
498 >        static int got = 0;
499 >        static struct pst_static pst;
500 >
501 >        if (!got) {
502 >                if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) {
503 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
504 >                                                "pstat_static");
505                          return NULL;
506                  }
507 <                addr = symbols[0].n_value;
507 >                got = 1;
508          }
509 <
319 <        if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) {
320 <                return NULL;
321 <        }
322 <        return &uvm;
509 >        return &pst;
510   }
511   #endif
512  
513 < int statgrab_init(){
514 < #ifdef ALLBSD
515 <        {
516 <                kvm_t *kvmd = get_kvm();
517 <                if (kvmd == NULL) return 1;
513 > int sg_init(){
514 >        sg_set_error(SG_ERROR_NONE, NULL);
515 >
516 > #if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD)
517 >        if (sg_get_kvm() == NULL) {
518 >                return -1;
519          }
520 < #endif
521 < #ifdef NETBSD
334 <        {
335 <                struct uvmexp *uvm = get_uvmexp();
336 <                if (uvm == NULL) return 1;
520 >        if (sg_get_kvm2() == NULL) {
521 >                return -1;
522          }
523   #endif
524   #ifdef SOLARIS
525 +        /* On solaris 7, this will fail if you are not root. But, everything
526 +         * will still work, just no disk mappings. So we will ignore the exit
527 +         * status of this, and carry on merrily.
528 +         */
529 + #ifdef HAVE_LIBDEVINFO_H
530          build_mapping();
531   #endif
532 + #endif
533          return 0;
534 + }
535 +
536 + int sg_drop_privileges() {
537 + #ifdef HAVE_SETEGID
538 +        if (setegid(getgid()) != 0) {
539 + #elif defined(HAVE_SETRESGID)
540 +        if (setresgid(getgid(), getgid(), getgid()) != 0) {
541 + #else
542 +        {
543 + #endif
544 +                sg_set_error_with_errno(SG_ERROR_SETEGID, NULL);
545 +                return -1;
546 +        }
547 + #ifdef HAVE_SETEUID
548 +        if (seteuid(getuid()) != 0) {
549 + #elif defined(HAVE_SETRESUID)
550 +        if (setresuid(getuid(), getuid(), getuid()) != 0) {
551 + #else
552 +        {
553 + #endif
554 +                sg_set_error_with_errno(SG_ERROR_SETEUID, NULL);
555 +                return -1;
556 +        }
557 +        return 0;
558 + }
559 +
560 + void *sg_realloc(void *ptr, size_t size) {
561 +        void *tmp = NULL;
562 +        tmp = realloc(ptr, size);
563 +        if(tmp == NULL) {
564 +                sg_set_error_with_errno(SG_ERROR_MALLOC, NULL);
565 +        }
566 +        return tmp;
567   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines