xref: /illumos-gate/usr/src/lib/libproc/common/Pidle.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013 by Delphix. All rights reserved.
27  */
28 
29 #include <stdlib.h>
30 #include <libelf.h>
31 #include <libgen.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <errno.h>
35 #include <sys/sysmacros.h>
36 
37 #include "libproc.h"
38 #include "Pcontrol.h"
39 
40 /*ARGSUSED*/
41 static ssize_t
42 Pread_idle(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
43     void *data)
44 {
45 	size_t resid = n;
46 
47 	while (resid > 0) {
48 		map_info_t *mp;
49 		uintptr_t mapoff;
50 		ssize_t len;
51 		off64_t off;
52 
53 		if ((mp = Paddr2mptr(P, addr)) == NULL)
54 			break;
55 
56 		mapoff = addr - mp->map_pmap.pr_vaddr;
57 		len = MIN(resid, mp->map_pmap.pr_size - mapoff);
58 		off = mp->map_offset + mapoff;
59 
60 		if ((len = pread64(P->asfd, buf, len, off)) <= 0)
61 			break;
62 
63 		resid -= len;
64 		addr += len;
65 		buf = (char *)buf + len;
66 	}
67 
68 	return (n - resid);
69 }
70 
71 /*ARGSUSED*/
72 static ssize_t
73 Pwrite_idle(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr,
74     void *data)
75 {
76 	errno = EIO;
77 	return (-1);
78 }
79 
80 /*ARGSUSED*/
81 static int
82 Ppriv_idle(struct ps_prochandle *P, prpriv_t **pprv, void *data)
83 {
84 	prpriv_t *pp;
85 
86 	pp = proc_get_priv(P->pid);
87 	if (pp == NULL) {
88 		return (-1);
89 	}
90 
91 	*pprv = pp;
92 	return (0);
93 }
94 
95 /* Default operations for the idl ops vector. */
96 static void *
97 Pidle_voidp()
98 {
99 	errno = ENODATA;
100 	return (NULL);
101 }
102 
103 static int
104 Pidle_int()
105 {
106 	errno = ENODATA;
107 	return (-1);
108 }
109 
110 static const ps_ops_t P_idle_ops = {
111 	.pop_pread	= Pread_idle,
112 	.pop_pwrite	= Pwrite_idle,
113 	.pop_cred	= (pop_cred_t)Pidle_int,
114 	.pop_priv	= Ppriv_idle,
115 	.pop_secflags	= (pop_secflags_t)Pidle_int,
116 	.pop_psinfo	= (pop_psinfo_t)Pidle_voidp,
117 	.pop_platform	= (pop_platform_t)Pidle_voidp,
118 	.pop_uname	= (pop_uname_t)Pidle_int,
119 	.pop_zonename	= (pop_zonename_t)Pidle_voidp,
120 #if defined(__i386) || defined(__amd64)
121 	.pop_ldt	= (pop_ldt_t)Pidle_int
122 #endif
123 };
124 
125 static int
126 idle_add_mapping(struct ps_prochandle *P, GElf_Phdr *php, file_info_t *fp)
127 {
128 	prmap_t pmap;
129 
130 	dprintf("mapping base %llx filesz %llu memsz %llu offset %llu\n",
131 	    (u_longlong_t)php->p_vaddr, (u_longlong_t)php->p_filesz,
132 	    (u_longlong_t)php->p_memsz, (u_longlong_t)php->p_offset);
133 
134 	pmap.pr_vaddr = (uintptr_t)php->p_vaddr;
135 	pmap.pr_size = php->p_filesz;
136 	(void) strncpy(pmap.pr_mapname, fp->file_pname,
137 	    sizeof (pmap.pr_mapname));
138 	pmap.pr_offset = php->p_offset;
139 
140 	pmap.pr_mflags = 0;
141 	if (php->p_flags & PF_R)
142 		pmap.pr_mflags |= MA_READ;
143 	if (php->p_flags & PF_W)
144 		pmap.pr_mflags |= MA_WRITE;
145 	if (php->p_flags & PF_X)
146 		pmap.pr_mflags |= MA_EXEC;
147 
148 	pmap.pr_pagesize = 0;
149 	pmap.pr_shmid = -1;
150 
151 	return (Padd_mapping(P, php->p_offset, fp, &pmap));
152 }
153 
154 struct ps_prochandle *
155 Pgrab_file(const char *fname, int *perr)
156 {
157 	struct ps_prochandle *P = NULL;
158 	char buf[PATH_MAX];
159 	GElf_Ehdr ehdr;
160 	Elf *elf = NULL;
161 	size_t phnum;
162 	file_info_t *fp = NULL;
163 	int fd;
164 	int i;
165 
166 	if ((fd = open64(fname, O_RDONLY)) < 0) {
167 		dprintf("couldn't open file");
168 		*perr = (errno == ENOENT) ? G_NOEXEC : G_STRANGE;
169 		return (NULL);
170 	}
171 
172 	if (elf_version(EV_CURRENT) == EV_NONE) {
173 		dprintf("libproc ELF version is more recent than libelf");
174 		*perr = G_ELF;
175 		goto err;
176 	}
177 
178 	if ((P = calloc(1, sizeof (struct ps_prochandle))) == NULL) {
179 		*perr = G_STRANGE;
180 		goto err;
181 	}
182 
183 	(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
184 	P->state = PS_IDLE;
185 	P->pid = (pid_t)-1;
186 	P->asfd = fd;
187 	P->ctlfd = -1;
188 	P->statfd = -1;
189 	P->agentctlfd = -1;
190 	P->agentstatfd = -1;
191 	P->info_valid = -1;
192 	Pinit_ops(&P->ops, &P_idle_ops);
193 	Pinitsym(P);
194 	Pinitfd(P);
195 
196 	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) {
197 		*perr = G_ELF;
198 		return (NULL);
199 	}
200 
201 	/*
202 	 * Construct a file_info_t that corresponds to this file.
203 	 */
204 	if ((fp = calloc(1, sizeof (file_info_t))) == NULL) {
205 		*perr = G_STRANGE;
206 		goto err;
207 	}
208 
209 	if ((fp->file_lo = calloc(1, sizeof (rd_loadobj_t))) == NULL) {
210 		*perr = G_STRANGE;
211 		goto err;
212 	}
213 
214 	if (*fname == '/') {
215 		(void) strncpy(fp->file_pname, fname, sizeof (fp->file_pname));
216 	} else {
217 		size_t sz;
218 
219 		if (getcwd(fp->file_pname, sizeof (fp->file_pname) - 1) ==
220 		    NULL) {
221 			*perr = G_STRANGE;
222 			goto err;
223 		}
224 
225 		sz = strlen(fp->file_pname);
226 		(void) snprintf(&fp->file_pname[sz],
227 		    sizeof (fp->file_pname) - sz, "/%s", fname);
228 	}
229 
230 	fp->file_fd = fd;
231 	fp->file_dbgfile = -1;
232 	fp->file_lo->rl_lmident = LM_ID_BASE;
233 	if ((fp->file_lname = strdup(fp->file_pname)) == NULL) {
234 		*perr = G_STRANGE;
235 		goto err;
236 	}
237 	fp->file_lbase = basename(fp->file_lname);
238 
239 	if ((P->execname = strdup(fp->file_pname)) == NULL) {
240 		*perr = G_STRANGE;
241 		goto err;
242 	}
243 
244 	P->num_files++;
245 	list_insert_head(&P->file_head, fp);
246 
247 	if (gelf_getehdr(elf, &ehdr) == NULL) {
248 		*perr = G_STRANGE;
249 		goto err;
250 	}
251 
252 	if (elf_getphdrnum(elf, &phnum) == -1) {
253 		*perr = G_STRANGE;
254 		goto err;
255 	}
256 
257 	dprintf("Pgrab_file: program header count = %lu\n", (ulong_t)phnum);
258 
259 	/*
260 	 * Sift through the program headers making the relevant maps.
261 	 */
262 	for (i = 0; i < phnum; i++) {
263 		GElf_Phdr phdr, *php;
264 
265 		if ((php = gelf_getphdr(elf, i, &phdr)) == NULL) {
266 			*perr = G_STRANGE;
267 			goto err;
268 		}
269 
270 		if (php->p_type != PT_LOAD)
271 			continue;
272 
273 		if (idle_add_mapping(P, php, fp) != 0) {
274 			*perr = G_STRANGE;
275 			goto err;
276 		}
277 	}
278 	Psort_mappings(P);
279 
280 	(void) elf_end(elf);
281 
282 	P->map_exec = fp->file_map;
283 
284 	P->status.pr_flags = PR_STOPPED;
285 	P->status.pr_nlwp = 0;
286 	P->status.pr_pid = (pid_t)-1;
287 	P->status.pr_ppid = (pid_t)-1;
288 	P->status.pr_pgid = (pid_t)-1;
289 	P->status.pr_sid = (pid_t)-1;
290 	P->status.pr_taskid = (taskid_t)-1;
291 	P->status.pr_projid = (projid_t)-1;
292 	P->status.pr_zoneid = (zoneid_t)-1;
293 	switch (ehdr.e_ident[EI_CLASS]) {
294 	case ELFCLASS32:
295 		P->status.pr_dmodel = PR_MODEL_ILP32;
296 		break;
297 	case ELFCLASS64:
298 		P->status.pr_dmodel = PR_MODEL_LP64;
299 		break;
300 	default:
301 		*perr = G_FORMAT;
302 		goto err;
303 	}
304 
305 	/*
306 	 * Pfindobj() checks what zone a process is associated with, so
307 	 * we call it after initializing pr_zoneid to -1.  This ensures
308 	 * we don't get associated with any zone on the system.
309 	 */
310 	if (Pfindobj(P, fp->file_lname, buf, sizeof (buf)) != NULL) {
311 		free(P->execname);
312 		P->execname = strdup(buf);
313 		if ((fp->file_rname = strdup(buf)) != NULL)
314 			fp->file_rbase = basename(fp->file_rname);
315 	}
316 
317 	/*
318 	 * The file and map lists are complete, and will never need to be
319 	 * adjusted.
320 	 */
321 	P->info_valid = 1;
322 
323 	return (P);
324 err:
325 	(void) close(fd);
326 	if (P != NULL)
327 		Pfree(P);
328 	if (elf != NULL)
329 		(void) elf_end(elf);
330 	return (NULL);
331 }
332