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 #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <assert.h>
357c478bd9Sstevel@tonic-gate #include <libuutil.h>
36*25e8c5aaSvikram #include <libintl.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <procfs.h>
397c478bd9Sstevel@tonic-gate #include <libcontract.h>
407c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
417c478bd9Sstevel@tonic-gate #include "libcontract_impl.h"
427c478bd9Sstevel@tonic-gate #include "process_dump.h"
43*25e8c5aaSvikram #include "device_dump.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate contract_type_t types[CTT_MAXTYPE] = {
47*25e8c5aaSvikram { "process", event_process },
48*25e8c5aaSvikram { "device", event_device }
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static int
close_on_exec(int fd)527c478bd9Sstevel@tonic-gate close_on_exec(int fd)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate int flags = fcntl(fd, F_GETFD, 0);
557c478bd9Sstevel@tonic-gate if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1))
567c478bd9Sstevel@tonic-gate return (0);
577c478bd9Sstevel@tonic-gate return (-1);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate int
contract_latest(ctid_t * id)617c478bd9Sstevel@tonic-gate contract_latest(ctid_t *id)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate int cfd, r;
647c478bd9Sstevel@tonic-gate ct_stathdl_t st;
657c478bd9Sstevel@tonic-gate ctid_t result;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate if ((cfd = open64(CTFS_ROOT "/process/latest", O_RDONLY)) == -1)
687c478bd9Sstevel@tonic-gate return (errno);
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if ((r = ct_status_read(cfd, CTD_COMMON, &st)) != 0) {
717c478bd9Sstevel@tonic-gate (void) close(cfd);
727c478bd9Sstevel@tonic-gate return (r);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate result = ct_status_get_id(st);
767c478bd9Sstevel@tonic-gate ct_status_free(st);
777c478bd9Sstevel@tonic-gate (void) close(cfd);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate *id = result;
807c478bd9Sstevel@tonic-gate return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate int
contract_open(ctid_t ctid,const char * type,const char * file,int oflag)847c478bd9Sstevel@tonic-gate contract_open(ctid_t ctid, const char *type, const char *file, int oflag)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate char path[PATH_MAX];
877c478bd9Sstevel@tonic-gate int n, fd;
887c478bd9Sstevel@tonic-gate
897c478bd9Sstevel@tonic-gate assert((oflag & O_CREAT) == 0);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate if (type == NULL)
927c478bd9Sstevel@tonic-gate type = "all";
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate n = snprintf(path, PATH_MAX, CTFS_ROOT "/%s/%ld/%s", type, ctid, file);
957c478bd9Sstevel@tonic-gate if (n >= PATH_MAX) {
967c478bd9Sstevel@tonic-gate errno = ENAMETOOLONG;
977c478bd9Sstevel@tonic-gate return (-1);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate fd = open64(path, oflag);
1017c478bd9Sstevel@tonic-gate if (fd != -1) {
1027c478bd9Sstevel@tonic-gate if (close_on_exec(fd) == -1) {
1037c478bd9Sstevel@tonic-gate int err = errno;
1047c478bd9Sstevel@tonic-gate (void) close(fd);
1057c478bd9Sstevel@tonic-gate errno = err;
1067c478bd9Sstevel@tonic-gate return (-1);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate return (fd);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate int
contract_abandon_id(ctid_t ctid)1137c478bd9Sstevel@tonic-gate contract_abandon_id(ctid_t ctid)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate int fd, err;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate fd = contract_open(ctid, "all", "ctl", O_WRONLY);
1187c478bd9Sstevel@tonic-gate if (fd == -1)
1197c478bd9Sstevel@tonic-gate return (errno);
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate err = ct_ctl_abandon(fd);
1227c478bd9Sstevel@tonic-gate (void) close(fd);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate return (err);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate ctid_t
getctid(void)1287c478bd9Sstevel@tonic-gate getctid(void)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate int fd;
1317c478bd9Sstevel@tonic-gate psinfo_t ps;
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1)
1347c478bd9Sstevel@tonic-gate return (-1);
1357c478bd9Sstevel@tonic-gate if (read(fd, &ps, sizeof (ps)) != sizeof (ps)) {
1367c478bd9Sstevel@tonic-gate (void) close(fd);
1377c478bd9Sstevel@tonic-gate return (-1);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate (void) close(fd);
1407c478bd9Sstevel@tonic-gate return (ps.pr_contract);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate void
contract_event_dump(FILE * file,ct_evthdl_t hdl,int verbose)1447c478bd9Sstevel@tonic-gate contract_event_dump(FILE *file, ct_evthdl_t hdl, int verbose)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate ct_typeid_t type;
1477c478bd9Sstevel@tonic-gate struct ctlib_event_info *info = hdl;
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate type = info->event.ctev_cttype;
1507c478bd9Sstevel@tonic-gate types[type].type_event(file, hdl, verbose);
1517c478bd9Sstevel@tonic-gate }
152*25e8c5aaSvikram
153*25e8c5aaSvikram void
contract_negend_dump(FILE * file,ct_evthdl_t ev)154*25e8c5aaSvikram contract_negend_dump(FILE *file, ct_evthdl_t ev)
155*25e8c5aaSvikram {
156*25e8c5aaSvikram ctevid_t nevid = 0;
157*25e8c5aaSvikram ctid_t my_ctid = ct_event_get_ctid(ev);
158*25e8c5aaSvikram ctid_t new_ctid = 0;
159*25e8c5aaSvikram char *s;
160*25e8c5aaSvikram
161*25e8c5aaSvikram (void) ct_event_get_nevid(ev, &nevid);
162*25e8c5aaSvikram (void) ct_event_get_newct(ev, &new_ctid);
163*25e8c5aaSvikram
164*25e8c5aaSvikram if (new_ctid != my_ctid) {
165*25e8c5aaSvikram s = dgettext(TEXT_DOMAIN, "negotiation %llu succeeded\n");
166*25e8c5aaSvikram } else {
167*25e8c5aaSvikram s = dgettext(TEXT_DOMAIN, "negotiation %llu failed\n");
168*25e8c5aaSvikram }
169*25e8c5aaSvikram /*LINTED*/
170*25e8c5aaSvikram (void) fprintf(file, s, (unsigned long long)nevid);
171*25e8c5aaSvikram }
172