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.
24 // In Mac OS X 10.5 and later trying to use the daemon function gives a “‘daemon’ is deprecated”
25 // error, which prevents compilation because we build with "-Werror".
26 // Since this is supposed to be portable cross-platform code, we don't care that daemon is
27 // deprecated on Mac OS X 10.5, so we use this preprocessor trick to eliminate the error message.
28 #define daemon yes_we_know_that_daemon_is_deprecated_in_os_x_10_5_thankyou
39 #include <sys/types.h>
43 extern int daemon(int, int);
46 #include "mDNSEmbeddedAPI.h"
47 #include "mDNSPosix.h"
48 #include "mDNSUNP.h" // For daemon()
49 #include "uds_daemon.h"
50 #include "PlatformCommon.h"
52 #define CONFIG_FILE "/etc/mdnsd.conf"
53 static domainname DynDNSZone
; // Default wide-area zone for service registration
54 static domainname DynDNSHostname
;
56 #define RR_CACHE_SIZE 500
57 static CacheEntity gRRCache
[RR_CACHE_SIZE
];
58 static mDNS_PlatformSupport PlatformStorage
;
60 mDNSlocal
void mDNS_StatusCallback(mDNS
*const m
, mStatus result
)
63 if (result
== mStatus_NoError
)
65 // On successful registration of dot-local mDNS host name, daemon may want to check if
66 // any name conflict and automatic renaming took place, and if so, record the newly negotiated
67 // name in persistent storage for next time. It should also inform the user of the name change.
68 // On Mac OS X we store the current dot-local mDNS host name in the SCPreferences store,
69 // and notify the user with a CFUserNotification.
71 else if (result
== mStatus_ConfigChanged
)
73 udsserver_handle_configchange(m
);
75 else if (result
== mStatus_GrowCache
)
77 // Allocate another chunk of cache storage
78 CacheEntity
*storage
= malloc(sizeof(CacheEntity
) * RR_CACHE_SIZE
);
79 if (storage
) mDNS_GrowCache(m
, storage
, RR_CACHE_SIZE
);
83 // %%% Reconfigure() probably belongs in the platform support layer (mDNSPosix.c), not the daemon cde
84 // -- all client layers running on top of mDNSPosix.c need to handle network configuration changes,
85 // not only the Unix Domain Socket Daemon
87 static void Reconfigure(mDNS
*m
)
90 const mDNSAddr dummy
= { mDNSAddrType_IPv4
, { { { 1, 1, 1, 1 } } } };;
91 mDNS_SetPrimaryInterfaceInfo(m
, NULL
, NULL
, NULL
);
92 if (ParseDNSServers(m
, uDNS_SERVERS_FILE
) < 0)
93 LogMsg("Unable to parse DNS server list. Unicast DNS-SD unavailable");
94 ReadDDNSSettingsFromConfFile(m
, CONFIG_FILE
, &DynDNSHostname
, &DynDNSZone
, NULL
);
95 mDNSPlatformSourceAddrForDest(&DynDNSIP
, &dummy
);
96 if (DynDNSHostname
.c
[0]) mDNS_AddDynDNSHostName(m
, &DynDNSHostname
, NULL
, NULL
);
97 if (DynDNSIP
.type
) mDNS_SetPrimaryInterfaceInfo(m
, &DynDNSIP
, NULL
, NULL
);
98 mDNS_ConfigChanged(m
);
101 // Do appropriate things at startup with command line arguments. Calls exit() if unhappy.
102 mDNSlocal
void ParseCmdLinArgs(int argc
, char **argv
)
106 if (0 == strcmp(argv
[1], "-debug")) mDNS_DebugMode
= mDNStrue
;
107 else printf("Usage: %s [-debug]\n", argv
[0]);
112 int result
= daemon(0, 0);
113 if (result
!= 0) { LogMsg("Could not run as daemon - exiting"); exit(result
); }
115 LogMsg("The POSIX mdnsd should only be used on OS X for testing - exiting");
121 mDNSlocal
void DumpStateLog(mDNS
*const m
)
122 // Dump a little log of what we've been up to.
124 LogMsg("---- BEGIN STATE LOG ----");
126 LogMsg("---- END STATE LOG ----");
129 mDNSlocal mStatus
MainLoop(mDNS
*m
) // Loop until we quit.
132 mDNSBool gotData
= mDNSfalse
;
134 mDNSPosixListenForSignalInEventLoop(SIGINT
);
135 mDNSPosixListenForSignalInEventLoop(SIGTERM
);
136 mDNSPosixListenForSignalInEventLoop(SIGUSR1
);
137 mDNSPosixListenForSignalInEventLoop(SIGPIPE
);
138 mDNSPosixListenForSignalInEventLoop(SIGHUP
) ;
142 // Work out how long we expect to sleep before the next scheduled task
143 struct timeval timeout
;
146 // Only idle if we didn't find any data the last time around
149 mDNSs32 nextTimerEvent
= mDNS_Execute(m
);
150 nextTimerEvent
= udsserver_idle(nextTimerEvent
);
151 ticks
= nextTimerEvent
- mDNS_TimeNow(m
);
152 if (ticks
< 1) ticks
= 1;
154 else // otherwise call EventLoop again with 0 timemout
157 timeout
.tv_sec
= ticks
/ mDNSPlatformOneSecond
;
158 timeout
.tv_usec
= (ticks
% mDNSPlatformOneSecond
) * 1000000 / mDNSPlatformOneSecond
;
160 (void) mDNSPosixRunEventLoopOnce(m
, &timeout
, &signals
, &gotData
);
162 if (sigismember(&signals
, SIGHUP
)) Reconfigure(m
);
163 if (sigismember(&signals
, SIGUSR1
)) DumpStateLog(m
);
164 // SIGPIPE happens when we try to write to a dead client; death should be detected soon in request_callback() and cleaned up.
165 if (sigismember(&signals
, SIGPIPE
)) LogMsg("Received SIGPIPE - ignoring");
166 if (sigismember(&signals
, SIGINT
) || sigismember(&signals
, SIGTERM
)) break;
171 int main(int argc
, char **argv
)
175 ParseCmdLinArgs(argc
, argv
);
177 LogMsg("%s starting", mDNSResponderVersionString
);
179 err
= mDNS_Init(&mDNSStorage
, &PlatformStorage
, gRRCache
, RR_CACHE_SIZE
, mDNS_Init_AdvertiseLocalAddresses
,
180 mDNS_StatusCallback
, mDNS_Init_NoInitCallbackContext
);
182 if (mStatus_NoError
== err
)
183 err
= udsserver_init(mDNSNULL
, 0);
185 Reconfigure(&mDNSStorage
);
187 // Now that we're finished with anything privileged, switch over to running as "nobody"
188 if (mStatus_NoError
== err
)
190 const struct passwd
*pw
= getpwnam("nobody");
194 LogMsg("WARNING: mdnsd continuing as root because user \"nobody\" does not exist");
197 if (mStatus_NoError
== err
)
198 err
= MainLoop(&mDNSStorage
);
200 LogMsg("%s stopping", mDNSResponderVersionString
);
202 mDNS_Close(&mDNSStorage
);
204 if (udsserver_exit() < 0)
205 LogMsg("ExitCallback: udsserver_exit failed");
207 #if MDNS_DEBUGMSGS > 0
208 printf("mDNSResponder exiting normally with %ld\n", err
);
214 // uds_daemon support ////////////////////////////////////////////////////////////
216 mStatus
udsSupportAddFDToEventLoop(int fd
, udsEventCallback callback
, void *context
, void **platform_data
)
217 /* Support routine for uds_daemon.c */
219 // Depends on the fact that udsEventCallback == mDNSPosixEventCallback
220 (void) platform_data
;
221 return mDNSPosixAddFDToEventLoop(fd
, callback
, context
);
224 int udsSupportReadFD(dnssd_sock_t fd
, char *buf
, int len
, int flags
, void *platform_data
)
226 (void) platform_data
;
227 return recv(fd
, buf
, len
, flags
);
230 mStatus
udsSupportRemoveFDFromEventLoop(int fd
, void *platform_data
) // Note: This also CLOSES the file descriptor
232 mStatus err
= mDNSPosixRemoveFDFromEventLoop(fd
);
233 (void) platform_data
;
238 mDNSexport
void RecordUpdatedNiceLabel(mDNS
*const m
, mDNSs32 delay
)
245 #if _BUILDING_XCODE_PROJECT_
246 // If the process crashes, then this string will be magically included in the automatically-generated crash log
247 const char *__crashreporter_info__
= mDNSResponderVersionString_SCCS
+ 5;
248 asm (".desc ___crashreporter_info__, 0x10");
251 // For convenience when using the "strings" command, this is the last thing in the file
252 #if mDNSResponderVersion > 1
253 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder-" STRINGIFY(mDNSResponderVersion
) " (" __DATE__
" " __TIME__
")";
254 #elif MDNS_VERSIONSTR_NODTS
255 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder (Engineering Build)";
257 mDNSexport
const char mDNSResponderVersionString_SCCS
[] = "@(#) mDNSResponder (Engineering Build) (" __DATE__
" " __TIME__
")";