Fix warning: catching polymorphic type ‘class std::exception’ by value
[jack2.git] / common / promiscuous.c
blobc6fed3c62f4ccf437912ff67808b7ee5f60b9bba
1 /*
2 Copyright (C) 2014-2017 Cédric Schieli
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public License
6 as published by the Free Software Foundation; either version 2.1
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef WIN32
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <grp.h>
28 #ifdef __APPLE__
29 #include <AvailabilityMacros.h>
30 #endif
31 #include "JackError.h"
32 #endif
35 int
36 jack_group2gid(const char* group)
38 #ifdef WIN32
39 return -1;
40 #else
41 size_t buflen;
42 char *buf;
43 int ret;
44 struct group grp;
45 struct group *result;
47 if (!group || !*group)
48 return -1;
50 ret = strtol(group, &buf, 10);
51 if (!*buf)
52 return ret;
54 /* MacOSX only defines _SC_GETGR_R_SIZE_MAX starting from 10.4 */
55 #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4
56 buflen = 4096;
57 #else
58 buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
59 if (buflen == -1)
60 buflen = 4096;
61 #endif
62 buf = (char*)malloc(buflen);
64 while (buf && ((ret = getgrnam_r(group, &grp, buf, buflen, &result)) == ERANGE)) {
65 buflen *= 2;
66 buf = (char*)realloc(buf, buflen);
68 if (!buf)
69 return -1;
70 free(buf);
71 if (ret || !result)
72 return -1;
73 return grp.gr_gid;
74 #endif
77 #ifndef WIN32
78 int
79 jack_promiscuous_perms(int fd, const char* path, gid_t gid)
81 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
82 if (gid >= 0) {
83 if (((fd < 0) ? chown(path, -1, gid) : fchown(fd, -1, gid)) < 0) {
84 jack_log("Cannot chgrp %s: %s. Falling back to permissive perms.", path, strerror(errno));
85 } else {
86 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
89 if (((fd < 0) ? chmod(path, mode) : fchmod(fd, mode)) < 0) {
90 jack_log("Cannot chmod %s: %s. Falling back to default (umask) perms.", path, strerror(errno));
91 return -1;
93 return 0;
95 #endif