jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.4KB

  1. /*
  2. Copyright (C) 2014-2017 Cédric Schieli
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation; either version 2.1
  6. of the License, or (at your option) any later version.
  7. This program 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef WIN32
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <grp.h>
  23. #ifdef __APPLE__
  24. #include <AvailabilityMacros.h>
  25. #endif
  26. #include "JackError.h"
  27. #endif
  28. int
  29. jack_group2gid(const char* group)
  30. {
  31. #ifdef WIN32
  32. return -1;
  33. #else
  34. size_t buflen;
  35. char *buf;
  36. int ret;
  37. struct group grp;
  38. struct group *result;
  39. if (!group || !*group)
  40. return -1;
  41. ret = strtol(group, &buf, 10);
  42. if (!*buf)
  43. return ret;
  44. /* MacOSX only defines _SC_GETGR_R_SIZE_MAX starting from 10.4 */
  45. #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
  46. buflen = 4096;
  47. #else
  48. buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
  49. if (buflen == -1)
  50. buflen = 4096;
  51. #endif
  52. buf = (char*)malloc(buflen);
  53. while (buf && ((ret = getgrnam_r(group, &grp, buf, buflen, &result)) == ERANGE)) {
  54. buflen *= 2;
  55. buf = (char*)realloc(buf, buflen);
  56. }
  57. if (!buf)
  58. return -1;
  59. free(buf);
  60. if (ret || !result)
  61. return -1;
  62. return grp.gr_gid;
  63. #endif
  64. }
  65. #ifndef WIN32
  66. int
  67. jack_promiscuous_perms(int fd, const char* path, gid_t gid)
  68. {
  69. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  70. if (gid >= 0) {
  71. if (((fd < 0) ? chown(path, -1, gid) : fchown(fd, -1, gid)) < 0) {
  72. jack_log("Cannot chgrp %s: %s. Falling back to permissive perms.", path, strerror(errno));
  73. } else {
  74. mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
  75. }
  76. }
  77. if (((fd < 0) ? chmod(path, mode) : fchmod(fd, mode)) < 0) {
  78. jack_log("Cannot chmod %s: %s. Falling back to default (umask) perms.", path, strerror(errno));
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. #endif