0c2f37daf8bdd06bad5cbcf041163657b016c038
[awesomized/libmemcached] / src / libmemcachedutil / pid.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcachedutil/common.h"
17
18 // Never look at the stat object directly.
19
20 pid_t libmemcached_util_getpid(const char *hostname, in_port_t port, memcached_return_t *ret) {
21 pid_t pid = -1;
22
23 memcached_return_t unused;
24 if (ret == NULL) {
25 ret = &unused;
26 }
27
28 memcached_st *memc_ptr = memcached_create(NULL);
29 if (memc_ptr == NULL) {
30 *ret = MEMCACHED_MEMORY_ALLOCATION_FAILURE;
31 return -1;
32 }
33
34 memcached_return_t rc = memcached_server_add(memc_ptr, hostname, port);
35 if (memcached_success(rc)) {
36 memcached_stat_st *stat = memcached_stat(memc_ptr, NULL, &rc);
37 if (memcached_success(rc) and stat and stat->pid != -1) {
38 pid = stat->pid;
39 } else if (memcached_success(rc)) {
40 rc = MEMCACHED_UNKNOWN_STAT_KEY; // Something went wrong if this happens
41 } else if (rc == MEMCACHED_SOME_ERRORS) // Generic answer, we will now find the specific reason
42 // (if one exists)
43 {
44 const memcached_instance_st *instance = memcached_server_instance_by_position(memc_ptr, 0);
45
46 assert_msg(instance and memcached_server_error(instance), " ");
47 if (instance and memcached_server_error(instance)) {
48 rc = memcached_server_error_return(instance);
49 }
50 }
51
52 memcached_stat_free(memc_ptr, stat);
53 }
54 memcached_free(memc_ptr);
55
56 *ret = rc;
57
58 return pid;
59 }
60
61 pid_t libmemcached_util_getpid2(const char *hostname, in_port_t port, const char *username,
62 const char *password, memcached_return_t *ret) {
63 if (username == NULL) {
64 return libmemcached_util_getpid(hostname, port, ret);
65 }
66
67 pid_t pid = -1;
68
69 memcached_return_t unused;
70 if (not ret)
71 ret = &unused;
72
73 if (LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
74 *ret = MEMCACHED_NOT_SUPPORTED;
75 return pid;
76 }
77
78 memcached_st *memc_ptr = memcached_create(NULL);
79 if (not memc_ptr) {
80 *ret = MEMCACHED_MEMORY_ALLOCATION_FAILURE;
81 return -1;
82 }
83
84 if (memcached_failed(*ret = memcached_set_sasl_auth_data(memc_ptr, username, password))) {
85 memcached_free(memc_ptr);
86 return false;
87 }
88
89 memcached_return_t rc = memcached_server_add(memc_ptr, hostname, port);
90 if (memcached_success(rc)) {
91 memcached_stat_st *stat = memcached_stat(memc_ptr, NULL, &rc);
92 if (memcached_success(rc) and stat and stat->pid != -1) {
93 pid = stat->pid;
94 } else if (memcached_success(rc)) {
95 rc = MEMCACHED_UNKNOWN_STAT_KEY; // Something went wrong if this happens
96 } else if (rc == MEMCACHED_SOME_ERRORS) // Generic answer, we will now find the specific reason
97 // (if one exists)
98 {
99 const memcached_instance_st *instance = memcached_server_instance_by_position(memc_ptr, 0);
100
101 #if 0
102 assert_msg(instance and instance->error_messages, " ");
103 #endif
104 if (instance and memcached_server_error(instance)) {
105 rc = memcached_server_error_return(instance);
106 }
107 }
108
109 memcached_stat_free(memc_ptr, stat);
110 }
111 memcached_free(memc_ptr);
112
113 *ret = rc;
114
115 return pid;
116 }