1 /* -*- Mode: C; tab-width: 4 -*- 2 * 3 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 17 File: daemon.c 18 19 Contains: main & associated Application layer for mDNSResponder on Linux. 20 21 */ 22 23 #if __APPLE__ 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 29 #endif 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <unistd.h> 34 #include <stdlib.h> 35 #include <signal.h> 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <pwd.h> 39 #include <sys/types.h> 40 41 #if __APPLE__ 42 #undef daemon 43 extern int daemon(int, int); 44 #endif 45 46 #include "mDNSEmbeddedAPI.h" 47 #include "mDNSPosix.h" 48 #include "mDNSUNP.h" // For daemon() 49 #include "uds_daemon.h" 50 #include "DNSCommon.h" 51 #include "PlatformCommon.h" 52 53 #ifndef MDNSD_USER 54 #define MDNSD_USER "nobody" 55 #endif 56 57 #define CONFIG_FILE "/etc/mdnsd.conf" 58 static domainname DynDNSZone; // Default wide-area zone for service registration 59 static domainname DynDNSHostname; 60 61 #define RR_CACHE_SIZE 500 62 static CacheEntity gRRCache[RR_CACHE_SIZE]; 63 static mDNS_PlatformSupport PlatformStorage; 64 65 mDNSlocal void mDNS_StatusCallback(mDNS *const m, mStatus result) 66 { 67 (void)m; // Unused 68 if (result == mStatus_NoError) 69 { 70 // On successful registration of dot-local mDNS host name, daemon may want to check if 71 // any name conflict and automatic renaming took place, and if so, record the newly negotiated 72 // name in persistent storage for next time. It should also inform the user of the name change. 73 // On Mac OS X we store the current dot-local mDNS host name in the SCPreferences store, 74 // and notify the user with a CFUserNotification. 75 } 76 else if (result == mStatus_ConfigChanged) 77 { 78 udsserver_handle_configchange(m); 79 } 80 else if (result == mStatus_GrowCache) 81 { 82 // Allocate another chunk of cache storage 83 CacheEntity *storage = malloc(sizeof(CacheEntity) * RR_CACHE_SIZE); 84 if (storage) mDNS_GrowCache(m, storage, RR_CACHE_SIZE); 85 } 86 } 87 88 // %%% Reconfigure() probably belongs in the platform support layer (mDNSPosix.c), not the daemon cde 89 // -- all client layers running on top of mDNSPosix.c need to handle network configuration changes, 90 // not only the Unix Domain Socket Daemon 91 92 static void Reconfigure(mDNS *m) 93 { 94 mDNSAddr DynDNSIP; 95 // Use a random address from TEST-NET-2 in RFC5737 96 const mDNSAddr dummy = { mDNSAddrType_IPv4, { { { 198, 51, 100, 42 } } } };; 97 mDNS_SetPrimaryInterfaceInfo(m, NULL, NULL, NULL); 98 mDNS_Lock(m); 99 if (ParseDNSServers(m, uDNS_SERVERS_FILE) < 0) 100 LogMsg("Unable to parse DNS server list. Unicast DNS-SD unavailable"); 101 mDNS_Unlock(m); 102 ReadDDNSSettingsFromConfFile(m, CONFIG_FILE, &DynDNSHostname, &DynDNSZone, NULL); 103 mDNSPlatformSourceAddrForDest(&DynDNSIP, &dummy); 104 if (DynDNSHostname.c[0]) mDNS_AddDynDNSHostName(m, &DynDNSHostname, NULL, NULL); 105 if (DynDNSIP.type) mDNS_SetPrimaryInterfaceInfo(m, &DynDNSIP, NULL, NULL); 106 mDNS_ConfigChanged(m); 107 } 108 109 // Do appropriate things at startup with command line arguments. Calls exit() if unhappy. 110 mDNSlocal void ParseCmdLinArgs(int argc, char **argv) 111 { 112 if (argc > 1) 113 { 114 if (0 == strcmp(argv[1], "-debug")) mDNS_DebugMode = mDNStrue; 115 else printf("Usage: %s [-debug]\n", argv[0]); 116 } 117 118 if (!mDNS_DebugMode) 119 { 120 int result = daemon(0, 0); 121 if (result != 0) { LogMsg("Could not run as daemon - exiting"); exit(result); } 122 #if __APPLE__ 123 LogMsg("The POSIX mdnsd should only be used on OS X for testing - exiting"); 124 exit(-1); 125 #endif 126 } 127 } 128 129 mDNSlocal void DumpStateLog(mDNS *const m) 130 // Dump a little log of what we've been up to. 131 { 132 DNSServer *s; 133 PosixNetworkInterface *i; 134 135 LogMsg("---- BEGIN STATE LOG ----"); 136 udsserver_info(m); 137 138 LogMsgNoIdent("----- Network Interfaces -------"); 139 for (i = (PosixNetworkInterface*)(m->HostInterfaces); 140 i; i = (PosixNetworkInterface *)(i->coreIntf.next)) { 141 LogMsg("%p %p %d %s%s%s%s%s %-8s %#a", i, 142 (void *)(i->coreIntf.InterfaceID), i->index, 143 i->coreIntf.InterfaceActive ? "-" : "D", 144 i->coreIntf.IPv4Available ? "4" : "-", 145 i->coreIntf.IPv6Available ? "6" : "-", 146 i->coreIntf.Advertise ? "A" : "-", 147 i->coreIntf.McastTxRx ? "M" : "-", 148 i->intfName, &(i->coreIntf.ip)); 149 } 150 151 LogMsgNoIdent("--------- DNS Servers ----------"); 152 if (!mDNSStorage.DNSServers) LogMsgNoIdent("<None>"); 153 else 154 { 155 for (s = m->DNSServers; s; s = s->next) 156 { 157 LogMsgNoIdent("DNS Server %##s %#a:%d %s", 158 s->domain.c, &s->addr, mDNSVal16(s->port), 159 s->teststate == DNSServer_Untested ? "(Untested)" : 160 s->teststate == DNSServer_Passed ? "" : 161 s->teststate == DNSServer_Failed ? "(Failed)" : 162 s->teststate == DNSServer_Disabled ? "(Disabled)" : "(Unknown state)"); 163 } 164 } 165 166 LogMsg("---- END STATE LOG ----"); 167 } 168 169 mDNSlocal mStatus MainLoop(mDNS *m) // Loop until we quit. 170 { 171 sigset_t signals; 172 mDNSBool gotData = mDNSfalse; 173 174 mDNSPosixListenForSignalInEventLoop(SIGINT); 175 mDNSPosixListenForSignalInEventLoop(SIGTERM); 176 mDNSPosixListenForSignalInEventLoop(SIGUSR1); 177 #ifdef HAVE_SIGINFO 178 mDNSPosixListenForSignalInEventLoop(SIGUSR2); 179 mDNSPosixListenForSignalInEventLoop(SIGINFO); 180 #endif 181 mDNSPosixListenForSignalInEventLoop(SIGPIPE); 182 mDNSPosixListenForSignalInEventLoop(SIGHUP) ; 183 184 for (; ;) 185 { 186 // Work out how long we expect to sleep before the next scheduled task 187 struct timeval timeout; 188 mDNSs32 ticks; 189 190 // Only idle if we didn't find any data the last time around 191 if (!gotData) 192 { 193 mDNSs32 nextTimerEvent = mDNS_Execute(m); 194 nextTimerEvent = udsserver_idle(nextTimerEvent); 195 ticks = nextTimerEvent - mDNS_TimeNow(m); 196 if (ticks < 1) ticks = 1; 197 } 198 else // otherwise call EventLoop again with 0 timemout 199 ticks = 0; 200 201 timeout.tv_sec = ticks / mDNSPlatformOneSecond; 202 timeout.tv_usec = (ticks % mDNSPlatformOneSecond) * 1000000 / mDNSPlatformOneSecond; 203 204 (void) mDNSPosixRunEventLoopOnce(m, &timeout, &signals, &gotData); 205 206 if (sigismember(&signals, SIGHUP )) Reconfigure(m); 207 #ifdef HAVE_SIGINFO 208 /* use OSX-compatible signals since we can, and gain enhanced debugging */ 209 if (sigismember(&signals, SIGINFO)) DumpStateLog(m); 210 if (sigismember(&signals, SIGUSR1)) 211 { 212 mDNS_LoggingEnabled = mDNS_LoggingEnabled ? 0 : 1; 213 LogMsg("SIGUSR1: Logging %s", mDNS_LoggingEnabled ? "Enabled" : "Disabled"); 214 } 215 if (sigismember(&signals, SIGUSR2)) 216 { 217 mDNS_PacketLoggingEnabled = mDNS_PacketLoggingEnabled ? 0 : 1; 218 LogMsg("SIGUSR2: Packet Logging %s", mDNS_PacketLoggingEnabled ? "Enabled" : "Disabled"); 219 } 220 #else 221 if (sigismember(&signals, SIGUSR1)) DumpStateLog(m); 222 #endif 223 // SIGPIPE happens when we try to write to a dead client; death should be detected soon in request_callback() and cleaned up. 224 if (sigismember(&signals, SIGPIPE)) LogMsg("Received SIGPIPE - ignoring"); 225 if (sigismember(&signals, SIGINT) || sigismember(&signals, SIGTERM)) break; 226 } 227 return EINTR; 228 } 229 230 int main(int argc, char **argv) 231 { 232 mStatus err; 233 234 ParseCmdLinArgs(argc, argv); 235 236 LogInfo("%s starting", mDNSResponderVersionString); 237 238 err = mDNS_Init(&mDNSStorage, &PlatformStorage, gRRCache, RR_CACHE_SIZE, mDNS_Init_AdvertiseLocalAddresses, 239 mDNS_StatusCallback, mDNS_Init_NoInitCallbackContext); 240 241 if (mStatus_NoError == err) 242 err = udsserver_init(mDNSNULL, 0); 243 244 Reconfigure(&mDNSStorage); 245 246 // Now that we're finished with anything privileged, switch over to running as "nobody" 247 if (mStatus_NoError == err) 248 { 249 const struct passwd *pw = getpwnam(MDNSD_USER); 250 if (pw != NULL) 251 { 252 setgid(pw->pw_gid); 253 setuid(pw->pw_uid); 254 } 255 else 256 #ifdef MDNSD_NOROOT 257 { 258 LogMsg("WARNING: mdnsd exiting because user \""MDNSD_USER"\" does not exist"); 259 err = mStatus_Invalid; 260 } 261 #else 262 LogMsg("WARNING: mdnsd continuing as root because user \""MDNSD_USER"\" does not exist"); 263 #endif 264 } 265 266 if (mStatus_NoError == err) 267 err = MainLoop(&mDNSStorage); 268 269 LogInfo("%s stopping", mDNSResponderVersionString); 270 271 mDNS_Close(&mDNSStorage); 272 273 if (udsserver_exit() < 0) 274 LogMsg("ExitCallback: udsserver_exit failed"); 275 276 #if MDNS_DEBUGMSGS > 0 277 printf("mDNSResponder exiting normally with %ld\n", err); 278 #endif 279 280 return err; 281 } 282 283 // uds_daemon support //////////////////////////////////////////////////////////// 284 285 mStatus udsSupportAddFDToEventLoop(int fd, udsEventCallback callback, void *context, void **platform_data) 286 /* Support routine for uds_daemon.c */ 287 { 288 // Depends on the fact that udsEventCallback == mDNSPosixEventCallback 289 (void) platform_data; 290 return mDNSPosixAddFDToEventLoop(fd, callback, context); 291 } 292 293 int udsSupportReadFD(dnssd_sock_t fd, char *buf, int len, int flags, void *platform_data) 294 { 295 (void) platform_data; 296 return recv(fd, buf, len, flags); 297 } 298 299 mStatus udsSupportRemoveFDFromEventLoop(int fd, void *platform_data) // Note: This also CLOSES the file descriptor 300 { 301 mStatus err = mDNSPosixRemoveFDFromEventLoop(fd); 302 (void) platform_data; 303 close(fd); 304 return err; 305 } 306 307 mDNSexport void RecordUpdatedNiceLabel(mDNS *const m, mDNSs32 delay) 308 { 309 (void)m; 310 (void)delay; 311 // No-op, for now 312 } 313 314 #if _BUILDING_XCODE_PROJECT_ 315 // If the process crashes, then this string will be magically included in the automatically-generated crash log 316 const char *__crashreporter_info__ = mDNSResponderVersionString_SCCS + 5; 317 asm(".desc ___crashreporter_info__, 0x10"); 318 #endif 319 320 // For convenience when using the "strings" command, this is the last thing in the file 321 #if mDNSResponderVersion > 1 322 mDNSexport const char mDNSResponderVersionString_SCCS[] = "@(#) mDNSResponder-" STRINGIFY(mDNSResponderVersion); 323 #elif MDNS_VERSIONSTR_NODTS 324 mDNSexport const char mDNSResponderVersionString_SCCS[] = "@(#) mDNSResponder (Engineering Build)"; 325 #else 326 mDNSexport const char mDNSResponderVersionString_SCCS[] = "@(#) mDNSResponder (Engineering Build)"; 327 #endif 328