qnetd-advanced-settings.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 = QNETD_DEFAULT_DPD_INTERVAL;
  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_TIE_BREAKER;
  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. */
  88. int
  89. qnetd_advanced_settings_set(struct qnetd_advanced_settings *settings,
  90. const char *option, const char *value)
  91. {
  92. long long int tmpll;
  93. if (strcasecmp(option, "listen_backlog") == 0) {
  94. if (utils_strtonum(value, QNETD_MIN_LISTEN_BACKLOG, INT_MAX, &tmpll) == -1) {
  95. return (-2);
  96. }
  97. settings->listen_backlog = (int)tmpll;
  98. } else if (strcasecmp(option, "max_client_send_buffers") == 0) {
  99. if (utils_strtonum(value, QNETD_MIN_CLIENT_SEND_BUFFERS, LLONG_MAX, &tmpll) == -1) {
  100. return (-2);
  101. }
  102. settings->max_client_send_buffers = (size_t)tmpll;
  103. } else if (strcasecmp(option, "max_client_send_size") == 0) {
  104. if (utils_strtonum(value, QNETD_MIN_CLIENT_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  105. return (-2);
  106. }
  107. settings->max_client_send_size = (size_t)tmpll;
  108. } else if (strcasecmp(option, "max_client_receive_size") == 0) {
  109. if (utils_strtonum(value, QNETD_MIN_CLIENT_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  110. return (-2);
  111. }
  112. settings->max_client_receive_size = (size_t)tmpll;
  113. } else if (strcasecmp(option, "nss_db_dir") == 0) {
  114. free(settings->nss_db_dir);
  115. if ((settings->nss_db_dir = strdup(value)) == NULL) {
  116. return (-1);
  117. }
  118. } else if (strcasecmp(option, "cert_nickname") == 0) {
  119. free(settings->cert_nickname);
  120. if ((settings->cert_nickname = strdup(value)) == NULL) {
  121. return (-1);
  122. }
  123. } else if (strcasecmp(option, "heartbeat_interval_min") == 0) {
  124. if (utils_strtonum(value, QNETD_MIN_HEARTBEAT_INTERVAL, UINT32_MAX, &tmpll) == -1) {
  125. return (-2);
  126. }
  127. settings->heartbeat_interval_min = (uint32_t)tmpll;
  128. } else if (strcasecmp(option, "heartbeat_interval_max") == 0) {
  129. if (utils_strtonum(value, QNETD_MIN_HEARTBEAT_INTERVAL, UINT32_MAX, &tmpll) == -1) {
  130. return (-2);
  131. }
  132. settings->heartbeat_interval_max = (uint32_t)tmpll;
  133. } else if (strcasecmp(option, "dpd_enabled") == 0) {
  134. if ((tmpll = utils_parse_bool_str(value)) == -1) {
  135. return (-2);
  136. }
  137. settings->dpd_enabled = (uint8_t)tmpll;
  138. } else if (strcasecmp(option, "dpd_interval") == 0) {
  139. if (utils_strtonum(value, QNETD_MIN_DPD_INTERVAL, UINT32_MAX, &tmpll) == -1) {
  140. return (-2);
  141. }
  142. settings->dpd_interval = (uint32_t)tmpll;
  143. } else if (strcasecmp(option, "lock_file") == 0) {
  144. free(settings->lock_file);
  145. if ((settings->lock_file = strdup(value)) == NULL) {
  146. return (-1);
  147. }
  148. } else if (strcasecmp(option, "local_socket_file") == 0) {
  149. free(settings->local_socket_file);
  150. if ((settings->local_socket_file = strdup(value)) == NULL) {
  151. return (-1);
  152. }
  153. } else if (strcasecmp(option, "local_socket_backlog") == 0) {
  154. if (utils_strtonum(value, QNETD_MIN_LOCAL_SOCKET_BACKLOG, INT_MAX, &tmpll) == -1) {
  155. return (-2);
  156. }
  157. settings->local_socket_backlog = (int)tmpll;
  158. } else if (strcasecmp(option, "ipc_max_clients") == 0) {
  159. if (utils_strtonum(value, QNETD_MIN_IPC_MAX_CLIENTS, LLONG_MAX, &tmpll) == -1) {
  160. return (-2);
  161. }
  162. settings->ipc_max_clients = (size_t)tmpll;
  163. } else if (strcasecmp(option, "ipc_max_receive_size") == 0) {
  164. if (utils_strtonum(value, QNETD_MIN_IPC_RECEIVE_SEND_SIZE, LLONG_MAX, &tmpll) == -1) {
  165. return (-2);
  166. }
  167. settings->ipc_max_receive_size = (size_t)tmpll;
  168. } else if (strcasecmp(option, "ipc_max_send_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_send_size = (size_t)tmpll;
  173. } else if (strcasecmp(option, "keep_active_partition_tie_breaker") == 0) {
  174. if ((tmpll = utils_parse_bool_str(value)) == -1) {
  175. return (-2);
  176. }
  177. settings->keep_active_partition_tie_breaker = (uint8_t)tmpll;
  178. } else {
  179. return (-1);
  180. }
  181. return (0);
  182. }