This source file includes following definitions.
- pmix_util_getid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 #include <src/include/pmix_config.h>
26 #include "include/pmix_common.h"
27 #include "src/include/pmix_socket_errno.h"
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #ifdef HAVE_SYS_TYPES_H
33 #include <sys/types.h>
34 #endif
35
36 #include "src/include/pmix_globals.h"
37 #include "src/util/error.h"
38 #include "src/util/output.h"
39
40 #include "src/util/getid.h"
41
42 pmix_status_t pmix_util_getid(int sd, uid_t *uid, gid_t *gid)
43 {
44 #if defined(SO_PEERCRED)
45 #ifdef HAVE_STRUCT_SOCKPEERCRED_UID
46 #define HAVE_STRUCT_UCRED_UID
47 struct sockpeercred ucred;
48 #else
49 struct ucred ucred;
50 #endif
51 socklen_t crlen = sizeof (ucred);
52 #endif
53
54 #if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
55
56 pmix_output_verbose(2, pmix_globals.debug_output,
57 "getid: checking getsockopt for peer credentials");
58 if (getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &ucred, &crlen) < 0) {
59 pmix_output_verbose(2, pmix_globals.debug_output,
60 "getid: getsockopt SO_PEERCRED failed: %s",
61 strerror (pmix_socket_errno));
62 return PMIX_ERR_INVALID_CRED;
63 }
64 #if defined(HAVE_STRUCT_UCRED_UID)
65 *uid = ucred.uid;
66 *gid = ucred.gid;
67 #else
68 *uid = ucred.cr_uid;
69 *gid = ucred.cr_gid;
70 #endif
71
72 #elif defined(HAVE_GETPEEREID)
73 pmix_output_verbose(2, pmix_globals.debug_output,
74 "getid: checking getpeereid for peer credentials");
75 if (0 != getpeereid(sd, uid, gid)) {
76 pmix_output_verbose(2, pmix_globals.debug_output,
77 "getid: getsockopt getpeereid failed: %s",
78 strerror (pmix_socket_errno));
79 return PMIX_ERR_INVALID_CRED;
80 }
81 #else
82 return PMIX_ERR_NOT_SUPPORTED;
83 #endif
84
85 return PMIX_SUCCESS;
86 }