~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to lib/util/system.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/*
 
26
   The idea is that this file will eventually have wrappers around all
 
27
   important system calls in samba. The aims are:
 
28
 
 
29
   - to enable easier porting by putting OS dependent stuff in here
 
30
 
 
31
   - to allow for hooks into other "pseudo-filesystems"
 
32
 
 
33
   - to allow easier integration of things like the japanese extensions
 
34
 
 
35
   - to support the philosophy of Samba to expose the features of
 
36
     the OS within the SMB model. In general whatever file/printer/variable
 
37
     expansions/etc make sense to the OS should be acceptable to Samba.
 
38
*/
 
39
 
 
40
/**************************************************************************
 
41
A wrapper for gethostbyname() that tries avoids looking up hostnames 
 
42
in the root domain, which can cause dial-on-demand links to come up for no
 
43
apparent reason.
 
44
****************************************************************************/
 
45
 
 
46
_PUBLIC_ struct hostent *sys_gethostbyname(const char *name)
 
47
{
 
48
#ifdef REDUCE_ROOT_DNS_LOOKUPS
 
49
        char query[256], hostname[256];
 
50
        char *domain;
 
51
 
 
52
        /* Does this name have any dots in it? If so, make no change */
 
53
 
 
54
        if (strchr(name, '.'))
 
55
                return(gethostbyname(name));
 
56
 
 
57
        /* Get my hostname, which should have domain name 
 
58
                attached. If not, just do the gethostname on the
 
59
                original string. 
 
60
        */
 
61
 
 
62
        gethostname(hostname, sizeof(hostname) - 1);
 
63
        hostname[sizeof(hostname) - 1] = 0;
 
64
        if ((domain = strchr(hostname, '.')) == NULL)
 
65
                return(gethostbyname(name));
 
66
 
 
67
        /* Attach domain name to query and do modified query.
 
68
                If names too large, just do gethostname on the
 
69
                original string.
 
70
        */
 
71
 
 
72
        if((strlen(name) + strlen(domain)) >= sizeof(query))
 
73
                return(gethostbyname(name));
 
74
 
 
75
        slprintf(query, sizeof(query)-1, "%s%s", name, domain);
 
76
        return(gethostbyname(query));
 
77
#else /* REDUCE_ROOT_DNS_LOOKUPS */
 
78
        return(gethostbyname(name));
 
79
#endif /* REDUCE_ROOT_DNS_LOOKUPS */
 
80
}
 
81
 
 
82
_PUBLIC_ struct in_addr sys_inet_makeaddr(int net, int host)
 
83
{
 
84
        struct in_addr in;
 
85
        struct in_addr in2;
 
86
        in = inet_makeaddr(net, host);
 
87
        in2.s_addr = in.s_addr;
 
88
        return in2;
 
89
}
 
90
 
 
91
/**************************************************************************
 
92
 Wrapper for fork. Ensures we clear our pid cache.
 
93
****************************************************************************/
 
94
 
 
95
static pid_t mypid = (pid_t)-1;
 
96
 
 
97
_PUBLIC_ pid_t sys_fork(void)
 
98
{
 
99
        pid_t forkret = fork();
 
100
 
 
101
        if (forkret == (pid_t)0) {
 
102
                /* Child - reset mypid so sys_getpid does a system call. */
 
103
                mypid = (pid_t) -1;
 
104
        }
 
105
 
 
106
        return forkret;
 
107
}
 
108
 
 
109
/**************************************************************************
 
110
 Wrapper for getpid. Ensures we only do a system call *once*.
 
111
****************************************************************************/
 
112
 
 
113
_PUBLIC_ pid_t sys_getpid(void)
 
114
{
 
115
        if (mypid == (pid_t)-1)
 
116
                mypid = getpid();
 
117
 
 
118
        return mypid;
 
119
}