Merge commit '42a3762d0138d6dd0c0f96964be98d0b21ab6bef'
[unleashed.git] / usr / src / cmd / smbsrv / smbd / smbd_main.c
blob4e5d8336d52132fc87d9447b9da76b27f536b577
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/ioccom.h>
29 #include <sys/corectl.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <wait.h>
38 #include <signal.h>
39 #include <atomic.h>
40 #include <libscf.h>
41 #include <limits.h>
42 #include <priv_utils.h>
43 #include <door.h>
44 #include <errno.h>
45 #include <pthread.h>
46 #include <time.h>
47 #include <libscf.h>
48 #include <zone.h>
49 #include <libgen.h>
50 #include <pwd.h>
51 #include <grp.h>
53 #include <smbsrv/smb_door.h>
54 #include <smbsrv/smb_ioctl.h>
55 #include <smbsrv/string.h>
56 #include <smbsrv/libsmb.h>
57 #include <smbsrv/libsmbns.h>
58 #include <smbsrv/libmlsvc.h>
59 #include "smbd.h"
61 #define SECSPERMIN 60
62 #define SMBD_ONLINE_WAIT_INTERVAL 10
63 #define SMBD_REFRESH_INTERVAL 10
64 #define SMB_DBDIR "/var/smb"
66 static int smbd_daemonize_init(void);
67 static void smbd_daemonize_fini(int, int);
68 static int smb_init_daemon_priv(int, uid_t, gid_t);
70 static int smbd_kernel_bind(void);
71 static void smbd_kernel_unbind(void);
72 static int smbd_already_running(void);
74 static int smbd_service_init(void);
75 static void smbd_service_fini(void);
77 static int smbd_setup_options(int argc, char *argv[]);
78 static void smbd_usage(FILE *fp);
80 static int32_t smbd_gmtoff(void);
81 static void smbd_localtime_init(void);
82 static void *smbd_localtime_monitor(void *arg);
84 static void smbd_dyndns_init(void);
85 static void smbd_load_shares(void);
86 static void *smbd_share_loader(void *);
88 static void smbd_refresh_handler(void);
90 static int smbd_kernel_start(void);
92 smbd_t smbd;
95 * Use SMF error codes only on return or exit.
97 int
98 main(int argc, char *argv[])
100 sigset_t set;
101 uid_t uid;
102 int pfd = -1;
103 int sigval;
104 struct rlimit rl;
105 int orig_limit;
107 smbd.s_pname = basename(argv[0]);
108 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
110 if (smbd_setup_options(argc, argv) != 0)
111 return (SMF_EXIT_ERR_FATAL);
113 if ((uid = getuid()) != smbd.s_uid) {
114 smbd_report("user %d: %s", uid, strerror(EPERM));
115 return (SMF_EXIT_ERR_FATAL);
118 if (smbd_already_running())
119 return (SMF_EXIT_OK);
122 * Raise the file descriptor limit to accommodate simultaneous user
123 * authentications/file access.
125 if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) &&
126 (rl.rlim_cur < rl.rlim_max)) {
127 orig_limit = rl.rlim_cur;
128 rl.rlim_cur = rl.rlim_max;
129 if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
130 smbd_report("Failed to raise file descriptor limit"
131 " from %d to %d", orig_limit, rl.rlim_cur);
135 * Block async signals in all threads.
137 (void) sigemptyset(&set);
139 (void) sigaddset(&set, SIGHUP);
140 (void) sigaddset(&set, SIGINT);
141 (void) sigaddset(&set, SIGQUIT);
142 (void) sigaddset(&set, SIGPIPE);
143 (void) sigaddset(&set, SIGTERM);
144 (void) sigaddset(&set, SIGUSR1);
145 (void) sigaddset(&set, SIGUSR2);
147 (void) sigprocmask(SIG_SETMASK, &set, NULL);
149 if (smbd.s_fg) {
150 if (smbd_service_init() != 0) {
151 smbd_report("service initialization failed");
152 exit(SMF_EXIT_ERR_FATAL);
154 } else {
156 * "pfd" is a pipe descriptor -- any fatal errors
157 * during subsequent initialization of the child
158 * process should be written to this pipe and the
159 * parent will report this error as the exit status.
161 pfd = smbd_daemonize_init();
163 if (smbd_service_init() != 0) {
164 smbd_report("daemon initialization failed");
165 exit(SMF_EXIT_ERR_FATAL);
168 smbd_daemonize_fini(pfd, SMF_EXIT_OK);
171 while (!smbd.s_shutting_down) {
172 int ret = sigwait(&set, &sigval);
173 if (ret != 0) {
174 syslog(LOG_DEBUG, "sigwait failed: %s", strerror(ret));
175 continue;
178 switch (sigval) {
179 case SIGPIPE:
180 break;
182 case SIGHUP:
183 syslog(LOG_DEBUG, "refresh requested");
184 smbd_refresh_handler();
185 break;
187 case SIGUSR1:
188 syslog(LOG_DEBUG, "SIGUSR1 ignored");
189 break;
191 default:
193 * Typically SIGINT or SIGTERM.
195 smbd.s_shutting_down = B_TRUE;
196 break;
201 * Allow termination signals while shutting down.
203 (void) sigemptyset(&set);
205 if (smbd.s_fg) {
206 (void) sigaddset(&set, SIGHUP);
207 (void) sigaddset(&set, SIGINT);
209 (void) sigaddset(&set, SIGTERM);
211 (void) sigprocmask(SIG_UNBLOCK, &set, NULL);
213 smbd_service_fini();
214 return ((smbd.s_fatal_error) ? SMF_EXIT_ERR_FATAL : SMF_EXIT_OK);
218 * This function will fork off a child process,
219 * from which only the child will return.
221 * Use SMF error codes only on exit.
223 static int
224 smbd_daemonize_init(void)
226 int status, pfds[2];
227 sigset_t set, oset;
228 pid_t pid;
229 int rc;
232 * Reset privileges to the minimum set required. We continue
233 * to run as root to create and access files in /var.
235 rc = smb_init_daemon_priv(PU_RESETGROUPS, smbd.s_uid, smbd.s_gid);
237 if (rc != 0) {
238 smbd_report("insufficient privileges");
239 exit(SMF_EXIT_ERR_FATAL);
243 * Block all signals prior to the fork and leave them blocked in the
244 * parent so we don't get in a situation where the parent gets SIGINT
245 * and returns non-zero exit status and the child is actually running.
246 * In the child, restore the signal mask once we've done our setsid().
248 (void) sigfillset(&set);
249 (void) sigdelset(&set, SIGABRT);
250 (void) sigprocmask(SIG_BLOCK, &set, &oset);
252 if (pipe(pfds) == -1) {
253 smbd_report("unable to create pipe");
254 exit(SMF_EXIT_ERR_FATAL);
257 closelog();
259 if ((pid = fork()) == -1) {
260 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
261 smbd_report("unable to fork");
262 closelog();
263 exit(SMF_EXIT_ERR_FATAL);
267 * If we're the parent process, wait for either the child to send us
268 * the appropriate exit status over the pipe or for the read to fail
269 * (presumably with 0 for EOF if our child terminated abnormally).
270 * If the read fails, exit with either the child's exit status if it
271 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
273 if (pid != 0) {
274 (void) close(pfds[1]);
276 if (read(pfds[0], &status, sizeof (status)) == sizeof (status))
277 _exit(status);
279 if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
280 _exit(WEXITSTATUS(status));
282 _exit(SMF_EXIT_ERR_FATAL);
285 openlog(smbd.s_pname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
286 (void) setsid();
287 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
288 (void) chdir("/");
289 (void) umask(022);
290 (void) close(pfds[0]);
292 return (pfds[1]);
296 * This function is based on __init_daemon_priv() and replaces
297 * __init_daemon_priv() since we want smbd to have all privileges so that it
298 * can execute map/unmap commands with all privileges during share
299 * connection/disconnection. Unused privileges are disabled until command
300 * execution. The permitted and the limit set contains all privileges. The
301 * inheritable set contains no privileges.
304 static const char root_cp[] = "/core.%f.%t";
305 static const char daemon_cp[] = "/var/tmp/core.%f.%t";
307 static int
308 smb_init_daemon_priv(int flags, uid_t uid, gid_t gid)
310 priv_set_t *perm = NULL;
311 int ret = -1;
312 char buf[1024];
315 * This is not a significant failure: it allows us to start programs
316 * with sufficient privileges and with the proper uid. We don't
317 * care enough about the extra groups in that case.
319 if (flags & PU_RESETGROUPS)
320 (void) setgroups(0, NULL);
322 if (gid != (gid_t)-1 && setgid(gid) != 0)
323 goto end;
325 perm = priv_allocset();
326 if (perm == NULL)
327 goto end;
329 /* E = P */
330 (void) getppriv(PRIV_PERMITTED, perm);
331 (void) setppriv(PRIV_SET, PRIV_EFFECTIVE, perm);
333 /* Now reset suid and euid */
334 if (uid != (uid_t)-1 && setreuid(uid, uid) != 0)
335 goto end;
337 /* I = 0 */
338 priv_emptyset(perm);
339 ret = setppriv(PRIV_SET, PRIV_INHERITABLE, perm);
340 end:
341 priv_freeset(perm);
343 if (core_get_process_path(buf, sizeof (buf), getpid()) == 0 &&
344 strcmp(buf, "core") == 0) {
346 if ((uid == (uid_t)-1 ? geteuid() : uid) == 0) {
347 (void) core_set_process_path(root_cp, sizeof (root_cp),
348 getpid());
349 } else {
350 (void) core_set_process_path(daemon_cp,
351 sizeof (daemon_cp), getpid());
354 (void) setpflags(__PROC_PROTECT, 0);
356 return (ret);
360 * Most privileges, except the ones that are required for smbd, are turn off
361 * in the effective set. They will be turn on when needed for command
362 * execution during share connection/disconnection.
364 static void
365 smbd_daemonize_fini(int fd, int exit_status)
367 priv_set_t *pset;
370 * Now that we're running, if a pipe fd was specified, write an exit
371 * status to it to indicate that our parent process can safely detach.
372 * Then proceed to loading the remaining non-built-in modules.
374 if (fd >= 0)
375 (void) write(fd, &exit_status, sizeof (exit_status));
377 (void) close(fd);
379 pset = priv_allocset();
380 if (pset == NULL)
381 return;
383 priv_basicset(pset);
385 /* list of privileges for smbd */
386 (void) priv_addset(pset, PRIV_NET_PRIVADDR);
387 (void) priv_addset(pset, PRIV_PROC_AUDIT);
388 (void) priv_addset(pset, PRIV_SYS_DEVICES);
389 (void) priv_addset(pset, PRIV_SYS_SMB);
390 (void) priv_addset(pset, PRIV_SYS_MOUNT);
392 priv_inverse(pset);
394 /* turn off unneeded privileges */
395 (void) setppriv(PRIV_OFF, PRIV_EFFECTIVE, pset);
397 priv_freeset(pset);
399 /* reenable core dumps */
400 __fini_daemon_priv(NULL);
404 * smbd_service_init
406 static int
407 smbd_service_init(void)
409 static struct dir {
410 char *name;
411 int perm;
412 } dir[] = {
413 { SMB_DBDIR, 0700 },
414 { SMB_CVOL, 0755 },
415 { SMB_SYSROOT, 0755 },
416 { SMB_SYSTEM32, 0755 },
417 { SMB_VSS, 0755 },
418 { SMB_PIPE_DIR, 0755 },
419 { "/var/smb/lipc", 0755 },
421 int rc, i;
423 smbd.s_pid = getpid();
426 * Stop for a debugger attach here, which is after the
427 * fork() etc. in smb_daemonize_init()
429 if (smbd.s_dbg_stop) {
430 smbd_report("pid %d stop for debugger attach", smbd.s_pid);
431 (void) kill(smbd.s_pid, SIGSTOP);
433 smbd_report("smbd starting, pid %d", smbd.s_pid);
435 for (i = 0; i < sizeof (dir)/sizeof (dir[0]); ++i) {
436 if ((mkdir(dir[i].name, dir[i].perm) < 0) &&
437 (errno != EEXIST)) {
438 smbd_report("mkdir %s: %s", dir[i].name,
439 strerror(errno));
440 return (-1);
445 * This environment variable tells mech_krb5 to give us
446 * MS-compatible behavior.
448 (void) putenv("MS_INTEROP=1");
450 if ((rc = smb_ccache_init(SMB_VARRUN_DIR, SMB_CCACHE_FILE)) != 0) {
451 if (rc == -1)
452 smbd_report("mkdir %s: %s", SMB_VARRUN_DIR,
453 strerror(errno));
454 else
455 smbd_report("unable to set KRB5CCNAME");
456 return (-1);
459 /* Upgrade SMF settings, if necessary. */
460 smb_config_upgrade();
462 smb_codepage_init();
464 rc = smbd_cups_init();
465 if (smb_config_getbool(SMB_CI_PRINT_ENABLE))
466 smbd_report("print service %savailable", (rc == 0) ? "" : "un");
468 if (smbd_nicmon_start(SMBD_DEFAULT_INSTANCE_FMRI) != 0)
469 smbd_report("NIC monitor failed to start");
471 smbd_dyndns_init();
472 smb_ipc_init();
474 if (smb_config_getbool(SMB_CI_NETBIOS_ENABLE) == 0)
475 smbd_report("NetBIOS services disabled");
476 else if (smb_netbios_start() != 0)
477 smbd_report("NetBIOS services failed to start");
478 else
479 smbd_report("NetBIOS services started");
481 smbd.s_secmode = smb_config_get_secmode();
482 if ((rc = smb_domain_init(smbd.s_secmode)) != 0) {
483 if (rc == SMB_DOMAIN_NOMACHINE_SID) {
484 smbd_report(
485 "no machine SID: check idmap configuration");
486 return (-1);
490 if (smbd_dc_monitor_init() != 0)
491 smbd_report("DC monitor initialization failed %s",
492 strerror(errno));
494 if (smbd_pipesvc_start() != 0) {
495 smbd_report("pipesvc initialization failed");
496 return (-1);
499 if (smbd_authsvc_start() != 0) {
500 smbd_report("authsvc initialization failed");
501 return (-1);
504 smbd.s_door_srv = smbd_door_start();
505 if (smbd.s_door_srv < 0) {
506 smbd_report("door initialization failed %s", strerror(errno));
507 return (-1);
510 dyndns_update_zones();
511 smbd_localtime_init();
512 (void) smb_lgrp_start();
513 smb_pwd_init(B_TRUE);
515 if (smb_shr_start() != 0) {
516 smbd_report("share initialization failed: %s", strerror(errno));
517 return (-1);
520 smbd.s_door_lmshr = smbd_share_start();
521 if (smbd.s_door_lmshr < 0)
522 smbd_report("share initialization failed");
524 /* Open the driver, load the kernel config. */
525 if (smbd_kernel_bind() != 0) {
526 return (-1);
529 smbd_load_shares();
530 smbd_load_printers();
531 smbd_spool_start();
533 smbd.s_initialized = B_TRUE;
534 smbd_report("service initialized");
536 return (0);
540 * Shutdown smbd and smbsrv kernel services.
542 * Called only by the main thread.
544 static void
545 smbd_service_fini(void)
548 smbd.s_shutting_down = B_TRUE;
549 smbd_report("service shutting down");
551 smb_kmod_stop();
552 smb_logon_abort();
553 smb_lgrp_stop();
554 smbd_pipesvc_stop();
555 smbd_door_stop();
556 smbd_authsvc_stop();
557 smbd_spool_stop();
558 smbd_kernel_unbind();
559 smbd_share_stop();
560 smb_shr_stop();
561 dyndns_stop();
562 smbd_nicmon_stop();
563 smb_ccache_remove(SMB_CCACHE_PATH);
564 smb_pwd_fini();
565 smb_domain_fini();
566 mlsvc_fini();
567 smb_netbios_stop();
568 smbd_cups_fini();
570 smbd.s_initialized = B_FALSE;
571 smbd_report("service terminated");
572 closelog();
576 * Called when SMF sends us a SIGHUP. Update the smbd configuration
577 * from SMF and check for changes that require service reconfiguration.
579 static void
580 smbd_refresh_handler()
582 int new_debug;
584 if (smbd.s_shutting_down)
585 return;
587 smbd.s_refreshes++;
589 new_debug = smb_config_get_debug();
590 if (smbd.s_debug || new_debug)
591 smbd_report("debug=%d", new_debug);
592 smbd.s_debug = new_debug;
594 smbd_spool_stop();
595 smbd_dc_monitor_refresh();
596 smb_ccache_remove(SMB_CCACHE_PATH);
599 * Clear the DNS zones for the existing interfaces
600 * before updating the NIC interface list.
602 dyndns_clear_zones();
604 if (smbd_nicmon_refresh() != 0)
605 smbd_report("NIC monitor refresh failed");
607 smb_netbios_name_reconfig();
608 smb_browser_reconfig();
609 dyndns_update_zones();
611 /* This reloads the in-kernel config. */
612 (void) smbd_kernel_bind();
614 smbd_load_shares();
615 smbd_load_printers();
616 smbd_spool_start();
619 void
620 smbd_set_secmode(int secmode)
622 switch (secmode) {
623 case SMB_SECMODE_WORKGRP:
624 case SMB_SECMODE_DOMAIN:
625 (void) smb_config_set_secmode(secmode);
626 smbd.s_secmode = secmode;
627 break;
629 default:
630 syslog(LOG_ERR, "invalid security mode: %d", secmode);
631 syslog(LOG_ERR, "entering maintenance mode");
632 (void) smb_smf_maintenance_mode();
637 * The service is online if initialization is complete and shutdown
638 * has not begun.
640 boolean_t
641 smbd_online(void)
643 return (smbd.s_initialized && !smbd.s_shutting_down);
647 * Wait until the service is online. Provided for threads that
648 * should wait until the service has been fully initialized before
649 * they start performing operations.
651 void
652 smbd_online_wait(const char *text)
654 while (!smbd_online())
655 (void) sleep(SMBD_ONLINE_WAIT_INTERVAL);
657 if (text != NULL) {
658 syslog(LOG_DEBUG, "%s: online", text);
659 (void) fprintf(stderr, "%s: online\n", text);
664 * If the door has already been opened by another process (non-zero pid
665 * in target), we assume that another smbd is already running. If there
666 * is a race here, it will be caught later when smbsrv is opened because
667 * only one process is allowed to open the device at a time.
669 static int
670 smbd_already_running(void)
672 door_info_t info;
673 char *door_name;
674 int door;
676 door_name = getenv("SMBD_DOOR_NAME");
677 if (door_name == NULL)
678 door_name = SMBD_DOOR_NAME;
680 if ((door = open(door_name, O_RDONLY)) < 0)
681 return (0);
683 if (door_info(door, &info) < 0)
684 return (0);
686 if (info.di_target > 0) {
687 smbd_report("already running: pid %ld\n", info.di_target);
688 (void) close(door);
689 return (1);
692 (void) close(door);
693 return (0);
697 * smbd_kernel_bind
699 * If smbsrv is already bound, reload the configuration and update smbsrv.
700 * Otherwise, open the smbsrv device and start the kernel service.
702 static int
703 smbd_kernel_bind(void)
705 smb_kmod_cfg_t cfg;
706 int rc;
708 if (smbd.s_kbound) {
709 smb_load_kconfig(&cfg);
710 smbd_get_authconf(&cfg);
711 rc = smb_kmod_setcfg(&cfg);
712 if (rc < 0)
713 smbd_report("kernel configuration update failed: %s",
714 strerror(rc));
715 return (rc);
718 if (smb_kmod_isbound())
719 smbd_kernel_unbind();
721 if ((rc = smb_kmod_bind()) == 0) {
722 rc = smbd_kernel_start();
723 if (rc != 0)
724 smb_kmod_unbind();
725 else
726 smbd.s_kbound = B_TRUE;
727 } else {
728 smbd_report("kernel bind error: %s", strerror(rc));
731 return (rc);
734 static int
735 smbd_kernel_start(void)
737 smb_kmod_cfg_t cfg;
738 int rc;
740 smb_load_kconfig(&cfg);
741 smbd_get_authconf(&cfg);
742 rc = smb_kmod_setcfg(&cfg);
743 if (rc != 0) {
744 smbd_report("kernel config ioctl error: %s", strerror(rc));
745 return (rc);
748 rc = smb_kmod_setgmtoff(smbd_gmtoff());
749 if (rc != 0) {
750 smbd_report("kernel gmtoff ioctl error: %s", strerror(rc));
751 return (rc);
754 rc = smb_kmod_start(smbd.s_door_opipe, smbd.s_door_lmshr,
755 smbd.s_door_srv);
757 if (rc != 0) {
758 smbd_report("kernel start ioctl error: %s", strerror(rc));
759 return (rc);
762 return (0);
766 * smbd_kernel_unbind
768 static void
769 smbd_kernel_unbind(void)
771 smb_kmod_unbind();
772 smbd.s_kbound = B_FALSE;
776 * Create the Dynamic DNS publisher thread.
778 static void
779 smbd_dyndns_init(void)
781 pthread_t tid;
782 pthread_attr_t attr;
783 int rc;
785 dyndns_start();
787 (void) pthread_attr_init(&attr);
788 (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
789 rc = pthread_create(&tid, &attr, dyndns_publisher, NULL);
790 (void) pthread_attr_destroy(&attr);
792 if (rc != 0)
793 smbd_report("unable to start dyndns publisher: %s",
794 strerror(errno));
798 * Launches a thread to populate the share cache by share information
799 * stored in sharemgr
801 static void
802 smbd_load_shares(void)
804 pthread_t tid;
805 pthread_attr_t attr;
806 int rc;
808 (void) pthread_attr_init(&attr);
809 (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
810 rc = pthread_create(&tid, &attr, smbd_share_loader, NULL);
811 (void) pthread_attr_destroy(&attr);
813 if (rc != 0)
814 smbd_report("unable to load disk shares: %s", strerror(errno));
817 static void *
818 smbd_share_loader(void *args)
820 (void) smb_shr_load(args);
821 return (NULL);
825 * Initialization of the localtime thread.
826 * Returns 0 on success, an error number if thread creation fails.
829 static void
830 smbd_localtime_init(void)
832 pthread_attr_t attr;
833 int rc;
835 (void) pthread_attr_init(&attr);
836 (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
837 rc = pthread_create(&smbd.s_localtime_tid, &attr,
838 smbd_localtime_monitor, NULL);
839 (void) pthread_attr_destroy(&attr);
841 if (rc != 0)
842 smbd_report("unable to monitor localtime: %s", strerror(errno));
846 * Send local gmtoff to the kernel module one time at startup and each
847 * time it changes (up to twice a year).
848 * Local gmtoff is checked once every 15 minutes since some timezones
849 * are aligned on half and quarter hour boundaries.
851 /*ARGSUSED*/
852 static void *
853 smbd_localtime_monitor(void *arg)
855 struct tm local_tm;
856 time_t secs;
857 int32_t gmtoff, last_gmtoff = -1;
858 int timeout;
859 int error;
861 smbd_online_wait("smbd_localtime_monitor");
863 for (;;) {
864 gmtoff = smbd_gmtoff();
866 if ((last_gmtoff != gmtoff) && smbd.s_kbound) {
867 error = smb_kmod_setgmtoff(gmtoff);
868 if (error != 0)
869 smbd_report("localtime set failed: %s",
870 strerror(error));
874 * Align the next iteration on a fifteen minute boundary.
876 secs = time(0);
877 (void) localtime_r(&secs, &local_tm);
878 timeout = ((15 - (local_tm.tm_min % 15)) * SECSPERMIN);
879 (void) sleep(timeout);
881 last_gmtoff = gmtoff;
884 /*NOTREACHED*/
885 return (NULL);
889 * smbd_gmtoff
891 * Determine offset from GMT. If daylight saving time use altzone,
892 * otherwise use timezone.
894 static int32_t
895 smbd_gmtoff(void)
897 time_t clock_val;
898 struct tm *atm;
899 int32_t gmtoff;
901 (void) time(&clock_val);
902 atm = localtime(&clock_val);
904 gmtoff = (atm->tm_isdst) ? altzone : timezone;
906 return (gmtoff);
910 * Set up configuration options and parse the command line.
911 * This function will determine if we will run as a daemon
912 * or in the foreground.
914 * Failure to find a uid or gid results in using the default (0).
916 static int
917 smbd_setup_options(int argc, char *argv[])
919 struct passwd *pwd;
920 struct group *grp;
921 int c;
923 if ((pwd = getpwnam("root")) != NULL)
924 smbd.s_uid = pwd->pw_uid;
926 if ((grp = getgrnam("sys")) != NULL)
927 smbd.s_gid = grp->gr_gid;
929 smbd.s_debug = smb_config_get_debug();
930 smbd.s_fg = smb_config_get_fg_flag();
932 while ((c = getopt(argc, argv, ":dfs")) != -1) {
933 switch (c) {
934 case 'd':
935 smbd.s_debug++;
936 break;
937 case 'f':
938 smbd.s_fg = 1;
939 break;
940 case 's':
941 smbd.s_dbg_stop = 1;
942 break;
943 case ':':
944 case '?':
945 default:
946 smbd_usage(stderr);
947 return (-1);
951 return (0);
954 static void
955 smbd_usage(FILE *fp)
957 static char *help[] = {
958 "-d enable debug messages"
959 "-f run program in foreground"
962 int i;
964 (void) fprintf(fp, "Usage: %s [-f]\n", smbd.s_pname);
966 for (i = 0; i < sizeof (help)/sizeof (help[0]); ++i)
967 (void) fprintf(fp, " %s\n", help[i]);
970 void
971 smbd_report(const char *fmt, ...)
973 char buf[128];
974 va_list ap;
976 if (fmt == NULL)
977 return;
979 va_start(ap, fmt);
980 (void) vsnprintf(buf, 128, fmt, ap);
981 va_end(ap);
983 (void) fprintf(stderr, "smbd: %s\n", buf);
987 * Enable libumem debugging by default on DEBUG builds.
989 #ifdef DEBUG
990 const char *
991 _umem_debug_init(void)
993 return ("default,verbose"); /* $UMEM_DEBUG setting */
996 const char *
997 _umem_logging_init(void)
999 return ("fail,contents"); /* $UMEM_LOGGING setting */
1001 #endif