mainconfig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. int err = 0;
  177. objdb->object_find_create (
  178. OBJECT_PARENT_HANDLE,
  179. "logging",
  180. strlen ("logging"),
  181. &object_find_handle);
  182. main_config->logmode = LOG_MODE_THREADED | LOG_MODE_FORK;
  183. if (objdb->object_find_next (
  184. object_find_handle,
  185. &object_service_handle) == 0) {
  186. if (!objdb_get_string (objdb,object_service_handle, "to_file", &value)) {
  187. if (strcmp (value, "yes") == 0) {
  188. main_config->logmode |= LOG_MODE_OUTPUT_FILE;
  189. } else
  190. if (strcmp (value, "no") == 0) {
  191. main_config->logmode &= ~LOG_MODE_OUTPUT_FILE;
  192. }
  193. }
  194. if (!objdb_get_string (objdb,object_service_handle, "to_syslog", &value)) {
  195. if (strcmp (value, "yes") == 0) {
  196. main_config->logmode |= LOG_MODE_OUTPUT_SYSLOG;
  197. } else
  198. if (strcmp (value, "no") == 0) {
  199. main_config->logmode &= ~LOG_MODE_OUTPUT_SYSLOG;
  200. }
  201. }
  202. if (!objdb_get_string (objdb,object_service_handle, "to_stderr", &value)) {
  203. if (strcmp (value, "yes") == 0) {
  204. main_config->logmode |= LOG_MODE_OUTPUT_STDERR;
  205. } else
  206. if (strcmp (value, "no") == 0) {
  207. main_config->logmode &= ~LOG_MODE_OUTPUT_STDERR;
  208. }
  209. }
  210. if (!objdb_get_string (objdb,object_service_handle, "fileline", &value)) {
  211. if (strcmp (value, "on") == 0) {
  212. if (!insert_into_buffer(new_format_buffer,
  213. sizeof(new_format_buffer),
  214. " %f:%l", "s]")) {
  215. err = logsys_format_set(new_format_buffer);
  216. } else
  217. if (!insert_into_buffer(new_format_buffer,
  218. sizeof(new_format_buffer),
  219. "%f:%l", NULL)) {
  220. err = logsys_format_set(new_format_buffer);
  221. }
  222. } else
  223. if (strcmp (value, "off") == 0) {
  224. /* nothing to do here */
  225. } else {
  226. goto parse_error;
  227. }
  228. }
  229. if (!objdb_get_string (objdb,object_service_handle, "function_name", &value)) {
  230. if (strcmp (value, "on") == 0) {
  231. if (!insert_into_buffer(new_format_buffer,
  232. sizeof(new_format_buffer),
  233. "%n:", "f:")) {
  234. err = logsys_format_set(new_format_buffer);
  235. } else
  236. if (!insert_into_buffer(new_format_buffer,
  237. sizeof(new_format_buffer),
  238. " %n", "s]")) {
  239. err = logsys_format_set(new_format_buffer);
  240. }
  241. } else
  242. if (strcmp (value, "off") == 0) {
  243. /* nothing to do here */
  244. } else {
  245. goto parse_error;
  246. }
  247. }
  248. if (!objdb_get_string (objdb,object_service_handle, "timestamp", &value)) {
  249. if (strcmp (value, "on") == 0) {
  250. if(!insert_into_buffer(new_format_buffer,
  251. sizeof(new_format_buffer),
  252. "%t ", NULL)) {
  253. err = logsys_format_set(new_format_buffer);
  254. }
  255. } else
  256. if (strcmp (value, "off") == 0) {
  257. /* nothing to do here */
  258. } else {
  259. goto parse_error;
  260. }
  261. }
  262. if (err) {
  263. error_reason = "exhausted virtual memory";
  264. goto parse_error;
  265. }
  266. /* free old string on reload */
  267. if (main_config->logfile) {
  268. free(main_config->logfile);
  269. main_config->logfile = NULL;
  270. }
  271. if (!objdb_get_string (objdb,object_service_handle, "logfile", &value)) {
  272. main_config->logfile = strdup (value);
  273. }
  274. if (!objdb_get_string (objdb,object_service_handle, "syslog_facility", &value)) {
  275. main_config->syslog_facility = logsys_facility_id_get(value);
  276. if (main_config->syslog_facility < 0) {
  277. error_reason = "unknown syslog facility specified";
  278. goto parse_error;
  279. }
  280. }
  281. logsys_config_facility_set ("corosync", main_config->syslog_facility);
  282. logsys_config_mode_set (main_config->logmode);
  283. logsys_config_file_set (error_string, main_config->logfile);
  284. objdb->object_find_create (
  285. object_service_handle,
  286. "logger_subsys",
  287. strlen ("logger_subsys"),
  288. &object_find_logsys_handle);
  289. while (objdb->object_find_next (
  290. object_find_logsys_handle,
  291. &object_logger_subsys_handle) == 0) {
  292. if (!objdb_get_string (objdb,
  293. object_logger_subsys_handle,
  294. "subsys", &value)) {
  295. strncpy (logsys_logger.subsys, value,
  296. sizeof (logsys_logger.subsys));
  297. }
  298. else {
  299. error_reason = "subsys required for logger directive";
  300. goto parse_error;
  301. }
  302. if (!objdb_get_string (objdb, object_logger_subsys_handle, "syslog_level", &value)) {
  303. logsys_logger.priority = logsys_priority_id_get(value);
  304. if (logsys_logger.priority < 0) {
  305. error_reason = "unknown syslog priority specified";
  306. goto parse_error;
  307. }
  308. }
  309. if (!objdb_get_string (objdb, object_logger_subsys_handle, "debug", &value)) {
  310. if (strcmp (value, "on") == 0) {
  311. logsys_logger.priority = LOG_LEVEL_DEBUG;
  312. } else
  313. if (strcmp (value, "off") == 0) {
  314. logsys_logger.priority &= ~LOG_LEVEL_DEBUG;
  315. } else {
  316. goto parse_error;
  317. }
  318. }
  319. if (!objdb_get_string (objdb, object_logger_subsys_handle, "tags", &value)) {
  320. char *token = strtok (value, "|");
  321. while (token != NULL) {
  322. int val;
  323. val = logsys_tag_id_get(token);
  324. if (val < 0) {
  325. error_reason = "bad tags value";
  326. goto parse_error;
  327. }
  328. logsys_logger.tags |= val;
  329. token = strtok(NULL, "|");
  330. }
  331. }
  332. /*
  333. * set individual logger configurations
  334. */
  335. logsys_config_subsys_set (
  336. logsys_logger.subsys,
  337. logsys_logger.tags,
  338. logsys_logger.priority);
  339. }
  340. objdb->object_find_destroy (object_find_logsys_handle);
  341. }
  342. objdb->object_find_destroy (object_find_handle);
  343. return 0;
  344. parse_error:
  345. snprintf (error_string_response, sizeof(error_string_response),
  346. "parse error in config: %s.\n",
  347. error_reason);
  348. *error_string = error_string_response;
  349. return (-1);
  350. }
  351. static int uid_determine (const char *req_user)
  352. {
  353. struct passwd *passwd;
  354. int ais_uid = 0;
  355. passwd = getpwnam(req_user);
  356. if (passwd == 0) {
  357. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user);
  358. corosync_exit_error (AIS_DONE_UID_DETERMINE);
  359. }
  360. ais_uid = passwd->pw_uid;
  361. endpwent ();
  362. return ais_uid;
  363. }
  364. static int gid_determine (const char *req_group)
  365. {
  366. struct group *group;
  367. int ais_gid = 0;
  368. group = getgrnam (req_group);
  369. if (group == 0) {
  370. log_printf (LOG_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group);
  371. corosync_exit_error (AIS_DONE_GID_DETERMINE);
  372. }
  373. ais_gid = group->gr_gid;
  374. endgrent ();
  375. return ais_gid;
  376. }
  377. int corosync_main_config_read (
  378. struct objdb_iface_ver0 *objdb,
  379. const char **error_string,
  380. struct main_config *main_config)
  381. {
  382. hdb_handle_t object_service_handle;
  383. char *value;
  384. const char *error_reason = error_string_response;
  385. hdb_handle_t object_find_handle;
  386. memset (main_config, 0, sizeof (struct main_config));
  387. corosync_main_config_read_logging(objdb, error_string, main_config);
  388. objdb->object_find_create (
  389. OBJECT_PARENT_HANDLE,
  390. "aisexec",
  391. strlen ("aisexec"),
  392. &object_find_handle);
  393. main_config->uid = uid_determine("ais");
  394. main_config->gid = gid_determine("ais");
  395. if (objdb->object_find_next (
  396. object_find_handle,
  397. &object_service_handle) == 0) {
  398. if (!objdb_get_string (objdb,object_service_handle, "user", &value)) {
  399. main_config->uid = uid_determine(value);
  400. }
  401. if (!objdb_get_string (objdb,object_service_handle, "group", &value)) {
  402. main_config->gid = gid_determine(value);
  403. }
  404. }
  405. objdb->object_find_destroy (object_find_handle);
  406. if ((main_config->logmode & LOG_MODE_OUTPUT_FILE) &&
  407. (main_config->logfile == NULL)) {
  408. error_reason = "logmode set to 'file' but no logfile specified";
  409. goto parse_error;
  410. }
  411. if (main_config->syslog_facility == 0)
  412. main_config->syslog_facility = LOG_DAEMON;
  413. add_logsys_config_notification(objdb, main_config);
  414. logsys_fork_completed ();
  415. return 0;
  416. parse_error:
  417. snprintf (error_string_response, sizeof(error_string_response),
  418. "parse error in config: %s.\n",
  419. error_reason);
  420. *error_string = error_string_response;
  421. return (-1);
  422. }
  423. static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush,
  424. void *priv_data_pt)
  425. {
  426. struct main_config *main_config = priv_data_pt;
  427. const char *error_string;
  428. if (type == OBJDB_RELOAD_NOTIFY_END) {
  429. /*
  430. * Reload the logsys configuration
  431. */
  432. logsys_format_set(NULL);
  433. corosync_main_config_read_logging(global_objdb,
  434. &error_string,
  435. main_config);
  436. }
  437. }
  438. static void add_logsys_config_notification(
  439. struct objdb_iface_ver0 *objdb,
  440. struct main_config *main_config)
  441. {
  442. global_objdb = objdb;
  443. objdb->object_track_start(OBJECT_PARENT_HANDLE,
  444. 1,
  445. NULL,
  446. NULL,
  447. NULL,
  448. main_objdb_reload_notify,
  449. main_config);
  450. }