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 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcachedutil/common.h"
18 // Never look at the stat object directly.
20 pid_t
libmemcached_util_getpid(const char *hostname
, in_port_t port
, memcached_return_t
*ret
) {
23 memcached_return_t unused
;
28 memcached_st
*memc_ptr
= memcached_create(NULL
);
29 if (memc_ptr
== NULL
) {
30 *ret
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
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) {
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
44 const memcached_instance_st
*instance
= memcached_server_instance_by_position(memc_ptr
, 0);
46 assert_msg(instance
and memcached_server_error(instance
), " ");
47 if (instance
and memcached_server_error(instance
)) {
48 rc
= memcached_server_error_return(instance
);
52 memcached_stat_free(memc_ptr
, stat
);
54 memcached_free(memc_ptr
);
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
);
69 memcached_return_t unused
;
73 if (LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
74 *ret
= MEMCACHED_NOT_SUPPORTED
;
78 memcached_st
*memc_ptr
= memcached_create(NULL
);
80 *ret
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
84 if (memcached_failed(*ret
= memcached_set_sasl_auth_data(memc_ptr
, username
, password
))) {
85 memcached_free(memc_ptr
);
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) {
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
99 const memcached_instance_st
*instance
= memcached_server_instance_by_position(memc_ptr
, 0);
102 assert_msg(instance
and instance
->error_messages
, " ");
104 if (instance
and memcached_server_error(instance
)) {
105 rc
= memcached_server_error_return(instance
);
109 memcached_stat_free(memc_ptr
, stat
);
111 memcached_free(memc_ptr
);