mainconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <corosync/corotypes.h>
  46. #include <corosync/list.h>
  47. #include <corosync/totem/totem.h>
  48. #include <corosync/engine/logsys.h>
  49. #include "util.h"
  50. #include "mainconfig.h"
  51. #include "mempool.h"
  52. static char error_string_response[512];
  53. static struct objdb_iface_ver0 *global_objdb;
  54. static void add_logsys_config_notification(
  55. struct objdb_iface_ver0 *objdb,
  56. struct main_config *main_config);
  57. /* This just makes the code below a little neater */
  58. static inline int objdb_get_string (
  59. struct objdb_iface_ver0 *objdb,
  60. hdb_handle_t object_service_handle,
  61. char *key, char **value)
  62. {
  63. int res;
  64. *value = NULL;
  65. if ( !(res = objdb->object_key_get (object_service_handle,
  66. key,
  67. strlen (key),
  68. (void *)value,
  69. NULL))) {
  70. if (*value) {
  71. return 0;
  72. }
  73. }
  74. return -1;
  75. }
  76. static inline void objdb_get_int (
  77. struct objdb_iface_ver0 *objdb,
  78. hdb_handle_t object_service_handle,
  79. char *key, unsigned int *intvalue)
  80. {
  81. char *value = NULL;
  82. if (!objdb->object_key_get (object_service_handle,
  83. key,
  84. strlen (key),
  85. (void *)&value,
  86. NULL)) {
  87. if (value) {
  88. *intvalue = atoi(value);
  89. }
  90. }
  91. }
  92. static struct logsys_config_struct {
  93. char subsys[6];
  94. unsigned int priority;
  95. unsigned int tags;
  96. } logsys_logger;
  97. int corosync_main_config_read_logging (
  98. struct objdb_iface_ver0 *objdb,
  99. char **error_string,
  100. struct main_config *main_config)
  101. {
  102. hdb_handle_t object_service_handle;
  103. hdb_handle_t object_logger_subsys_handle;
  104. char *value;
  105. char *error_reason = error_string_response;
  106. hdb_handle_t object_find_handle;
  107. hdb_handle_t object_find_logsys_handle;
  108. objdb->object_find_create (
  109. OBJECT_PARENT_HANDLE,
  110. "logging",
  111. strlen ("logging"),
  112. &object_find_handle);
  113. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  114. if (objdb->object_find_next (
  115. object_find_handle,
  116. &object_service_handle) == 0) {
  117. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  118. if (strcmp (value, "yes") == 0) {
  119. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  120. } else
  121. if (strcmp (value, "no") == 0) {
  122. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  123. }
  124. }
  125. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  126. if (strcmp (value, "yes") == 0) {
  127. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  128. } else
  129. if (strcmp (value, "no") == 0) {
  130. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  131. }
  132. }
  133. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  134. if (strcmp (value, "yes") == 0) {
  135. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  136. } else
  137. if (strcmp (value, "no") == 0) {
  138. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  139. }
  140. }
  141. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  142. if (strcmp (value, "on") == 0) {
  143. logsys_format_set("%t [%6s] %b");
  144. } else
  145. if (strcmp (value, "off") == 0) {
  146. logsys_format_set(NULL);
  147. } else {
  148. goto parse_error;
  149. }
  150. }
  151. /* free old string on reload */
  152. if (main_config->logfile) {
  153. free(main_config->logfile);
  154. main_config->logfile = NULL;
  155. }
  156. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  157. main_config->logfile = strdup (value);
  158. }
  159. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  160. /* TODO
  161. if (strcmp (value, "on") == 0) {
  162. main_config->logmode |= LOG_MODE_DISPLAY_FILELINE;
  163. } else
  164. if (strcmp (value, "off") == 0) {
  165. main_config->logmode &= ~LOG_MODE_DISPLAY_FILELINE;
  166. } else {
  167. goto parse_error;
  168. }
  169. */
  170. }
  171. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  172. main_config->syslog_facility = logsys_facility_id_get(value);
  173. if (main_config->syslog_facility < 0) {
  174. error_reason = "unknown syslog facility specified";
  175. goto parse_error;
  176. }
  177. }
  178. logsys_config_facility_set ("corosync", main_config->syslog_facility);
  179. logsys_config_mode_set (main_config->logmode);
  180. logsys_config_file_set (error_string, main_config->logfile);
  181. objdb->object_find_create (
  182. object_service_handle,
  183. "logger_subsys",
  184. strlen ("logger_subsys"),
  185. &object_find_logsys_handle);
  186. while (objdb->object_find_next (
  187. object_find_logsys_handle,
  188. &object_logger_subsys_handle) == 0) {
  189. if (!objdb_get_string (objdb,
  190. object_logger_subsys_handle,
  191. "subsys", &value)) {
  192. strncpy (logsys_logger.subsys, value,
  193. sizeof (logsys_logger.subsys));
  194. }
  195. else {
  196. error_reason = "subsys required for logger directive";
  197. goto parse_error;
  198. }
  199. if (!objdb_get_string (objdb, object_logger_subsys_handle, "syslog_level", &value)) {
  200. logsys_logger.priority = logsys_priority_id_get(value);
  201. if (logsys_logger.priority < 0) {
  202. error_reason = "unknown syslog priority specified";
  203. goto parse_error;
  204. }
  205. }
  206. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  207. if (strcmp (value, "on") == 0) {
  208. logsys_logger.priority = LOG_LEVEL_DEBUG;
  209. } else
  210. if (strcmp (value, "off") == 0) {
  211. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  212. } else {
  213. goto parse_error;
  214. }
  215. }
  216. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  217. char *token = strtok (value, "|");
  218. while (token != NULL) {
  219. int val;
  220. val = logsys_tag_id_get(token);
  221. if (val < 0) {
  222. error_reason = "bad tags value";
  223. goto parse_error;
  224. }
  225. logsys_logger.tags |= val;
  226. token = strtok(NULL, "|");
  227. }
  228. }
  229. /*
  230. * set individual logger configurations
  231. */
  232. logsys_config_subsys_set (
  233. logsys_logger.subsys,
  234. logsys_logger.tags,
  235. logsys_logger.priority);
  236. }
  237. objdb->object_find_destroy (object_find_logsys_handle);
  238. }
  239. objdb->object_find_destroy (object_find_handle);
  240. return 0;
  241. parse_error:
  242. sprintf (error_string_response,
  243. "parse error in config: %s.\n",
  244. error_reason);
  245. *error_string = error_string_response;
  246. return (-1);
  247. }
  248. static int uid_determine (char *req_user)
  249. {
  250. struct passwd *passwd;
  251. int ais_uid = 0;
  252. passwd = getpwnam(req_user);
  253. if (passwd == 0) {
  254. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user);
  255. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  256. }
  257. ais_uid = passwd->pw_uid;
  258. endpwent ();
  259. return ais_uid;
  260. }
  261. static int gid_determine (char *req_group)
  262. {
  263. struct group *group;
  264. int ais_gid = 0;
  265. group = getgrnam (req_group);
  266. if (group == 0) {
  267. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group);
  268. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  269. }
  270. ais_gid = group->gr_gid;
  271. endgrent ();
  272. return ais_gid;
  273. }
  274. int corosync_main_config_read (
  275. struct objdb_iface_ver0 *objdb,
  276. char **error_string,
  277. struct main_config *main_config)
  278. {
  279. hdb_handle_t object_service_handle;
  280. char *value;
  281. char *error_reason = error_string_response;
  282. hdb_handle_t object_find_handle;
  283. memset (main_config, 0, sizeof (struct main_config));
  284. corosync_main_config_read_logging(objdb, error_string, main_config);
  285. objdb->object_find_create (
  286. OBJECT_PARENT_HANDLE,
  287. "aisexec",
  288. strlen ("aisexec"),
  289. &object_find_handle);
  290. if (objdb->object_find_next (
  291. object_find_handle,
  292. &object_service_handle) == 0) {
  293. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  294. main_config->uid = uid_determine(value);
  295. } else
  296. main_config->uid = uid_determine("ais");
  297. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  298. main_config->gid = gid_determine(value);
  299. } else
  300. main_config->gid = gid_determine("ais");
  301. }
  302. objdb->object_find_destroy (object_find_handle);
  303. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  304. (main_config->logfile == NULL)) {
  305. error_reason = "logmode set to 'file' but no logfile specified";
  306. goto parse_error;
  307. }
  308. if (main_config->syslog_facility == 0)
  309. main_config->syslog_facility = LOG_DAEMON;
  310. add_logsys_config_notification(objdb, main_config);
  311. logsys_fork_completed ();
  312. return 0;
  313. parse_error:
  314. sprintf (error_string_response,
  315. "parse error in config: %s.\n",
  316. error_reason);
  317. *error_string = error_string_response;
  318. return (-1);
  319. }
  320. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  321. void *priv_data_pt)
  322. {
  323. struct main_config *main_config = priv_data_pt;
  324. char *error_string;
  325. if (type == OBJDB_RELOAD_NOTIFY_END) {
  326. /*
  327. * Reload the logsys configuration
  328. */
  329. corosync_main_config_read_logging(global_objdb,
  330. &error_string,
  331. main_config);
  332. }
  333. }
  334. static void add_logsys_config_notification(
  335. struct objdb_iface_ver0 *objdb,
  336. struct main_config *main_config)
  337. {
  338. global_objdb = objdb;
  339. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  340. 1,
  341. NULL,
  342. NULL,
  343. NULL,
  344. main_objdb_reload_notify,
  345. main_config);
  346. }