mainconfig.c 11 KB

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