root/pengine/main.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pe_ipc_accept
  2. pe_ipc_created
  3. pe_ipc_dispatch
  4. pe_ipc_closed
  5. pe_ipc_destroy
  6. main
  7. pengine_shutdown

   1 /*
   2  * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
   3  *
   4  * This program is free software; you can redistribute it and/or
   5  * modify it under the terms of the GNU General Public
   6  * License as published by the Free Software Foundation; either
   7  * version 2 of the License, or (at your option) any later version.
   8  *
   9  * This software is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12  * General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU General Public
  15  * License along with this library; if not, write to the Free Software
  16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17  */
  18 
  19 #include <crm_internal.h>
  20 
  21 #include <crm/crm.h>
  22 #include <stdio.h>
  23 #include <sys/types.h>
  24 #include <unistd.h>
  25 
  26 #include <stdlib.h>
  27 #include <errno.h>
  28 #include <fcntl.h>
  29 
  30 #include <crm/common/ipcs.h>
  31 #include <crm/common/mainloop.h>
  32 #include <crm/pengine/internal.h>
  33 #include <crm/msg_xml.h>
  34 
  35 #if HAVE_LIBXML2
  36 #  include <libxml/parser.h>
  37 #endif
  38 
  39 #define OPTARGS "hVc"
  40 
  41 GMainLoop *mainloop = NULL;
  42 qb_ipcs_service_t *ipcs = NULL;
  43 
  44 void pengine_shutdown(int nsig);
  45 
  46 static int32_t
  47 pe_ipc_accept(qb_ipcs_connection_t * c, uid_t uid, gid_t gid)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49     crm_trace("Connection %p", c);
  50     if (crm_client_new(c, uid, gid) == NULL) {
  51         return -EIO;
  52     }
  53     return 0;
  54 }
  55 
  56 static void
  57 pe_ipc_created(qb_ipcs_connection_t * c)
     /* [previous][next][first][last][top][bottom][index][help] */
  58 {
  59     crm_trace("Connection %p", c);
  60 }
  61 
  62 gboolean process_pe_message(xmlNode * msg, xmlNode * xml_data, crm_client_t * sender);
  63 
  64 static int32_t
  65 pe_ipc_dispatch(qb_ipcs_connection_t * qbc, void *data, size_t size)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67     uint32_t id = 0;
  68     uint32_t flags = 0;
  69     crm_client_t *c = crm_client_get(qbc);
  70     xmlNode *msg = crm_ipcs_recv(c, data, size, &id, &flags);
  71 
  72     crm_ipcs_send_ack(c, id, flags, "ack", __FUNCTION__, __LINE__);
  73     if (msg != NULL) {
  74         xmlNode *data_xml = get_message_xml(msg, F_CRM_DATA);
  75 
  76         process_pe_message(msg, data_xml, c);
  77         free_xml(msg);
  78     }
  79     return 0;
  80 }
  81 
  82 /* Error code means? */
  83 static int32_t
  84 pe_ipc_closed(qb_ipcs_connection_t * c)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86     crm_client_t *client = crm_client_get(c);
  87 
  88     if (client == NULL) {
  89         return 0;
  90     }
  91     crm_trace("Connection %p", c);
  92     crm_client_destroy(client);
  93     return 0;
  94 }
  95 
  96 static void
  97 pe_ipc_destroy(qb_ipcs_connection_t * c)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99     crm_trace("Connection %p", c);
 100     pe_ipc_closed(c);
 101 }
 102 
 103 struct qb_ipcs_service_handlers ipc_callbacks = {
 104     .connection_accept = pe_ipc_accept,
 105     .connection_created = pe_ipc_created,
 106     .msg_process = pe_ipc_dispatch,
 107     .connection_closed = pe_ipc_closed,
 108     .connection_destroyed = pe_ipc_destroy
 109 };
 110 
 111 /* *INDENT-OFF* */
 112 static struct crm_option long_options[] = {
 113     /* Top-level Options */
 114     {"help",    0, 0, '?', "\tThis text"},
 115     {"verbose", 0, 0, 'V', "\tIncrease debug output"},
 116 
 117     {0, 0, 0, 0}
 118 };
 119 /* *INDENT-ON* */
 120 
 121 int
 122 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 {
 124     int flag;
 125     int index = 0;
 126     int argerr = 0;
 127 
 128     crm_log_preinit(NULL, argc, argv);
 129     crm_set_options(NULL, "[options]",
 130                     long_options, "Daemon for calculating the cluster's response to events");
 131 
 132     mainloop_add_signal(SIGTERM, pengine_shutdown);
 133 
 134     while (1) {
 135         flag = crm_get_option(argc, argv, &index);
 136         if (flag == -1)
 137             break;
 138 
 139         switch (flag) {
 140             case 'V':
 141                 crm_bump_log_level(argc, argv);
 142                 break;
 143             case 'h':          /* Help message */
 144                 crm_help('?', EX_OK);
 145                 break;
 146             default:
 147                 ++argerr;
 148                 break;
 149         }
 150     }
 151 
 152     if (argc - optind == 1 && safe_str_eq("metadata", argv[optind])) {
 153         pe_metadata();
 154         return 0;
 155     }
 156 
 157     if (optind > argc) {
 158         ++argerr;
 159     }
 160 
 161     if (argerr) {
 162         crm_help('?', EX_USAGE);
 163     }
 164 
 165     crm_log_init(NULL, LOG_INFO, TRUE, FALSE, argc, argv, FALSE);
 166     if (crm_is_writable(PE_STATE_DIR, NULL, CRM_DAEMON_USER, CRM_DAEMON_GROUP, FALSE) == FALSE) {
 167         crm_err("Bad permissions on " PE_STATE_DIR ". Terminating");
 168         fprintf(stderr, "ERROR: Bad permissions on " PE_STATE_DIR ". See logs for details\n");
 169         fflush(stderr);
 170         return 100;
 171     }
 172 
 173     crm_debug("Init server comms");
 174     ipcs = mainloop_add_ipc_server(CRM_SYSTEM_PENGINE, QB_IPC_SHM, &ipc_callbacks);
 175     if (ipcs == NULL) {
 176         crm_err("Failed to create IPC server: shutting down and inhibiting respawn");
 177         crm_exit(DAEMON_RESPAWN_STOP);
 178     }
 179 
 180     /* Create the mainloop and run it... */
 181     crm_info("Starting %s", crm_system_name);
 182 
 183     mainloop = g_main_new(FALSE);
 184     g_main_run(mainloop);
 185 
 186     crm_info("Exiting %s", crm_system_name);
 187     return crm_exit(pcmk_ok);
 188 }
 189 
 190 void
 191 pengine_shutdown(int nsig)
     /* [previous][next][first][last][top][bottom][index][help] */
 192 {
 193     mainloop_del_ipc_server(ipcs);
 194     crm_exit(pcmk_ok);
 195 }

/* [previous][next][first][last][top][bottom][index][help] */