4
0

qnetd-advanced-settings.c 7.7 KB

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