- markus@cvs.openbsd.org 2006/10/10 10:12:45
[openssh-git.git] / openbsd-compat / port-solaris.c
blobf57433e78f3bba66dac9237e158d1b9b05e53cab
1 /* $Id: port-solaris.c,v 1.2 2006/09/01 05:38:41 djm Exp $ */
3 /*
4 * Copyright (c) 2006 Chad Mynhier.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "config.h"
20 #include "includes.h"
22 #ifdef USE_SOLARIS_PROCESS_CONTRACTS
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/param.h>
28 #include <errno.h>
29 #ifdef HAVE_FCNTL_H
30 # include <fcntl.h>
31 #endif
32 #include <stdarg.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include <libcontract.h>
37 #include <sys/contract/process.h>
38 #include <sys/ctfs.h>
40 #include "log.h"
42 #define CT_TEMPLATE CTFS_ROOT "/process/template"
43 #define CT_LATEST CTFS_ROOT "/process/latest"
45 static int tmpl_fd = -1;
47 /* Lookup the latest process contract */
48 static ctid_t
49 get_active_process_contract_id(void)
51 int stat_fd;
52 ctid_t ctid = -1;
53 ct_stathdl_t stathdl;
55 if ((stat_fd = open64(CT_LATEST, O_RDONLY)) == -1) {
56 error("%s: Error opening 'latest' process "
57 "contract: %s", __func__, strerror(errno));
58 return -1;
60 if (ct_status_read(stat_fd, CTD_COMMON, &stathdl) != 0) {
61 error("%s: Error reading process contract "
62 "status: %s", __func__, strerror(errno));
63 goto out;
65 if ((ctid = ct_status_get_id(stathdl)) < 0) {
66 error("%s: Error getting process contract id: %s",
67 __func__, strerror(errno));
68 goto out;
71 ct_status_free(stathdl);
72 out:
73 close(stat_fd);
74 return ctid;
77 void
78 solaris_contract_pre_fork(void)
80 if ((tmpl_fd = open64(CT_TEMPLATE, O_RDWR)) == -1) {
81 error("%s: open %s: %s", __func__,
82 CT_TEMPLATE, strerror(errno));
83 return;
86 debug2("%s: setting up process contract template on fd %d",
87 __func__, tmpl_fd);
89 /* We have to set certain attributes before activating the template */
90 if (ct_pr_tmpl_set_fatal(tmpl_fd,
91 CT_PR_EV_HWERR|CT_PR_EV_SIGNAL|CT_PR_EV_CORE) != 0) {
92 error("%s: Error setting process contract template "
93 "fatal events: %s", __func__, strerror(errno));
94 goto fail;
96 if (ct_tmpl_set_critical(tmpl_fd, CT_PR_EV_HWERR) != 0) {
97 error("%s: Error setting process contract template "
98 "critical events: %s", __func__, strerror(errno));
99 goto fail;
102 /* Now make this the active template for this process. */
103 if (ct_tmpl_activate(tmpl_fd) != 0) {
104 error("%s: Error activating process contract "
105 "template: %s", __func__, strerror(errno));
106 goto fail;
108 return;
110 fail:
111 if (tmpl_fd != -1) {
112 close(tmpl_fd);
113 tmpl_fd = -1;
117 void
118 solaris_contract_post_fork_child()
120 debug2("%s: clearing process contract template on fd %d",
121 __func__, tmpl_fd);
123 /* Clear the active template. */
124 if (ct_tmpl_clear(tmpl_fd) != 0)
125 error("%s: Error clearing active process contract "
126 "template: %s", __func__, strerror(errno));
128 close(tmpl_fd);
129 tmpl_fd = -1;
132 void
133 solaris_contract_post_fork_parent(pid_t pid)
135 ctid_t ctid;
136 char ctl_path[256];
137 int r, ctl_fd = -1, stat_fd = -1;
139 debug2("%s: clearing template (fd %d)", __func__, tmpl_fd);
141 if (tmpl_fd == -1)
142 return;
144 /* First clear the active template. */
145 if ((r = ct_tmpl_clear(tmpl_fd)) != 0)
146 error("%s: Error clearing active process contract "
147 "template: %s", __func__, strerror(errno));
149 close(tmpl_fd);
150 tmpl_fd = -1;
153 * If either the fork didn't succeed (pid < 0), or clearing
154 * th active contract failed (r != 0), then we have nothing
155 * more do.
157 if (r != 0 || pid <= 0)
158 return;
160 /* Now lookup and abandon the contract we've created. */
161 ctid = get_active_process_contract_id();
163 debug2("%s: abandoning contract id %ld", __func__, ctid);
165 snprintf(ctl_path, sizeof(ctl_path),
166 CTFS_ROOT "/process/%ld/ctl", ctid);
167 if ((ctl_fd = open64(ctl_path, O_WRONLY)) < 0) {
168 error("%s: Error opening process contract "
169 "ctl file: %s", __func__, strerror(errno));
170 goto fail;
172 if (ct_ctl_abandon(ctl_fd) < 0) {
173 error("%s: Error abandoning process contract: %s",
174 __func__, strerror(errno));
175 goto fail;
177 close(ctl_fd);
178 return;
180 fail:
181 if (tmpl_fd != -1) {
182 close(tmpl_fd);
183 tmpl_fd = -1;
185 if (stat_fd != -1)
186 close(stat_fd);
187 if (ctl_fd != -1)
188 close(ctl_fd);
190 #endif