17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*25e8c5aaSvikram  * Common Development and Distribution License (the "License").
6*25e8c5aaSvikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*25e8c5aaSvikram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
277c478bd9Sstevel@tonic-gate #include <unistd.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <assert.h>
337c478bd9Sstevel@tonic-gate #include <libuutil.h>
34*25e8c5aaSvikram #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <procfs.h>
377c478bd9Sstevel@tonic-gate #include <libcontract.h>
387c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
397c478bd9Sstevel@tonic-gate #include "libcontract_impl.h"
407c478bd9Sstevel@tonic-gate #include "process_dump.h"
41*25e8c5aaSvikram #include "device_dump.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate contract_type_t types[CTT_MAXTYPE] = {
45*25e8c5aaSvikram 	{ "process", event_process },
46*25e8c5aaSvikram 	{ "device", event_device }
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static int
close_on_exec(int fd)507c478bd9Sstevel@tonic-gate close_on_exec(int fd)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	int flags = fcntl(fd, F_GETFD, 0);
537c478bd9Sstevel@tonic-gate 	if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1))
547c478bd9Sstevel@tonic-gate 		return (0);
557c478bd9Sstevel@tonic-gate 	return (-1);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int
contract_latest(ctid_t * id)597c478bd9Sstevel@tonic-gate contract_latest(ctid_t *id)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int cfd, r;
627c478bd9Sstevel@tonic-gate 	ct_stathdl_t st;
637c478bd9Sstevel@tonic-gate 	ctid_t result;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if ((cfd = open64(CTFS_ROOT "/process/latest", O_RDONLY)) == -1)
667c478bd9Sstevel@tonic-gate 		return (errno);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if ((r = ct_status_read(cfd, CTD_COMMON, &st)) != 0) {
697c478bd9Sstevel@tonic-gate 		(void) close(cfd);
707c478bd9Sstevel@tonic-gate 		return (r);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	result = ct_status_get_id(st);
747c478bd9Sstevel@tonic-gate 	ct_status_free(st);
757c478bd9Sstevel@tonic-gate 	(void) close(cfd);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	*id = result;
787c478bd9Sstevel@tonic-gate 	return (0);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int
contract_open(ctid_t ctid,const char * type,const char * file,int oflag)827c478bd9Sstevel@tonic-gate contract_open(ctid_t ctid, const char *type, const char *file, int oflag)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
857c478bd9Sstevel@tonic-gate 	int n, fd;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	assert((oflag & O_CREAT) == 0);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (type == NULL)
907c478bd9Sstevel@tonic-gate 		type = "all";
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	n = snprintf(path, PATH_MAX, CTFS_ROOT "/%s/%ld/%s", type, ctid, file);
937c478bd9Sstevel@tonic-gate 	if (n >= PATH_MAX) {
947c478bd9Sstevel@tonic-gate 		errno = ENAMETOOLONG;
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	fd = open64(path, oflag);
997c478bd9Sstevel@tonic-gate 	if (fd != -1) {
1007c478bd9Sstevel@tonic-gate 		if (close_on_exec(fd) == -1) {
1017c478bd9Sstevel@tonic-gate 			int err = errno;
1027c478bd9Sstevel@tonic-gate 			(void) close(fd);
1037c478bd9Sstevel@tonic-gate 			errno = err;
1047c478bd9Sstevel@tonic-gate 			return (-1);
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	return (fd);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate int
contract_abandon_id(ctid_t ctid)1117c478bd9Sstevel@tonic-gate contract_abandon_id(ctid_t ctid)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	int fd, err;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	fd = contract_open(ctid, "all", "ctl", O_WRONLY);
1167c478bd9Sstevel@tonic-gate 	if (fd == -1)
1177c478bd9Sstevel@tonic-gate 		return (errno);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	err = ct_ctl_abandon(fd);
1207c478bd9Sstevel@tonic-gate 	(void) close(fd);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (err);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate ctid_t
getctid(void)1267c478bd9Sstevel@tonic-gate getctid(void)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	int fd;
1297c478bd9Sstevel@tonic-gate 	psinfo_t ps;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	if (read(fd, &ps, sizeof (ps)) != sizeof (ps)) {
1347c478bd9Sstevel@tonic-gate 		(void) close(fd);
1357c478bd9Sstevel@tonic-gate 		return (-1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 	(void) close(fd);
1387c478bd9Sstevel@tonic-gate 	return (ps.pr_contract);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
contract_event_dump(FILE * file,ct_evthdl_t hdl,int verbose)1427c478bd9Sstevel@tonic-gate contract_event_dump(FILE *file, ct_evthdl_t hdl, int verbose)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	ct_typeid_t type;
1457c478bd9Sstevel@tonic-gate 	struct ctlib_event_info *info = hdl;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	type = info->event.ctev_cttype;
1487c478bd9Sstevel@tonic-gate 	types[type].type_event(file, hdl, verbose);
1497c478bd9Sstevel@tonic-gate }
150*25e8c5aaSvikram 
151*25e8c5aaSvikram void
contract_negend_dump(FILE * file,ct_evthdl_t ev)152*25e8c5aaSvikram contract_negend_dump(FILE *file, ct_evthdl_t ev)
153*25e8c5aaSvikram {
154*25e8c5aaSvikram 	ctevid_t nevid = 0;
155*25e8c5aaSvikram 	ctid_t my_ctid = ct_event_get_ctid(ev);
156*25e8c5aaSvikram 	ctid_t new_ctid = 0;
157*25e8c5aaSvikram 	char *s;
158*25e8c5aaSvikram 
159*25e8c5aaSvikram 	(void) ct_event_get_nevid(ev, &nevid);
160*25e8c5aaSvikram 	(void) ct_event_get_newct(ev, &new_ctid);
161*25e8c5aaSvikram 
162*25e8c5aaSvikram 	if (new_ctid != my_ctid) {
163*25e8c5aaSvikram 		s = dgettext(TEXT_DOMAIN, "negotiation %llu succeeded\n");
164*25e8c5aaSvikram 	} else {
165*25e8c5aaSvikram 		s = dgettext(TEXT_DOMAIN, "negotiation %llu failed\n");
166*25e8c5aaSvikram 	}
167*25e8c5aaSvikram 	/*LINTED*/
168*25e8c5aaSvikram 	(void) fprintf(file, s, (unsigned long long)nevid);
169*25e8c5aaSvikram }
170