osdep-linux.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/param.h>
  20. #include <event.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include "tmux.h"
  25. char *
  26. osdep_get_name(int fd, __unused char *tty)
  27. {
  28. FILE *f;
  29. char *path, *buf;
  30. size_t len;
  31. int ch;
  32. pid_t pgrp;
  33. if ((pgrp = tcgetpgrp(fd)) == -1)
  34. return (NULL);
  35. xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
  36. if ((f = fopen(path, "r")) == NULL) {
  37. free(path);
  38. return (NULL);
  39. }
  40. free(path);
  41. len = 0;
  42. buf = NULL;
  43. while ((ch = fgetc(f)) != EOF) {
  44. if (ch == '\0')
  45. break;
  46. buf = xrealloc(buf, len + 2);
  47. buf[len++] = ch;
  48. }
  49. if (buf != NULL)
  50. buf[len] = '\0';
  51. fclose(f);
  52. return (buf);
  53. }
  54. char *
  55. osdep_get_cwd(int fd)
  56. {
  57. static char target[MAXPATHLEN + 1];
  58. char *path;
  59. pid_t pgrp, sid;
  60. ssize_t n;
  61. if ((pgrp = tcgetpgrp(fd)) == -1)
  62. return (NULL);
  63. xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp);
  64. n = readlink(path, target, MAXPATHLEN);
  65. free(path);
  66. if (n == -1 && ioctl(fd, TIOCGSID, &sid) != -1) {
  67. xasprintf(&path, "/proc/%lld/cwd", (long long) sid);
  68. n = readlink(path, target, MAXPATHLEN);
  69. free(path);
  70. }
  71. if (n > 0) {
  72. target[n] = '\0';
  73. return (target);
  74. }
  75. return (NULL);
  76. }
  77. struct event_base *
  78. osdep_event_init(void)
  79. {
  80. /* On Linux, epoll doesn't work on /dev/null (yes, really). */
  81. setenv("EVENT_NOEPOLL", "1", 1);
  82. return (event_init());
  83. }