qnetd-advanced-settings.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2015-2020 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 <errno.h>
  36. #include <limits.h>
  37. #include <string.h>
  38. #include "dynar.h"
  39. #include "dynar-getopt-lex.h"
  40. #include "dynar-str.h"
  41. #include "qnet-config.h"
  42. #include "qnetd-advanced-settings.h"
  43. #include "utils.h"
  44. int
  45. qnetd_advanced_settings_init(struct qnetd_advanced_settings *settings)
  46. {
  47. memset(settings, 0, sizeof(*settings));
  48. settings->listen_backlog = QNETD_DEFAULT_LISTEN_BACKLOG;
  49. settings->max_client_send_buffers = QNETD_DEFAULT_MAX_CLIENT_SEND_BUFFERS;
  50. settings->max_client_send_size = QNETD_DEFAULT_MAX_CLIENT_SEND_SIZE;
  51. settings->max_client_receive_size = QNETD_DEFAULT_MAX_CLIENT_RECEIVE_SIZE;
  52. if ((settings->nss_db_dir = strdup(QNETD_DEFAULT_NSS_DB_DIR)) == NULL) {
  53. return (-1);
  54. }
  55. if ((settings->cert_nickname = strdup(QNETD_DEFAULT_CERT_NICKNAME)) == NULL) {
  56. return (-1);
  57. }
  58. settings->heartbeat_interval_min = QNETD_DEFAULT_HEARTBEAT_INTERVAL_MIN;
  59. settings->heartbeat_interval_max = QNETD_DEFAULT_HEARTBEAT_INTERVAL_MAX;
  60. settings->dpd_enabled = QNETD_DEFAULT_DPD_ENABLED;
  61. settings->dpd_interval_coefficient = QNETD_DEFAULT_DPD_INTERVAL_COEFFICIENT;
  62. if ((settings->lock_file = strdup(QNETD_DEFAULT_LOCK_FILE)) == NULL) {
  63. return (-1);
  64. }
  65. if ((settings->local_socket_file = strdup(QNETD_DEFAULT_LOCAL_SOCKET_FILE)) == NULL) {
  66. return (-1);
  67. }
  68. settings->local_socket_backlog = QNETD_DEFAULT_LOCAL_SOCKET_BACKLOG;
  69. settings->ipc_max_clients = QNETD_DEFAULT_IPC_MAX_CLIENTS;
  70. settings->ipc_max_receive_size = QNETD_DEFAULT_IPC_MAX_RECEIVE_SIZE;
  71. settings->ipc_max_send_size = QNETD_DEFAULT_IPC_MAX_SEND_SIZE;
  72. settings->keep_active_partition_tie_breaker = QNETD_DEFAULT_KEEP_ACTIVE_PARTITION_TB;
  73. return (0);
  74. }
  75. void
  76. qnetd_advanced_settings_destroy(struct qnetd_advanced_settings *settings)
  77. {
  78. free(settings->nss_db_dir);
  79. free(settings->cert_nickname);
  80. free(settings->lock_file);
  81. free(settings->local_socket_file);
  82. }
  83. /*
  84. * 0 - No error
  85. * -1 - Unknown option
  86. * -2 - Incorrect value
  87. * -3 - Deprecated value
  88. */
  89. int
  90. qnetd_advanced_settings_set(struct qnetd_advanced_settings *settings,
  91. const char *option, const char *value)
  92. {
  93. long long int tmpll;
  94. double tmpdbl;
  95. if (strcasecmp(option, "listen_backlog") == 0) {
  96. if (utils_strtonum(value, QNETD_MIN_LISTEN_BACKLOG, INT_MAX, &tmpll) == -1) {
  97. return (-2);
  98. }
  99. settings->listen_backlog = (int)tmpll;
  100. } else if (strcasecmp(option, "max_client_send_buffers") == 0) {
  101. if (utils_strtonum(value, QNETD_MIN_CLIENT_SEND_BUFFERS, LLONG_MAX, &tmpll) == -1) {
  102. return (-2);
  103. }
  104. settings->max_client_send_buffers = (size_t)tmpll;
  105. } else if (strcasecmp(option, "max_client_send_size") == 0) {
  106. if (utils_strtonum(value, QNETD_MIN_CLIENT_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  107. return (-2);
  108. }
  109. settings->max_client_send_size = (size_t)tmpll;
  110. } else if (strcasecmp(option, "max_client_receive_size") == 0) {
  111. if (utils_strtonum(value, QNETD_MIN_CLIENT_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  112. return (-2);
  113. }
  114. settings->max_client_receive_size = (size_t)tmpll;
  115. } else if (strcasecmp(option, "nss_db_dir") == 0) {
  116. free(settings->nss_db_dir);
  117. if ((settings->nss_db_dir = strdup(value)) == NULL) {
  118. return (-1);
  119. }
  120. } else if (strcasecmp(option, "cert_nickname") == 0) {
  121. free(settings->cert_nickname);
  122. if ((settings->cert_nickname = strdup(value)) == NULL) {
  123. return (-1);
  124. }
  125. } else if (strcasecmp(option, "heartbeat_interval_min") == 0) {
  126. if (utils_strtonum(value, QNETD_MIN_HEARTBEAT_INTERVAL, UINT32_MAX, &tmpll) == -1) {
  127. return (-2);
  128. }
  129. settings->heartbeat_interval_min = (uint32_t)tmpll;
  130. } else if (strcasecmp(option, "heartbeat_interval_max") == 0) {
  131. if (utils_strtonum(value, QNETD_MIN_HEARTBEAT_INTERVAL, UINT32_MAX, &tmpll) == -1) {
  132. return (-2);
  133. }
  134. settings->heartbeat_interval_max = (uint32_t)tmpll;
  135. } else if (strcasecmp(option, "dpd_enabled") == 0) {
  136. if ((tmpll = utils_parse_bool_str(value)) == -1) {
  137. return (-2);
  138. }
  139. settings->dpd_enabled = (uint8_t)tmpll;
  140. } else if (strcasecmp(option, "dpd_interval") == 0) {
  141. return (-3);
  142. } else if (strcasecmp(option, "dpd_interval_coefficient") == 0) {
  143. if (utils_strtod(value, QNETD_MIN_DPD_INTERVAL_COEFFICIENT,
  144. QNETD_MAX_DPD_INTERVAL_COEFFICIENT, &tmpdbl) == -1) {
  145. return (-2);
  146. }
  147. settings->dpd_interval_coefficient = tmpdbl;
  148. } else if (strcasecmp(option, "lock_file") == 0) {
  149. free(settings->lock_file);
  150. if ((settings->lock_file = strdup(value)) == NULL) {
  151. return (-1);
  152. }
  153. } else if (strcasecmp(option, "local_socket_file") == 0) {
  154. free(settings->local_socket_file);
  155. if ((settings->local_socket_file = strdup(value)) == NULL) {
  156. return (-1);
  157. }
  158. } else if (strcasecmp(option, "local_socket_backlog") == 0) {
  159. if (utils_strtonum(value, QNETD_MIN_LOCAL_SOCKET_BACKLOG, INT_MAX, &tmpll) == -1) {
  160. return (-2);
  161. }
  162. settings->local_socket_backlog = (int)tmpll;
  163. } else if (strcasecmp(option, "ipc_max_clients") == 0) {
  164. if (utils_strtonum(value, QNETD_MIN_IPC_MAX_CLIENTS, LLONG_MAX, &tmpll) == -1) {
  165. return (-2);
  166. }
  167. settings->ipc_max_clients = (size_t)tmpll;
  168. } else if (strcasecmp(option, "ipc_max_receive_size") == 0) {
  169. if (utils_strtonum(value, QNETD_MIN_IPC_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  170. return (-2);
  171. }
  172. settings->ipc_max_receive_size = (size_t)tmpll;
  173. } else if (strcasecmp(option, "ipc_max_send_size") == 0) {
  174. if (utils_strtonum(value, QNETD_MIN_IPC_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  175. return (-2);
  176. }
  177. settings->ipc_max_send_size = (size_t)tmpll;
  178. } else if (strcasecmp(option, "keep_active_partition_tie_breaker") == 0) {
  179. if ((tmpll = utils_parse_bool_str(value)) == -1) {
  180. return (-2);
  181. }
  182. settings->keep_active_partition_tie_breaker = (uint8_t)tmpll;
  183. } else {
  184. return (-1);
  185. }
  186. return (0);
  187. }