xref: /netbsd-src/external/bsd/tmux/dist/osdep-sunos.c (revision b49cc1491953ef2348eff9c84520ffd0678a5c8d)
1 /* $Id: osdep-sunos.c,v 1.1.1.1 2011/03/10 09:15:38 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Todd Carson <toc@daybefore.net>
5  *
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.
9  *
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 MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 
22 #include <fcntl.h>
23 #include <procfs.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 char *
31 osdep_get_name(int fd, char *tty)
32 {
33 	struct psinfo	 p;
34 	struct stat	 st;
35 	char		*path;
36 	ssize_t		 bytes;
37 	int		 f;
38 	pid_t		 pgrp;
39 
40 	if ((f = open(tty, O_RDONLY)) < 0)
41 		return (NULL);
42 
43 	if ((fstat(f, &st) != 0) ||
44 	    (ioctl(f, TIOCGPGRP, &pgrp) != 0)) {
45 		close(f);
46 		return (NULL);
47 	}
48 	close(f);
49 
50 	xasprintf(&path, "/proc/%hu/psinfo", pgrp);
51 	f = open(path, O_RDONLY);
52 	xfree(path);
53 	if (f < 0)
54 		return (NULL);
55 
56 	bytes = read(f, &p, sizeof(p));
57 	close(f);
58 	if (bytes != sizeof(p))
59 		return (NULL);
60 
61 	if (p.pr_ttydev != st.st_rdev)
62 		return (NULL);
63 
64 	return (xstrdup(p.pr_fname));
65 }
66