qdevice-advanced-settings.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2015-2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <errno.h>
  37. #include "dynar.h"
  38. #include "dynar-getopt-lex.h"
  39. #include "dynar-str.h"
  40. #include "qdevice-config.h"
  41. #include "qnet-config.h"
  42. #include "qdevice-advanced-settings.h"
  43. #include "utils.h"
  44. int
  45. qdevice_advanced_settings_init(struct qdevice_advanced_settings *settings)
  46. {
  47. memset(settings, 0, sizeof(*settings));
  48. if ((settings->lock_file = strdup(QDEVICE_DEFAULT_LOCK_FILE)) == NULL) {
  49. return (-1);
  50. }
  51. if ((settings->local_socket_file = strdup(QDEVICE_DEFAULT_LOCAL_SOCKET_FILE)) == NULL) {
  52. return (-1);
  53. }
  54. settings->local_socket_backlog = QDEVICE_DEFAULT_LOCAL_SOCKET_BACKLOG;
  55. settings->max_cs_try_again = QDEVICE_DEFAULT_MAX_CS_TRY_AGAIN;
  56. if ((settings->votequorum_device_name = strdup(QDEVICE_DEFAULT_VOTEQUORUM_DEVICE_NAME)) == NULL) {
  57. return (-1);
  58. }
  59. settings->ipc_max_clients = QDEVICE_DEFAULT_IPC_MAX_CLIENTS;
  60. settings->ipc_max_receive_size = QDEVICE_DEFAULT_IPC_MAX_RECEIVE_SIZE;
  61. settings->ipc_max_send_size = QDEVICE_DEFAULT_IPC_MAX_SEND_SIZE;
  62. if ((settings->net_nss_db_dir = strdup(QDEVICE_NET_DEFAULT_NSS_DB_DIR)) == NULL) {
  63. return (-1);
  64. }
  65. settings->net_initial_msg_receive_size = QDEVICE_NET_DEFAULT_INITIAL_MSG_RECEIVE_SIZE;
  66. settings->net_initial_msg_send_size = QDEVICE_NET_DEFAULT_INITIAL_MSG_SEND_SIZE;
  67. settings->net_min_msg_send_size = QDEVICE_NET_DEFAULT_MIN_MSG_SEND_SIZE;
  68. settings->net_max_msg_receive_size = QDEVICE_NET_DEFAULT_MAX_MSG_RECEIVE_SIZE;
  69. settings->net_max_send_buffers = QDEVICE_NET_DEFAULT_MAX_SEND_BUFFERS;
  70. if ((settings->net_nss_qnetd_cn = strdup(QDEVICE_NET_DEFAULT_NSS_QNETD_CN)) == NULL) {
  71. return (-1);
  72. }
  73. if ((settings->net_nss_client_cert_nickname =
  74. strdup(QDEVICE_NET_DEFAULT_NSS_CLIENT_CERT_NICKNAME)) == NULL) {
  75. return (-1);
  76. }
  77. settings->net_heartbeat_interval_min = QDEVICE_NET_DEFAULT_HEARTBEAT_INTERVAL_MIN;
  78. settings->net_heartbeat_interval_max = QDEVICE_NET_DEFAULT_HEARTBEAT_INTERVAL_MAX;
  79. settings->net_min_connect_timeout = QDEVICE_NET_DEFAULT_MIN_CONNECT_TIMEOUT;
  80. settings->net_max_connect_timeout = QDEVICE_NET_DEFAULT_MAX_CONNECT_TIMEOUT;
  81. settings->net_test_algorithm_enabled = QDEVICE_NET_DEFAULT_TEST_ALGORITHM_ENABLED;
  82. settings->master_wins = QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_MODEL;
  83. return (0);
  84. }
  85. void
  86. qdevice_advanced_settings_destroy(struct qdevice_advanced_settings *settings)
  87. {
  88. free(settings->local_socket_file);
  89. free(settings->lock_file);
  90. free(settings->votequorum_device_name);
  91. free(settings->net_nss_db_dir);
  92. free(settings->net_nss_qnetd_cn);
  93. free(settings->net_nss_client_cert_nickname);
  94. }
  95. /*
  96. * 0 - No error
  97. * -1 - Unknown option
  98. * -2 - Incorrect value
  99. */
  100. int
  101. qdevice_advanced_settings_set(struct qdevice_advanced_settings *settings,
  102. const char *option, const char *value)
  103. {
  104. long long int tmpll;
  105. char *ep;
  106. if (strcasecmp(option, "lock_file") == 0) {
  107. free(settings->lock_file);
  108. if ((settings->lock_file = strdup(value)) == NULL) {
  109. return (-1);
  110. }
  111. } else if (strcasecmp(option, "local_socket_file") == 0) {
  112. free(settings->local_socket_file);
  113. if ((settings->local_socket_file = strdup(value)) == NULL) {
  114. return (-1);
  115. }
  116. } else if (strcasecmp(option, "local_socket_backlog") == 0) {
  117. tmpll = strtoll(value, &ep, 10);
  118. if (tmpll < QDEVICE_MIN_LOCAL_SOCKET_BACKLOG || errno != 0 || *ep != '\0') {
  119. return (-2);
  120. }
  121. settings->local_socket_backlog = (int)tmpll;
  122. } else if (strcasecmp(option, "max_cs_try_again") == 0) {
  123. tmpll = strtoll(value, &ep, 10);
  124. if (tmpll < QDEVICE_MIN_MAX_CS_TRY_AGAIN || errno != 0 || *ep != '\0') {
  125. return (-2);
  126. }
  127. settings->max_cs_try_again = (int)tmpll;
  128. } else if (strcasecmp(option, "votequorum_device_name") == 0) {
  129. free(settings->votequorum_device_name);
  130. if ((settings->votequorum_device_name = strdup(value)) == NULL) {
  131. return (-1);
  132. }
  133. } else if (strcasecmp(option, "ipc_max_clients") == 0) {
  134. tmpll = strtoll(value, &ep, 10);
  135. if (tmpll < QDEVICE_MIN_IPC_MAX_CLIENTS || errno != 0 || *ep != '\0') {
  136. return (-2);
  137. }
  138. settings->ipc_max_clients = (size_t)tmpll;
  139. } else if (strcasecmp(option, "ipc_max_receive_size") == 0) {
  140. tmpll = strtoll(value, &ep, 10);
  141. if (tmpll < QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  142. return (-2);
  143. }
  144. settings->ipc_max_receive_size = (size_t)tmpll;
  145. } else if (strcasecmp(option, "ipc_max_send_size") == 0) {
  146. tmpll = strtoll(value, &ep, 10);
  147. if (tmpll < QDEVICE_MIN_IPC_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  148. return (-2);
  149. }
  150. settings->ipc_max_send_size = (size_t)tmpll;
  151. } else if (strcasecmp(option, "net_nss_db_dir") == 0) {
  152. free(settings->net_nss_db_dir);
  153. if ((settings->net_nss_db_dir = strdup(value)) == NULL) {
  154. return (-1);
  155. }
  156. } else if (strcasecmp(option, "net_initial_msg_receive_size") == 0) {
  157. tmpll = strtoll(value, &ep, 10);
  158. if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  159. return (-2);
  160. }
  161. settings->net_initial_msg_receive_size = (size_t)tmpll;
  162. } else if (strcasecmp(option, "net_initial_msg_send_size") == 0) {
  163. tmpll = strtoll(value, &ep, 10);
  164. if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  165. return (-2);
  166. }
  167. settings->net_initial_msg_send_size = (size_t)tmpll;
  168. } else if (strcasecmp(option, "net_min_msg_send_size") == 0) {
  169. tmpll = strtoll(value, &ep, 10);
  170. if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  171. return (-2);
  172. }
  173. settings->net_min_msg_send_size = (size_t)tmpll;
  174. } else if (strcasecmp(option, "net_max_msg_receive_size") == 0) {
  175. tmpll = strtoll(value, &ep, 10);
  176. if (tmpll < QDEVICE_NET_MIN_MSG_RECEIVE_SEND_SIZE || errno != 0 || *ep != '\0') {
  177. return (-2);
  178. }
  179. settings->net_max_msg_receive_size = (size_t)tmpll;
  180. } else if (strcasecmp(option, "net_max_send_buffers") == 0) {
  181. tmpll = strtoll(value, &ep, 10);
  182. if (tmpll < QDEVICE_NET_MIN_MAX_SEND_BUFFERS || errno != 0 || *ep != '\0') {
  183. return (-2);
  184. }
  185. settings->net_max_send_buffers = (size_t)tmpll;
  186. } else if (strcasecmp(option, "net_nss_qnetd_cn") == 0) {
  187. free(settings->net_nss_qnetd_cn);
  188. if ((settings->net_nss_qnetd_cn = strdup(value)) == NULL) {
  189. return (-1);
  190. }
  191. } else if (strcasecmp(option, "net_nss_client_cert_nickname") == 0) {
  192. free(settings->net_nss_client_cert_nickname);
  193. if ((settings->net_nss_client_cert_nickname = strdup(value)) == NULL) {
  194. return (-1);
  195. }
  196. } else if (strcasecmp(option, "net_heartbeat_interval_min") == 0) {
  197. tmpll = strtoll(value, &ep, 10);
  198. if (tmpll < QDEVICE_NET_MIN_HEARTBEAT_INTERVAL || errno != 0 || *ep != '\0') {
  199. return (-2);
  200. }
  201. settings->net_heartbeat_interval_min = (uint32_t)tmpll;
  202. } else if (strcasecmp(option, "net_heartbeat_interval_max") == 0) {
  203. tmpll = strtoll(value, &ep, 10);
  204. if (tmpll < QDEVICE_NET_MIN_HEARTBEAT_INTERVAL || errno != 0 || *ep != '\0') {
  205. return (-2);
  206. }
  207. settings->net_heartbeat_interval_max = (uint32_t)tmpll;
  208. } else if (strcasecmp(option, "net_min_connect_timeout") == 0) {
  209. tmpll = strtoll(value, &ep, 10);
  210. if (tmpll < QDEVICE_NET_MIN_CONNECT_TIMEOUT || errno != 0 || *ep != '\0') {
  211. return (-2);
  212. }
  213. settings->net_min_connect_timeout = (uint32_t)tmpll;
  214. } else if (strcasecmp(option, "net_max_connect_timeout") == 0) {
  215. tmpll = strtoll(value, &ep, 10);
  216. if (tmpll < QDEVICE_NET_MIN_CONNECT_TIMEOUT || errno != 0 || *ep != '\0') {
  217. return (-2);
  218. }
  219. settings->net_max_connect_timeout = (uint32_t)tmpll;
  220. } else if (strcasecmp(option, "net_test_algorithm_enabled") == 0) {
  221. if ((tmpll = utils_parse_bool_str(value)) == -1) {
  222. return (-2);
  223. }
  224. settings->net_test_algorithm_enabled = (uint8_t)tmpll;
  225. } else if (strcasecmp(option, "master_wins") == 0) {
  226. tmpll = utils_parse_bool_str(value);
  227. if (tmpll == 0) {
  228. settings->master_wins = QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_FORCE_OFF;
  229. } else if (tmpll == 1) {
  230. settings->master_wins = QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_FORCE_ON;
  231. } else if (strcasecmp(value, "model") == 0) {
  232. settings->master_wins = QDEVICE_ADVANCED_SETTINGS_MASTER_WINS_MODEL;
  233. } else {
  234. return (-2);
  235. }
  236. } else {
  237. return (-1);
  238. }
  239. return (0);
  240. }