/ kcm / main.c
main.c
  1  /*
  2   * Copyright (c) 1997-2002 Kungliga Tekniska Högskolan
  3   * (Royal Institute of Technology, Stockholm, Sweden).
  4   * All rights reserved.
  5   *
  6   * Redistribution and use in source and binary forms, with or without
  7   * modification, are permitted provided that the following conditions
  8   * are met:
  9   *
 10   * 1. Redistributions of source code must retain the above copyright
 11   *    notice, this list of conditions and the following disclaimer.
 12   *
 13   * 2. Redistributions in binary form must reproduce the above copyright
 14   *    notice, this list of conditions and the following disclaimer in the
 15   *    documentation and/or other materials provided with the distribution.
 16   *
 17   * 3. Neither the name of the Institute nor the names of its contributors
 18   *    may be used to endorse or promote products derived from this software
 19   *    without specific prior written permission.
 20   *
 21   * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 24   * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 25   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 27   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 28   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 29   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 30   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 31   * SUCH DAMAGE.
 32   */
 33  
 34  #include "kcm_locl.h"
 35  
 36  #ifdef __APPLE__
 37  #include <sandbox.h>
 38  #include <os/log.h>
 39  #include <os/log_private.h>
 40  #endif
 41  
 42  sig_atomic_t exit_flag = 0;
 43  
 44  krb5_context kcm_context = NULL;
 45  
 46  const char *service_name = "org.h5l.kcm";
 47  
 48  static void
 49  terminated(void *ctx)
 50  {
 51      exit(0);
 52  }
 53  
 54  static void
 55  sigusr1(void *ctx)
 56  {
 57      kcm_debug_ccache(kcm_context);
 58  }
 59  
 60  static void
 61  sigusr2(void *ctx)
 62  {
 63      kcm_debug_events(kcm_context);
 64  }
 65  
 66  static void
 67  timeout_handler(void)
 68  {
 69      kcm_write_dump(kcm_context);
 70      exit(0);
 71  }
 72  
 73  int
 74  main(int argc, char **argv)
 75  {
 76      krb5_error_code ret;
 77      setprogname(argv[0]);
 78  
 79  #ifdef __APPLE__
 80      /* Tell logd we're special */
 81      os_log_set_client_type(OS_LOG_CLIENT_TYPE_LOGD_DEPENDENCY, 0);
 82  #endif
 83  
 84      ret = krb5_init_context(&kcm_context);
 85      if (ret) {
 86  	errx (1, "krb5_init_context failed: %d", ret);
 87  	return ret;
 88      }
 89  
 90      kcm_configure(argc, argv);
 91  
 92  #ifdef HAVE_SIGACTION
 93      {
 94  	struct sigaction sa;
 95  
 96  	sa.sa_flags = 0;
 97  	sa.sa_handler = SIG_IGN;
 98  	sigemptyset(&sa.sa_mask);
 99  	sigaction(SIGPIPE, &sa, NULL);
100      }
101  #else
102      signal(SIGPIPE, SIG_IGN);
103  #endif
104  
105      heim_sipc_signal_handler(SIGINT, terminated, "SIGINT");
106      heim_sipc_signal_handler(SIGTERM, terminated, "SIGTERM");
107      heim_sipc_signal_handler(SIGUSR1, sigusr1, NULL);
108      heim_sipc_signal_handler(SIGUSR2, sigusr2, NULL);
109  #ifdef SIGXCPU
110      heim_sipc_signal_handler(SIGXCPU, terminated, "CPU time limit exceeded");
111  #endif
112  
113  
114  #ifdef SUPPORT_DETACH
115      if (detach_from_console)
116  	daemon(0, 0);
117  #endif
118      kcm_session_setup_handler();
119  
120      kcm_read_dump(kcm_context);
121      
122      if (launchd_flag) {
123  	heim_sipc mach;
124  	heim_sipc_launchd_mach_init(service_name, kcm_service, NULL, &mach);
125      } else {
126  	heim_sipc un;
127  	heim_sipc_service_unix(service_name, kcm_service, NULL, &un);
128      }
129  
130  #ifdef __APPLE__
131      {
132  	char *errorstring;
133  	ret = sandbox_init("kcm", SANDBOX_NAMED, &errorstring);
134  	if (ret)
135  	    errx(1, "sandbox_init failed: %d: %s", ret, errorstring);
136      }
137  #endif /* __APPLE__ */
138  
139      heim_sipc_set_timeout_handler(timeout_handler);
140  
141      /*
142       * If we have didn't get a time or, and it was non zero,
143       * lets set the timeout
144       */
145      if (kcm_timeout < 0)
146  	kcm_timeout = 15;
147      if (kcm_timeout != 0)
148  	heim_sipc_timeout(kcm_timeout);
149  
150      heim_ipc_main();
151  
152      krb5_free_context(kcm_context);
153      return 0;
154  }