mainconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2002-2005 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <sys/socket.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <pwd.h>
  44. #include <grp.h>
  45. #include <limits.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/list.h>
  48. #include <corosync/totem/totem.h>
  49. #include <corosync/engine/logsys.h>
  50. #include "util.h"
  51. #include "mainconfig.h"
  52. #include "mempool.h"
  53. static char error_string_response[512];
  54. static struct objdb_iface_ver0 *global_objdb;
  55. static void add_logsys_config_notification(
  56. struct objdb_iface_ver0 *objdb,
  57. struct main_config *main_config);
  58. /* This just makes the code below a little neater */
  59. static inline int objdb_get_string (
  60. struct objdb_iface_ver0 *objdb,
  61. hdb_handle_t object_service_handle,
  62. char *key, char **value)
  63. {
  64. int res;
  65. *value = NULL;
  66. if ( !(res = objdb->object_key_get (object_service_handle,
  67. key,
  68. strlen (key),
  69. (void *)value,
  70. NULL))) {
  71. if (*value) {
  72. return 0;
  73. }
  74. }
  75. return -1;
  76. }
  77. static inline void objdb_get_int (
  78. struct objdb_iface_ver0 *objdb,
  79. hdb_handle_t object_service_handle,
  80. char *key, unsigned int *intvalue)
  81. {
  82. char *value = NULL;
  83. if (!objdb->object_key_get (object_service_handle,
  84. key,
  85. strlen (key),
  86. (void *)&value,
  87. NULL)) {
  88. if (value) {
  89. *intvalue = atoi(value);
  90. }
  91. }
  92. }
  93. static struct logsys_config_struct {
  94. char subsys[6];
  95. unsigned int priority;
  96. unsigned int tags;
  97. } logsys_logger;
  98. int corosync_main_config_read_logging (
  99. struct objdb_iface_ver0 *objdb,
  100. const char **error_string,
  101. struct main_config *main_config)
  102. {
  103. hdb_handle_t object_service_handle;
  104. hdb_handle_t object_logger_subsys_handle;
  105. char *value;
  106. const char *error_reason = error_string_response;
  107. hdb_handle_t object_find_handle;
  108. hdb_handle_t object_find_logsys_handle;
  109. objdb->object_find_create (
  110. OBJECT_PARENT_HANDLE,
  111. "logging",
  112. strlen ("logging"),
  113. &object_find_handle);
  114. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  115. if (objdb->object_find_next (
  116. object_find_handle,
  117. &object_service_handle) == 0) {
  118. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  119. if (strcmp (value, "yes") == 0) {
  120. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  121. } else
  122. if (strcmp (value, "no") == 0) {
  123. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  124. }
  125. }
  126. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  127. if (strcmp (value, "yes") == 0) {
  128. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  129. } else
  130. if (strcmp (value, "no") == 0) {
  131. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  132. }
  133. }
  134. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  135. if (strcmp (value, "yes") == 0) {
  136. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  137. } else
  138. if (strcmp (value, "no") == 0) {
  139. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  140. }
  141. }
  142. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  143. char new_format_buffer[PATH_MAX];
  144. memset(&new_format_buffer, 0, sizeof(new_format_buffer));
  145. if (strcmp (value, "on") == 0) {
  146. snprintf(new_format_buffer, PATH_MAX-1, "%%t %s", logsys_format_get());
  147. logsys_format_set(new_format_buffer);
  148. } else
  149. if (strcmp (value, "off") == 0) {
  150. if (!strncmp("%t ", logsys_format_get(), 3)) {
  151. snprintf(new_format_buffer, PATH_MAX-1, "%s", logsys_format_get() + 3);
  152. logsys_format_set(new_format_buffer);
  153. }
  154. } else {
  155. goto parse_error;
  156. }
  157. }
  158. /* free old string on reload */
  159. if (main_config->logfile) {
  160. free(main_config->logfile);
  161. main_config->logfile = NULL;
  162. }
  163. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  164. main_config->logfile = strdup (value);
  165. }
  166. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  167. /* TODO
  168. if (strcmp (value, "on") == 0) {
  169. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  170. } else
  171. if (strcmp (value, "off") == 0) {
  172. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  173. } else {
  174. goto parse_error;
  175. }
  176. */
  177. }
  178. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  179. main_config->syslog_facility = logsys_facility_id_get(value);
  180. if (main_config->syslog_facility < 0) {
  181. error_reason = "unknown syslog facility specified";
  182. goto parse_error;
  183. }
  184. }
  185. logsys_config_facility_set ("corosync", main_config->syslog_facility);
  186. logsys_config_mode_set (main_config->logmode);
  187. logsys_config_file_set (error_string, main_config->logfile);
  188. objdb->object_find_create (
  189. object_service_handle,
  190. "logger_subsys",
  191. strlen ("logger_subsys"),
  192. &object_find_logsys_handle);
  193. while (objdb->object_find_next (
  194. object_find_logsys_handle,
  195. &object_logger_subsys_handle) == 0) {
  196. if (!objdb_get_string (objdb,
  197. object_logger_subsys_handle,
  198. "subsys", &value)) {
  199. strncpy (logsys_logger.subsys, value,
  200. sizeof (logsys_logger.subsys));
  201. }
  202. else {
  203. error_reason = "subsys required for logger directive";
  204. goto parse_error;
  205. }
  206. if (!objdb_get_string (objdb, object_logger_subsys_handle, "syslog_level", &value)) {
  207. logsys_logger.priority = logsys_priority_id_get(value);
  208. if (logsys_logger.priority < 0) {
  209. error_reason = "unknown syslog priority specified";
  210. goto parse_error;
  211. }
  212. }
  213. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  214. if (strcmp (value, "on") == 0) {
  215. logsys_logger.priority = LOG_LEVEL_DEBUG;
  216. } else
  217. if (strcmp (value, "off") == 0) {
  218. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  219. } else {
  220. goto parse_error;
  221. }
  222. }
  223. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  224. char *token = strtok (value, "|");
  225. while (token != NULL) {
  226. int val;
  227. val = logsys_tag_id_get(token);
  228. if (val < 0) {
  229. error_reason = "bad tags value";
  230. goto parse_error;
  231. }
  232. logsys_logger.tags |= val;
  233. token = strtok(NULL, "|");
  234. }
  235. }
  236. /*
  237. * set individual logger configurations
  238. */
  239. logsys_config_subsys_set (
  240. logsys_logger.subsys,
  241. logsys_logger.tags,
  242. logsys_logger.priority);
  243. }
  244. objdb->object_find_destroy (object_find_logsys_handle);
  245. }
  246. objdb->object_find_destroy (object_find_handle);
  247. return 0;
  248. parse_error:
  249. sprintf (error_string_response,
  250. "parse error in config: %s.\n",
  251. error_reason);
  252. *error_string = error_string_response;
  253. return (-1);
  254. }
  255. static int uid_determine (char *req_user)
  256. {
  257. struct passwd *passwd;
  258. int ais_uid = 0;
  259. passwd = getpwnam(req_user);
  260. if (passwd == 0) {
  261. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user);
  262. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  263. }
  264. ais_uid = passwd->pw_uid;
  265. endpwent ();
  266. return ais_uid;
  267. }
  268. static int gid_determine (char *req_group)
  269. {
  270. struct group *group;
  271. int ais_gid = 0;
  272. group = getgrnam (req_group);
  273. if (group == 0) {
  274. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group);
  275. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  276. }
  277. ais_gid = group->gr_gid;
  278. endgrent ();
  279. return ais_gid;
  280. }
  281. int corosync_main_config_read (
  282. struct objdb_iface_ver0 *objdb,
  283. const char **error_string,
  284. struct main_config *main_config)
  285. {
  286. hdb_handle_t object_service_handle;
  287. char *value;
  288. char *error_reason = error_string_response;
  289. hdb_handle_t object_find_handle;
  290. memset (main_config, 0, sizeof (struct main_config));
  291. corosync_main_config_read_logging(objdb, error_string, main_config);
  292. objdb->object_find_create (
  293. OBJECT_PARENT_HANDLE,
  294. "aisexec",
  295. strlen ("aisexec"),
  296. &object_find_handle);
  297. if (objdb->object_find_next (
  298. object_find_handle,
  299. &object_service_handle) == 0) {
  300. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  301. main_config->uid = uid_determine(value);
  302. } else
  303. main_config->uid = uid_determine("ais");
  304. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  305. main_config->gid = gid_determine(value);
  306. } else
  307. main_config->gid = gid_determine("ais");
  308. }
  309. objdb->object_find_destroy (object_find_handle);
  310. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  311. (main_config->logfile == NULL)) {
  312. error_reason = "logmode set to 'file' but no logfile specified";
  313. goto parse_error;
  314. }
  315. if (main_config->syslog_facility == 0)
  316. main_config->syslog_facility = LOG_DAEMON;
  317. add_logsys_config_notification(objdb, main_config);
  318. logsys_fork_completed ();
  319. return 0;
  320. parse_error:
  321. sprintf (error_string_response,
  322. "parse error in config: %s.\n",
  323. error_reason);
  324. *error_string = error_string_response;
  325. return (-1);
  326. }
  327. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  328. void *priv_data_pt)
  329. {
  330. struct main_config *main_config = priv_data_pt;
  331. const char *error_string;
  332. if (type == OBJDB_RELOAD_NOTIFY_END) {
  333. /*
  334. * Reload the logsys configuration
  335. */
  336. corosync_main_config_read_logging(global_objdb,
  337. &error_string,
  338. main_config);
  339. }
  340. }
  341. static void add_logsys_config_notification(
  342. struct objdb_iface_ver0 *objdb,
  343. struct main_config *main_config)
  344. {
  345. global_objdb = objdb;
  346. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  347. 1,
  348. NULL,
  349. NULL,
  350. NULL,
  351. main_objdb_reload_notify,
  352. main_config);
  353. }