2011-07-09 09:42:33 +00:00
|
|
|
/* $Id$ */
|
2009-01-20 19:35:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2014-10-21 06:11:44 +00:00
|
|
|
#include <sys/param.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
|
2010-12-30 20:41:08 +00:00
|
|
|
#include <event.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
#include <stdio.h>
|
2010-12-30 20:41:08 +00:00
|
|
|
#include <stdlib.h>
|
2009-01-20 19:35:03 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-02-13 00:43:04 +00:00
|
|
|
char *
|
|
|
|
osdep_get_name(int fd, unused char *tty)
|
2009-01-20 19:35:03 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char *path, *buf;
|
|
|
|
size_t len;
|
|
|
|
int ch;
|
2009-01-26 22:57:20 +00:00
|
|
|
pid_t pgrp;
|
|
|
|
|
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1)
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-01-20 19:35:03 +00:00
|
|
|
|
|
|
|
xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
|
2009-01-26 22:57:20 +00:00
|
|
|
if ((f = fopen(path, "r")) == NULL) {
|
2012-07-11 19:50:46 +00:00
|
|
|
free(path);
|
2009-02-13 00:43:04 +00:00
|
|
|
return (NULL);
|
2009-01-26 22:57:20 +00:00
|
|
|
}
|
2012-07-11 19:50:46 +00:00
|
|
|
free(path);
|
2009-01-20 19:35:03 +00:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
buf = NULL;
|
|
|
|
while ((ch = fgetc(f)) != EOF) {
|
|
|
|
if (ch == '\0')
|
|
|
|
break;
|
2014-10-21 06:11:44 +00:00
|
|
|
buf = xrealloc(buf, len + 2);
|
2009-01-20 19:35:03 +00:00
|
|
|
buf[len++] = ch;
|
|
|
|
}
|
|
|
|
if (buf != NULL)
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2009-02-02 15:46:36 +00:00
|
|
|
fclose(f);
|
2009-02-13 00:43:04 +00:00
|
|
|
return (buf);
|
2009-01-20 19:35:03 +00:00
|
|
|
}
|
2010-12-30 20:41:08 +00:00
|
|
|
|
2011-12-09 16:37:29 +00:00
|
|
|
char *
|
2012-09-24 13:05:38 +00:00
|
|
|
osdep_get_cwd(int fd)
|
2011-12-09 16:37:29 +00:00
|
|
|
{
|
|
|
|
static char target[MAXPATHLEN + 1];
|
|
|
|
char *path;
|
2014-04-17 22:48:19 +00:00
|
|
|
pid_t pgrp, sid;
|
2011-12-09 16:37:29 +00:00
|
|
|
ssize_t n;
|
|
|
|
|
2012-09-24 13:05:38 +00:00
|
|
|
if ((pgrp = tcgetpgrp(fd)) == -1)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp);
|
2011-12-09 16:37:29 +00:00
|
|
|
n = readlink(path, target, MAXPATHLEN);
|
2012-07-11 19:50:46 +00:00
|
|
|
free(path);
|
2014-04-17 22:48:19 +00:00
|
|
|
|
|
|
|
if (n == -1 && ioctl(fd, TIOCGSID, &sid) != -1) {
|
|
|
|
xasprintf(&path, "/proc/%lld/cwd", (long long) sid);
|
|
|
|
n = readlink(path, target, MAXPATHLEN);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
2011-12-09 16:37:29 +00:00
|
|
|
if (n > 0) {
|
|
|
|
target[n] = '\0';
|
|
|
|
return (target);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2010-12-30 20:41:08 +00:00
|
|
|
struct event_base *
|
|
|
|
osdep_event_init(void)
|
|
|
|
{
|
2013-08-23 14:25:05 +00:00
|
|
|
/* On Linux, epoll doesn't work on /dev/null (yes, really). */
|
|
|
|
setenv("EVENT_NOEPOLL", "1", 1);
|
2010-12-30 20:41:08 +00:00
|
|
|
return (event_init());
|
|
|
|
}
|