mainconfig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. /**
  95. * insert_into_buffer
  96. * @target_buffer: a buffer where to write results
  97. * @bufferlen: tell us the size of the buffer to avoid overflows
  98. * @entry: entry that needs to be added to the buffer
  99. * @after: can either be NULL or set to a string.
  100. * if NULL, @entry is prependend to logsys_format_get buffer.
  101. * if set, @entry is added immediately after @after.
  102. *
  103. * Since the function is specific to logsys_format_get handling, it is implicit
  104. * that source is logsys_format_get();
  105. *
  106. * In case of failure, target_buffer could be left dirty. So don't trust
  107. * any data leftover in it.
  108. *
  109. * Searching for "after" assumes that there is only entry of "after"
  110. * in the source. Afterall we control the string here and for logging format
  111. * it makes little to no sense to have duplicate format entries.
  112. *
  113. * Returns: 0 on success, -1 on failure
  114. **/
  115. static int insert_into_buffer(
  116. char *target_buffer,
  117. size_t bufferlen,
  118. const char *entry,
  119. const char *after)
  120. {
  121. const char *current_format = NULL;
  122. current_format = logsys_format_get();
  123. /* if the entry is already in the format we don't add it again */
  124. if (strstr(current_format, entry) != NULL) {
  125. return -1;
  126. }
  127. /* if there is no "after", simply prepend the requested entry
  128. * otherwise go for beautiful string manipulation.... </sarcasm> */
  129. if (!after) {
  130. if (snprintf(target_buffer, bufferlen - 1, "%s%s",
  131. entry,
  132. current_format) >= bufferlen - 1) {
  133. return -1;
  134. }
  135. } else {
  136. const char *afterpos;
  137. size_t afterlen;
  138. size_t templen;
  139. /* check if after is contained in the format
  140. * and afterlen has a meaning or return an error */
  141. afterpos = strstr(current_format, after);
  142. afterlen = strlen(after);
  143. if ((!afterpos) || (!afterlen)) {
  144. return -1;
  145. }
  146. templen = afterpos - current_format + afterlen;
  147. if (snprintf(target_buffer, templen + 1, "%s", current_format)
  148. >= bufferlen - 1) {
  149. return -1;
  150. }
  151. if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
  152. "%s%s", entry, current_format + templen)
  153. >= bufferlen - ( templen + 1 )) {
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }
  159. static struct logsys_config_struct {
  160. char subsys[6];
  161. unsigned int priority;
  162. unsigned int tags;
  163. } logsys_logger;
  164. int corosync_main_config_read_logging (
  165. struct objdb_iface_ver0 *objdb,
  166. const char **error_string,
  167. struct main_config *main_config)
  168. {
  169. hdb_handle_t object_service_handle;
  170. hdb_handle_t object_logger_subsys_handle;
  171. char *value;
  172. const char *error_reason = error_string_response;
  173. hdb_handle_t object_find_handle;
  174. hdb_handle_t object_find_logsys_handle;
  175. char new_format_buffer[PATH_MAX];
  176. objdb->object_find_create (
  177. OBJECT_PARENT_HANDLE,
  178. "logging",
  179. strlen ("logging"),
  180. &object_find_handle);
  181. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  182. if (objdb->object_find_next (
  183. object_find_handle,
  184. &object_service_handle) == 0) {
  185. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  186. if (strcmp (value, "yes") == 0) {
  187. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  188. } else
  189. if (strcmp (value, "no") == 0) {
  190. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  191. }
  192. }
  193. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  194. if (strcmp (value, "yes") == 0) {
  195. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  196. } else
  197. if (strcmp (value, "no") == 0) {
  198. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  199. }
  200. }
  201. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  202. if (strcmp (value, "yes") == 0) {
  203. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  204. } else
  205. if (strcmp (value, "no") == 0) {
  206. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  207. }
  208. }
  209. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  210. if (strcmp (value, "on") == 0) {
  211. if (!insert_into_buffer(new_format_buffer,
  212. sizeof(new_format_buffer),
  213. " %f:%l", "s]")) {
  214. logsys_format_set(new_format_buffer);
  215. } else
  216. if (!insert_into_buffer(new_format_buffer,
  217. sizeof(new_format_buffer),
  218. "%f:%l", NULL)) {
  219. logsys_format_set(new_format_buffer);
  220. }
  221. } else
  222. if (strcmp (value, "off") == 0) {
  223. /* nothing to do here */
  224. } else {
  225. goto parse_error;
  226. }
  227. }
  228. if (!objdb_get_string (objdb,object_service_handle, "function_name", &value)) {
  229. if (strcmp (value, "on") == 0) {
  230. if (!insert_into_buffer(new_format_buffer,
  231. sizeof(new_format_buffer),
  232. "%n:", "f:")) {
  233. logsys_format_set(new_format_buffer);
  234. } else
  235. if (!insert_into_buffer(new_format_buffer,
  236. sizeof(new_format_buffer),
  237. " %n", "s]")) {
  238. logsys_format_set(new_format_buffer);
  239. }
  240. } else
  241. if (strcmp (value, "off") == 0) {
  242. /* nothing to do here */
  243. } else {
  244. goto parse_error;
  245. }
  246. }
  247. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  248. if (strcmp (value, "on") == 0) {
  249. if(!insert_into_buffer(new_format_buffer,
  250. sizeof(new_format_buffer),
  251. "%t ", NULL)) {
  252. logsys_format_set(new_format_buffer);
  253. }
  254. } else
  255. if (strcmp (value, "off") == 0) {
  256. /* nothing to do here */
  257. } else {
  258. goto parse_error;
  259. }
  260. }
  261. /* free old string on reload */
  262. if (main_config->logfile) {
  263. free(main_config->logfile);
  264. main_config->logfile = NULL;
  265. }
  266. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  267. main_config->logfile = strdup (value);
  268. }
  269. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  270. main_config->syslog_facility = logsys_facility_id_get(value);
  271. if (main_config->syslog_facility < 0) {
  272. error_reason = "unknown syslog facility specified";
  273. goto parse_error;
  274. }
  275. }
  276. logsys_config_facility_set ("corosync", main_config->syslog_facility);
  277. logsys_config_mode_set (main_config->logmode);
  278. logsys_config_file_set (error_string, main_config->logfile);
  279. objdb->object_find_create (
  280. object_service_handle,
  281. "logger_subsys",
  282. strlen ("logger_subsys"),
  283. &object_find_logsys_handle);
  284. while (objdb->object_find_next (
  285. object_find_logsys_handle,
  286. &object_logger_subsys_handle) == 0) {
  287. if (!objdb_get_string (objdb,
  288. object_logger_subsys_handle,
  289. "subsys", &value)) {
  290. strncpy (logsys_logger.subsys, value,
  291. sizeof (logsys_logger.subsys));
  292. }
  293. else {
  294. error_reason = "subsys required for logger directive";
  295. goto parse_error;
  296. }
  297. if (!objdb_get_string (objdb, object_logger_subsys_handle, "syslog_level", &value)) {
  298. logsys_logger.priority = logsys_priority_id_get(value);
  299. if (logsys_logger.priority < 0) {
  300. error_reason = "unknown syslog priority specified";
  301. goto parse_error;
  302. }
  303. }
  304. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  305. if (strcmp (value, "on") == 0) {
  306. logsys_logger.priority = LOG_LEVEL_DEBUG;
  307. } else
  308. if (strcmp (value, "off") == 0) {
  309. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  310. } else {
  311. goto parse_error;
  312. }
  313. }
  314. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  315. char *token = strtok (value, "|");
  316. while (token != NULL) {
  317. int val;
  318. val = logsys_tag_id_get(token);
  319. if (val < 0) {
  320. error_reason = "bad tags value";
  321. goto parse_error;
  322. }
  323. logsys_logger.tags |= val;
  324. token = strtok(NULL, "|");
  325. }
  326. }
  327. /*
  328. * set individual logger configurations
  329. */
  330. logsys_config_subsys_set (
  331. logsys_logger.subsys,
  332. logsys_logger.tags,
  333. logsys_logger.priority);
  334. }
  335. objdb->object_find_destroy (object_find_logsys_handle);
  336. }
  337. objdb->object_find_destroy (object_find_handle);
  338. return 0;
  339. parse_error:
  340. sprintf (error_string_response,
  341. "parse error in config: %s.\n",
  342. error_reason);
  343. *error_string = error_string_response;
  344. return (-1);
  345. }
  346. static int uid_determine (const char *req_user)
  347. {
  348. struct passwd *passwd;
  349. int ais_uid = 0;
  350. passwd = getpwnam(req_user);
  351. if (passwd == 0) {
  352. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user);
  353. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  354. }
  355. ais_uid = passwd->pw_uid;
  356. endpwent ();
  357. return ais_uid;
  358. }
  359. static int gid_determine (const char *req_group)
  360. {
  361. struct group *group;
  362. int ais_gid = 0;
  363. group = getgrnam (req_group);
  364. if (group == 0) {
  365. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group);
  366. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  367. }
  368. ais_gid = group->gr_gid;
  369. endgrent ();
  370. return ais_gid;
  371. }
  372. int corosync_main_config_read (
  373. struct objdb_iface_ver0 *objdb,
  374. const char **error_string,
  375. struct main_config *main_config)
  376. {
  377. hdb_handle_t object_service_handle;
  378. char *value;
  379. const char *error_reason = error_string_response;
  380. hdb_handle_t object_find_handle;
  381. memset (main_config, 0, sizeof (struct main_config));
  382. corosync_main_config_read_logging(objdb, error_string, main_config);
  383. objdb->object_find_create (
  384. OBJECT_PARENT_HANDLE,
  385. "aisexec",
  386. strlen ("aisexec"),
  387. &object_find_handle);
  388. main_config->uid = uid_determine("ais");
  389. main_config->gid = gid_determine("ais");
  390. if (objdb->object_find_next (
  391. object_find_handle,
  392. &object_service_handle) == 0) {
  393. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  394. main_config->uid = uid_determine(value);
  395. }
  396. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  397. main_config->gid = gid_determine(value);
  398. }
  399. }
  400. objdb->object_find_destroy (object_find_handle);
  401. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  402. (main_config->logfile == NULL)) {
  403. error_reason = "logmode set to 'file' but no logfile specified";
  404. goto parse_error;
  405. }
  406. if (main_config->syslog_facility == 0)
  407. main_config->syslog_facility = LOG_DAEMON;
  408. add_logsys_config_notification(objdb, main_config);
  409. logsys_fork_completed ();
  410. return 0;
  411. parse_error:
  412. sprintf (error_string_response,
  413. "parse error in config: %s.\n",
  414. error_reason);
  415. *error_string = error_string_response;
  416. return (-1);
  417. }
  418. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  419. void *priv_data_pt)
  420. {
  421. struct main_config *main_config = priv_data_pt;
  422. const char *error_string;
  423. if (type == OBJDB_RELOAD_NOTIFY_END) {
  424. /*
  425. * Reload the logsys configuration
  426. */
  427. logsys_format_set(NULL);
  428. corosync_main_config_read_logging(global_objdb,
  429. &error_string,
  430. main_config);
  431. }
  432. }
  433. static void add_logsys_config_notification(
  434. struct objdb_iface_ver0 *objdb,
  435. struct main_config *main_config)
  436. {
  437. global_objdb = objdb;
  438. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  439. 1,
  440. NULL,
  441. NULL,
  442. NULL,
  443. main_objdb_reload_notify,
  444. main_config);
  445. }