1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <sys/ctfs.h> 30*0Sstevel@tonic-gate #include <unistd.h> 31*0Sstevel@tonic-gate #include <fcntl.h> 32*0Sstevel@tonic-gate #include <errno.h> 33*0Sstevel@tonic-gate #include <limits.h> 34*0Sstevel@tonic-gate #include <stdio.h> 35*0Sstevel@tonic-gate #include <assert.h> 36*0Sstevel@tonic-gate #include <libuutil.h> 37*0Sstevel@tonic-gate #include <string.h> 38*0Sstevel@tonic-gate #include <procfs.h> 39*0Sstevel@tonic-gate #include <libcontract.h> 40*0Sstevel@tonic-gate #include <libcontract_priv.h> 41*0Sstevel@tonic-gate #include "libcontract_impl.h" 42*0Sstevel@tonic-gate #include "process_dump.h" 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate contract_type_t types[CTT_MAXTYPE] = { 46*0Sstevel@tonic-gate { "process", event_process } 47*0Sstevel@tonic-gate }; 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate static int 50*0Sstevel@tonic-gate close_on_exec(int fd) 51*0Sstevel@tonic-gate { 52*0Sstevel@tonic-gate int flags = fcntl(fd, F_GETFD, 0); 53*0Sstevel@tonic-gate if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)) 54*0Sstevel@tonic-gate return (0); 55*0Sstevel@tonic-gate return (-1); 56*0Sstevel@tonic-gate } 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate int 59*0Sstevel@tonic-gate contract_latest(ctid_t *id) 60*0Sstevel@tonic-gate { 61*0Sstevel@tonic-gate int cfd, r; 62*0Sstevel@tonic-gate ct_stathdl_t st; 63*0Sstevel@tonic-gate ctid_t result; 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate if ((cfd = open64(CTFS_ROOT "/process/latest", O_RDONLY)) == -1) 66*0Sstevel@tonic-gate return (errno); 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate if ((r = ct_status_read(cfd, CTD_COMMON, &st)) != 0) { 69*0Sstevel@tonic-gate (void) close(cfd); 70*0Sstevel@tonic-gate return (r); 71*0Sstevel@tonic-gate } 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate result = ct_status_get_id(st); 74*0Sstevel@tonic-gate ct_status_free(st); 75*0Sstevel@tonic-gate (void) close(cfd); 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate *id = result; 78*0Sstevel@tonic-gate return (0); 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate int 82*0Sstevel@tonic-gate contract_open(ctid_t ctid, const char *type, const char *file, int oflag) 83*0Sstevel@tonic-gate { 84*0Sstevel@tonic-gate char path[PATH_MAX]; 85*0Sstevel@tonic-gate int n, fd; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate assert((oflag & O_CREAT) == 0); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate if (type == NULL) 90*0Sstevel@tonic-gate type = "all"; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate n = snprintf(path, PATH_MAX, CTFS_ROOT "/%s/%ld/%s", type, ctid, file); 93*0Sstevel@tonic-gate if (n >= PATH_MAX) { 94*0Sstevel@tonic-gate errno = ENAMETOOLONG; 95*0Sstevel@tonic-gate return (-1); 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate fd = open64(path, oflag); 99*0Sstevel@tonic-gate if (fd != -1) { 100*0Sstevel@tonic-gate if (close_on_exec(fd) == -1) { 101*0Sstevel@tonic-gate int err = errno; 102*0Sstevel@tonic-gate (void) close(fd); 103*0Sstevel@tonic-gate errno = err; 104*0Sstevel@tonic-gate return (-1); 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate } 107*0Sstevel@tonic-gate return (fd); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate int 111*0Sstevel@tonic-gate contract_abandon_id(ctid_t ctid) 112*0Sstevel@tonic-gate { 113*0Sstevel@tonic-gate int fd, err; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate fd = contract_open(ctid, "all", "ctl", O_WRONLY); 116*0Sstevel@tonic-gate if (fd == -1) 117*0Sstevel@tonic-gate return (errno); 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate err = ct_ctl_abandon(fd); 120*0Sstevel@tonic-gate (void) close(fd); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate return (err); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate ctid_t 126*0Sstevel@tonic-gate getctid(void) 127*0Sstevel@tonic-gate { 128*0Sstevel@tonic-gate int fd; 129*0Sstevel@tonic-gate psinfo_t ps; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate if ((fd = open("/proc/self/psinfo", O_RDONLY)) == -1) 132*0Sstevel@tonic-gate return (-1); 133*0Sstevel@tonic-gate if (read(fd, &ps, sizeof (ps)) != sizeof (ps)) { 134*0Sstevel@tonic-gate (void) close(fd); 135*0Sstevel@tonic-gate return (-1); 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate (void) close(fd); 138*0Sstevel@tonic-gate return (ps.pr_contract); 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate void 142*0Sstevel@tonic-gate contract_event_dump(FILE *file, ct_evthdl_t hdl, int verbose) 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate ct_typeid_t type; 145*0Sstevel@tonic-gate struct ctlib_event_info *info = hdl; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate type = info->event.ctev_cttype; 148*0Sstevel@tonic-gate types[type].type_event(file, hdl, verbose); 149*0Sstevel@tonic-gate } 150