1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 Contains: main & associated Application layer for mDNSResponder on Linux.
21 Change History (most recent first):
23 $Log: PosixDaemon.c,v $
24 Revision 1.42 2007/09/18 19:09:02 cheshire
25 <rdar://problem/5489549> mDNSResponderHelper (and other binaries) missing SCCS version strings
27 Revision 1.41 2007/09/04 17:02:25 cheshire
28 <rdar://problem/5458929> False positives in changed files list in nightly builds
29 Added MDNS_VERSIONSTR_NODTS option at the reqest of Rishi Srivatsavai (Sun)
31 Revision 1.40 2007/07/31 23:08:34 mcguire
32 <rdar://problem/5329542> BTMM: Make AutoTunnel mode work with multihoming
34 Revision 1.39 2007/03/21 00:30:44 cheshire
35 Remove obsolete mDNS_DeleteDNSServers() call
37 Revision 1.38 2007/02/14 01:58:19 cheshire
38 <rdar://problem/4995831> Don't delete Unix Domain Socket on exit if we didn't create it on startup
40 Revision 1.37 2007/02/07 19:32:00 cheshire
41 <rdar://problem/4980353> All mDNSResponder components should contain version strings in SCCS-compatible format
43 Revision 1.36 2007/02/06 19:06:48 cheshire
44 <rdar://problem/3956518> Need to go native with launchd
46 Revision 1.35 2007/01/05 08:30:52 cheshire
47 Trim excessive "$Log" checkin history from before 2006
48 (checkin history still available via "cvs log ..." of course)
50 Revision 1.34 2007/01/05 05:46:08 cheshire
51 Add mDNS *const m parameter to udsserver_handle_configchange()
53 Revision 1.33 2006/12/21 00:10:53 cheshire
54 Make mDNS_PlatformSupport PlatformStorage a static global instead of a stack variable
56 Revision 1.32 2006/11/03 22:28:50 cheshire
57 PosixDaemon needs to handle mStatus_ConfigChanged and mStatus_GrowCache messages
59 Revision 1.31 2006/08/14 23:24:46 cheshire
60 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
62 Revision 1.30 2006/07/07 01:09:12 cheshire
63 <rdar://problem/4472013> Add Private DNS server functionality to dnsextd
64 Only use mallocL/freeL debugging routines when building mDNSResponder, not dnsextd
76 #include <sys/types.h>
78 #include "mDNSEmbeddedAPI.h"
79 #include "mDNSDebug.h"
80 #include "mDNSPosix.h"
81 #include "uds_daemon.h"
82 #include "PlatformCommon.h"
84 #define CONFIG_FILE "/etc/mdnsd.conf"
85 static domainname DynDNSZone
; // Default wide-area zone for service registration
86 static domainname DynDNSHostname
;
88 #define RR_CACHE_SIZE 500
89 static CacheEntity gRRCache
[RR_CACHE_SIZE
];
90 static mDNS_PlatformSupport PlatformStorage
;
92 mDNSlocal
void mDNS_StatusCallback(mDNS
*const m
, mStatus result
)
95 if (result
== mStatus_NoError
)
97 // On successful registration of dot-local mDNS host name, daemon may want to check if
98 // any name conflict and automatic renaming took place, and if so, record the newly negotiated
99 // name in persistent storage for next time. It should also inform the user of the name change.
100 // On Mac OS X we store the current dot-local mDNS host name in the SCPreferences store,
101 // and notify the user with a CFUserNotification.
103 else if (result
== mStatus_ConfigChanged
)
105 udsserver_handle_configchange(m
);
107 else if (result
== mStatus_GrowCache
)
109 // Allocate another chunk of cache storage
110 CacheEntity
*storage
= malloc(sizeof(CacheEntity
) * RR_CACHE_SIZE
);
111 if (storage
) mDNS_GrowCache(m
, storage
, RR_CACHE_SIZE
);
115 // %%% Reconfigure() probably belongs in the platform support layer (mDNSPosix.c), not the daemon cde
116 // -- all client layers running on top of mDNSPosix.c need to handle network configuration changes,
117 // not only the Unix Domain Socket Daemon
119 static void Reconfigure(mDNS
*m
)
122 mDNS_SetPrimaryInterfaceInfo(m
, NULL
, NULL
, NULL
);
123 if (ParseDNSServers(m
, uDNS_SERVERS_FILE
) < 0)
124 LogMsg("Unable to parse DNS server list. Unicast DNS-SD unavailable");
125 ReadDDNSSettingsFromConfFile(m
, CONFIG_FILE
, &DynDNSHostname
, &DynDNSZone
, NULL
);
126 FindSourceAddrForIP(NULL
, &DynDNSIP
);
127 if (DynDNSHostname
.c
[0]) mDNS_AddDynDNSHostName(m
, &DynDNSHostname
, NULL
, NULL
);
128 if (DynDNSIP
.type
) mDNS_SetPrimaryInterfaceInfo(m
, &DynDNSIP
, NULL
, NULL
);
129 m
->MainCallback(m
, mStatus_ConfigChanged
);
132 // Do appropriate things at startup with command line arguments. Calls exit() if unhappy.
133 mDNSlocal
void ParseCmdLinArgs(int argc
, char **argv
)
137 if (0 == strcmp(argv
[1], "-debug")) mDNS_DebugMode
= mDNStrue
;
138 else printf("Usage: %s [-debug]\n", argv
[0]);
143 int result
= daemon(0, 0);
144 if (result
!= 0) { LogMsg("Could not run as daemon - exiting"); exit(result
); }
146 LogMsg("The POSIX mdnsd should only be used on OS X for testing - exiting");
152 mDNSlocal
void DumpStateLog(mDNS
*const m
)
153 // Dump a little log of what we've been up to.
155 LogMsgIdent(mDNSResponderVersionString
, "---- BEGIN STATE LOG ----");
157 LogMsgIdent(mDNSResponderVersionString
, "---- END STATE LOG ----");
160 mDNSlocal mStatus
MainLoop(mDNS
*m
) // Loop until we quit.
163 mDNSBool gotData
= mDNSfalse
;
165 mDNSPosixListenForSignalInEventLoop(SIGINT
);
166 mDNSPosixListenForSignalInEventLoop(SIGTERM
);
167 mDNSPosixListenForSignalInEventLoop(SIGUSR1
);
168 mDNSPosixListenForSignalInEventLoop(SIGPIPE
);
169 mDNSPosixListenForSignalInEventLoop(SIGHUP
) ;
173 // Work out how long we expect to sleep before the next scheduled task
174 struct timeval timeout
;
177 // Only idle if we didn't find any data the last time around
180 mDNSs32 nextTimerEvent
= mDNS_Execute(m
);
181 nextTimerEvent
= udsserver_idle(nextTimerEvent
);
182 ticks
= nextTimerEvent
- mDNS_TimeNow(m
);
183 if (ticks
< 1) ticks
= 1;
185 else // otherwise call EventLoop again with 0 timemout
188 timeout
.tv_sec
= ticks
/ mDNSPlatformOneSecond
;
189 timeout
.tv_usec
= (ticks
% mDNSPlatformOneSecond
) * 1000000 / mDNSPlatformOneSecond
;
191 (void) mDNSPosixRunEventLoopOnce(m
, &timeout
, &signals
, &gotData
);
193 if (sigismember(&signals
, SIGHUP
)) Reconfigure(m
);
194 if (sigismember(&signals
, SIGUSR1
)) DumpStateLog(m
);
195 // SIGPIPE happens when we try to write to a dead client; death should be detected soon in request_callback() and cleaned up.
196 if (sigismember(&signals
, SIGPIPE
)) LogMsg("Received SIGPIPE - ignoring");
197 if (sigismember(&signals
, SIGINT
) || sigismember(&signals
, SIGTERM
)) break;
202 int main(int argc
, char **argv
)
206 ParseCmdLinArgs(argc
, argv
);
208 LogMsgIdent(mDNSResponderVersionString
, "starting");
210 err
= mDNS_Init(&mDNSStorage
, &PlatformStorage
, gRRCache
, RR_CACHE_SIZE
, mDNS_Init_AdvertiseLocalAddresses
,
211 mDNS_StatusCallback
, mDNS_Init_NoInitCallbackContext
);
213 if (mStatus_NoError
== err
)
214 err
= udsserver_init(dnssd_InvalidSocket
);
216 Reconfigure(&mDNSStorage
);
218 // Now that we're finished with anything privileged, switch over to running as "nobody"
219 if (mStatus_NoError
== err
)
221 const struct passwd
*pw
= getpwnam("nobody");
225 LogMsg("WARNING: mdnsd continuing as root because user \"nobody\" does not exist");
228 if (mStatus_NoError
== err
)
229 err
= MainLoop(&mDNSStorage
);
231 LogMsgIdent(mDNSResponderVersionString
, "stopping");
233 mDNS_Close(&mDNSStorage
);
235 if (udsserver_exit(dnssd_InvalidSocket
) < 0)
236 LogMsg("ExitCallback: udsserver_exit failed");
238 #if MDNS_DEBUGMSGS > 0
239 printf("mDNSResponder exiting normally with %ld\n", err
);
245 // uds_daemon support ////////////////////////////////////////////////////////////
247 mStatus
udsSupportAddFDToEventLoop(int fd
, udsEventCallback callback
, void *context
)
248 /* Support routine for uds_daemon.c */
250 // Depends on the fact that udsEventCallback == mDNSPosixEventCallback
251 return mDNSPosixAddFDToEventLoop(fd
, callback
, context
);
254 mStatus
udsSupportRemoveFDFromEventLoop(int fd
) // Note: This also CLOSES the file descriptor
256 mStatus err
= mDNSPosixRemoveFDFromEventLoop(fd
);
261 mDNSexport
void RecordUpdatedNiceLabel(mDNS
*const m
, mDNSs32 delay
)
268 // If the process crashes, then this string will be magically included in the automatically-generated crash log
269 const char *__crashreporter_info__
= mDNSResponderVersionString_SCCS
+ 5;
270 asm(".desc ___crashreporter_info__, 0x10");
272 // For convenience when using the "strings" command, this is the last thing in the file
273 #if mDNSResponderVersion > 1
274 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder-" STRINGIFY(mDNSResponderVersion
) " (" __DATE__
" " __TIME__
")";
275 #elif MDNS_VERSIONSTR_NODTS
276 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder (Engineering Build)";
278 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder (Engineering Build) (" __DATE__
" " __TIME__
")";