lib/util: Allow calloc use in util.c, too.
[ab/samba.git/.git] / lib / util / system.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba system utilities
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 1998-2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/network.h"
23 #include "system/filesys.h"
24
25 #undef malloc
26
27 /*
28    The idea is that this file will eventually have wrappers around all
29    important system calls in samba. The aims are:
30
31    - to enable easier porting by putting OS dependent stuff in here
32
33    - to allow for hooks into other "pseudo-filesystems"
34
35    - to allow easier integration of things like the japanese extensions
36
37    - to support the philosophy of Samba to expose the features of
38      the OS within the SMB model. In general whatever file/printer/variable
39      expansions/etc make sense to the OS should be acceptable to Samba.
40 */
41
42
43 /**************************************************************************
44  Wrapper for fork. Ensures we clear our pid cache.
45 ****************************************************************************/
46
47 static pid_t mypid = (pid_t)-1;
48
49 _PUBLIC_ pid_t sys_fork(void)
50 {
51         pid_t forkret = fork();
52
53         if (forkret == (pid_t)0) {
54                 /* Child - reset mypid so sys_getpid does a system call. */
55                 mypid = (pid_t) -1;
56         }
57
58         return forkret;
59 }
60
61 /**************************************************************************
62  Wrapper for getpid. Ensures we only do a system call *once*.
63 ****************************************************************************/
64
65 _PUBLIC_ pid_t sys_getpid(void)
66 {
67         if (mypid == (pid_t)-1)
68                 mypid = getpid();
69
70         return mypid;
71 }
72
73
74 _PUBLIC_ int sys_getnameinfo(const struct sockaddr *psa,
75                              int salen,
76                              char *host,
77                              size_t hostlen,
78                              char *service,
79                              size_t servlen,
80                              int flags)
81 {
82         /*
83          * For Solaris we must make sure salen is the
84          * correct length for the incoming sa_family.
85          */
86
87         if (salen == sizeof(struct sockaddr_storage)) {
88                 salen = sizeof(struct sockaddr_in);
89 #if defined(HAVE_IPV6)
90                 if (psa->sa_family == AF_INET6) {
91                         salen = sizeof(struct sockaddr_in6);
92                 }
93 #endif
94         }
95         return getnameinfo(psa, salen, host, hostlen, service, servlen, flags);
96 }