osdep-sunos.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Todd Carson <toc@daybefore.net>
  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 <event.h>
  20. #include <fcntl.h>
  21. #include <procfs.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include "tmux.h"
  26. char *
  27. osdep_get_name(int fd, char *tty)
  28. {
  29. struct psinfo p;
  30. struct stat st;
  31. char *path;
  32. ssize_t bytes;
  33. int f;
  34. pid_t pgrp;
  35. if ((f = open(tty, O_RDONLY)) < 0)
  36. return (NULL);
  37. if (fstat(f, &st) != 0 || ioctl(f, TIOCGPGRP, &pgrp) != 0) {
  38. close(f);
  39. return (NULL);
  40. }
  41. close(f);
  42. xasprintf(&path, "/proc/%u/psinfo", (u_int) pgrp);
  43. f = open(path, O_RDONLY);
  44. free(path);
  45. if (f < 0)
  46. return (NULL);
  47. bytes = read(f, &p, sizeof(p));
  48. close(f);
  49. if (bytes != sizeof(p))
  50. return (NULL);
  51. if (p.pr_ttydev != st.st_rdev)
  52. return (NULL);
  53. return (xstrdup(p.pr_fname));
  54. }
  55. char *
  56. osdep_get_cwd(int fd)
  57. {
  58. static char target[MAXPATHLEN + 1];
  59. char *path;
  60. const char *ttypath;
  61. ssize_t n;
  62. pid_t pgrp;
  63. int retval, ttyfd;
  64. if ((ttypath = ptsname(fd)) == NULL)
  65. return (NULL);
  66. if ((ttyfd = open(ttypath, O_RDONLY|O_NOCTTY)) == -1)
  67. return (NULL);
  68. retval = ioctl(ttyfd, TIOCGPGRP, &pgrp);
  69. close(ttyfd);
  70. if (retval == -1)
  71. return (NULL);
  72. xasprintf(&path, "/proc/%u/path/cwd", (u_int) pgrp);
  73. n = readlink(path, target, MAXPATHLEN);
  74. free(path);
  75. if (n > 0) {
  76. target[n] = '\0';
  77. return (target);
  78. }
  79. return (NULL);
  80. }
  81. struct event_base *
  82. osdep_event_init(void)
  83. {
  84. return (event_init());
  85. }