qnetd-advanced-settings.c 6.8 KB

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