osdep-darwin.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* $OpenBSD$ */
  2. /*
  3. * Copyright (c) 2009 Joshua Elsasser <josh@elsasser.org>
  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 <event.h>
  19. #include <libproc.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. char *osdep_get_name(int, char *);
  24. char *osdep_get_cwd(int);
  25. struct event_base *osdep_event_init(void);
  26. #define __unused __attribute__ ((__unused__))
  27. char *
  28. osdep_get_name(int fd, __unused char *tty)
  29. {
  30. struct proc_bsdinfo bsdinfo;
  31. pid_t pgrp;
  32. int ret;
  33. if ((pgrp = tcgetpgrp(fd)) == -1)
  34. return (NULL);
  35. ret = proc_pidinfo(pgrp, PROC_PIDTBSDINFO, 0,
  36. &bsdinfo, sizeof bsdinfo);
  37. if (ret == sizeof bsdinfo && *bsdinfo.pbi_comm != '\0')
  38. return (strdup(bsdinfo.pbi_comm));
  39. return (NULL);
  40. }
  41. char *
  42. osdep_get_cwd(int fd)
  43. {
  44. static char wd[PATH_MAX];
  45. struct proc_vnodepathinfo pathinfo;
  46. pid_t pgrp;
  47. int ret;
  48. if ((pgrp = tcgetpgrp(fd)) == -1)
  49. return (NULL);
  50. ret = proc_pidinfo(pgrp, PROC_PIDVNODEPATHINFO, 0,
  51. &pathinfo, sizeof pathinfo);
  52. if (ret == sizeof pathinfo) {
  53. strlcpy(wd, pathinfo.pvi_cdir.vip_path, sizeof wd);
  54. return (wd);
  55. }
  56. return (NULL);
  57. }
  58. struct event_base *
  59. osdep_event_init(void)
  60. {
  61. /*
  62. * On OS X, kqueue and poll are both completely broken and don't
  63. * work on anything except socket file descriptors (yes, really).
  64. */
  65. setenv("EVENT_NOKQUEUE", "1", 1);
  66. setenv("EVENT_NOPOLL", "1", 1);
  67. return (event_init());
  68. }