Merge branch 'obsd-master'
[tmux.git] / osdep-darwin.c
bloba2b125ad1738c147796a651fa960bd9e740158c3
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/sysctl.h>
22 #include <Availability.h>
23 #include <libproc.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "compat.h"
30 char *osdep_get_name(int, char *);
31 char *osdep_get_cwd(int);
32 struct event_base *osdep_event_init(void);
34 #ifndef __unused
35 #define __unused __attribute__ ((__unused__))
36 #endif
38 char *
39 osdep_get_name(int fd, __unused char *tty)
41 #ifdef __MAC_10_7
42 struct proc_bsdshortinfo bsdinfo;
43 pid_t pgrp;
44 int ret;
46 if ((pgrp = tcgetpgrp(fd)) == -1)
47 return (NULL);
49 ret = proc_pidinfo(pgrp, PROC_PIDT_SHORTBSDINFO, 0,
50 &bsdinfo, sizeof bsdinfo);
51 if (ret == sizeof bsdinfo && *bsdinfo.pbsi_comm != '\0')
52 return (strdup(bsdinfo.pbsi_comm));
53 return (NULL);
54 #else
55 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };
56 size_t size;
57 struct kinfo_proc kp;
59 if ((mib[3] = tcgetpgrp(fd)) == -1)
60 return (NULL);
62 size = sizeof kp;
63 if (sysctl(mib, 4, &kp, &size, NULL, 0) == -1)
64 return (NULL);
65 if (size != (sizeof kp) || *kp.kp_proc.p_comm == '\0')
66 return (NULL);
68 return (strdup(kp.kp_proc.p_comm));
69 #endif
72 char *
73 osdep_get_cwd(int fd)
75 static char wd[PATH_MAX];
76 struct proc_vnodepathinfo pathinfo;
77 pid_t pgrp;
78 int ret;
80 if ((pgrp = tcgetpgrp(fd)) == -1)
81 return (NULL);
83 ret = proc_pidinfo(pgrp, PROC_PIDVNODEPATHINFO, 0,
84 &pathinfo, sizeof pathinfo);
85 if (ret == sizeof pathinfo) {
86 strlcpy(wd, pathinfo.pvi_cdir.vip_path, sizeof wd);
87 return (wd);
89 return (NULL);
92 struct event_base *
93 osdep_event_init(void)
95 struct event_base *base;
98 * On OS X, kqueue and poll are both completely broken and don't
99 * work on anything except socket file descriptors (yes, really).
101 setenv("EVENT_NOKQUEUE", "1", 1);
102 setenv("EVENT_NOPOLL", "1", 1);
104 base = event_init();
105 unsetenv("EVENT_NOKQUEUE");
106 unsetenv("EVENT_NOPOLL");
107 return (base);