tcgetsid.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright (C) 1997, 1999 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <termios.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/types.h>
  18. #include <unistd.h>
  19. /* Return the session ID of FD. */
  20. pid_t
  21. tcgetsid (int fd)
  22. {
  23. pid_t pgrp;
  24. pid_t sid;
  25. #ifdef TIOCGSID
  26. static smallint tiocgsid_does_not_work;
  27. if (! tiocgsid_does_not_work)
  28. {
  29. int serrno = errno;
  30. if (ioctl (fd, TIOCGSID, &sid) < 0)
  31. {
  32. if (errno == EINVAL)
  33. {
  34. tiocgsid_does_not_work = 1;
  35. __set_errno (serrno);
  36. }
  37. else
  38. return (pid_t) -1;
  39. }
  40. else
  41. return (pid_t) sid;
  42. }
  43. #endif
  44. pgrp = tcgetpgrp (fd);
  45. if (pgrp == -1)
  46. return (pid_t) -1;
  47. sid = getsid (pgrp);
  48. if (sid == -1 && errno == ESRCH)
  49. __set_errno (ENOTTY);
  50. return sid;
  51. }