mainconfig.c 11 KB

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