qnetd-advanced-settings.c 7.0 KB

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