root/include/crm/common/mainloop.h

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

INCLUDED FROM


   1 /*
   2  * Copyright 2009-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #ifndef CRM_COMMON_MAINLOOP__H
  11 #  define CRM_COMMON_MAINLOOP__H
  12 
  13 #ifdef __cplusplus
  14 extern "C" {
  15 #endif
  16 
  17 /**
  18  * \file
  19  * \brief Wrappers for and extensions to glib mainloop
  20  * \ingroup core
  21  */
  22 
  23 #  include <signal.h> // sighandler_t
  24 #  include <glib.h>
  25 
  26 enum mainloop_child_flags {
  27     /* don't kill pid group on timeout, only kill the pid */
  28     mainloop_leave_pid_group = 0x01,
  29 };
  30 
  31 typedef struct trigger_s crm_trigger_t;
  32 typedef struct mainloop_io_s mainloop_io_t;
  33 typedef struct mainloop_child_s mainloop_child_t;
  34 typedef struct mainloop_timer_s mainloop_timer_t;
  35 
  36 void mainloop_cleanup(void);
  37 
  38 crm_trigger_t *mainloop_add_trigger(int priority, int (*dispatch) (gpointer user_data),
  39                                     gpointer userdata);
  40 
  41 void mainloop_set_trigger(crm_trigger_t * source);
  42 
  43 void mainloop_trigger_complete(crm_trigger_t * trig);
  44 
  45 gboolean mainloop_destroy_trigger(crm_trigger_t * source);
  46 
  47 #  ifndef HAVE_SIGHANDLER_T
  48 typedef void (*sighandler_t)(int);
  49 #  endif
  50 
  51 sighandler_t crm_signal_handler(int sig, sighandler_t dispatch);
  52 
  53 gboolean mainloop_add_signal(int sig, void (*dispatch) (int sig));
  54 
  55 gboolean mainloop_destroy_signal(int sig);
  56 
  57 bool mainloop_timer_running(mainloop_timer_t *t);
  58 
  59 void mainloop_timer_start(mainloop_timer_t *t);
  60 
  61 void mainloop_timer_stop(mainloop_timer_t *t);
  62 
  63 guint mainloop_timer_set_period(mainloop_timer_t *t, guint period_ms);
  64 
  65 mainloop_timer_t *mainloop_timer_add(const char *name, guint period_ms, bool repeat, GSourceFunc cb, void *userdata);
  66 
  67 void mainloop_timer_del(mainloop_timer_t *t);
  68 
  69 
  70 #  include <crm/common/ipc.h>
  71 #  include <qb/qbipcs.h>
  72 
  73 struct ipc_client_callbacks {
  74     /*!
  75      * \brief Dispatch function for an IPC connection used as mainloop source
  76      *
  77      * \param[in] buffer    Message read from IPC connection
  78      * \param[in] length    Number of bytes in \p buffer
  79      * \param[in] userdata  User data passed when creating mainloop source
  80      *
  81      * \return Negative value to remove source, anything else to keep it
  82      */
  83     int (*dispatch) (const char *buffer, ssize_t length, gpointer userdata);
  84 
  85     /*!
  86      * \brief Destroy function for mainloop IPC connection client data
  87      *
  88      * \param[in] userdata  User data passed when creating mainloop source
  89      */
  90     void (*destroy) (gpointer userdata);
  91 };
  92 
  93 qb_ipcs_service_t *mainloop_add_ipc_server(const char *name, enum qb_ipc_type type,
  94                                            struct qb_ipcs_service_handlers *callbacks);
  95 
  96 /*!
  97  * \brief Start server-side API end-point, hooked into the internal event loop
  98  *
  99  * \param[in] name    name of the IPC end-point ("address" for the client)
 100  * \param[in] type    selects libqb's IPC back-end (or use #QB_IPC_NATIVE)
 101  * \param[in] callbacks  defines libqb's IPC service-level handlers
 102  * \param[in] priority  priority relative to other events handled in the
 103  *                      abstract handling loop, use #QB_LOOP_MED when unsure
 104  *
 105  * \return libqb's opaque handle to the created service abstraction
 106  *
 107  * \note For portability concerns, do not use this function if you keep
 108  *       \p priority as #QB_LOOP_MED, stick with #mainloop_add_ipc_server
 109  *       (with exactly such semantics) instead (once you link with this new
 110  *       symbol employed, you can't downgrade the library freely anymore).
 111  *
 112  * \note The intended effect will only get fully reflected when run-time
 113  *       linked to patched libqb: https://github.com/ClusterLabs/libqb/pull/352
 114  */
 115 qb_ipcs_service_t *mainloop_add_ipc_server_with_prio(const char *name,
 116                                                     enum qb_ipc_type type,
 117                                                     struct qb_ipcs_service_handlers *callbacks,
 118                                                     enum qb_loop_priority prio);
 119 
 120 void mainloop_del_ipc_server(qb_ipcs_service_t * server);
 121 
 122 mainloop_io_t *mainloop_add_ipc_client(const char *name, int priority, size_t max_size,
 123                                        void *userdata, struct ipc_client_callbacks *callbacks);
 124 
 125 void mainloop_del_ipc_client(mainloop_io_t * client);
 126 
 127 crm_ipc_t *mainloop_get_ipc_client(mainloop_io_t * client);
 128 
 129 struct mainloop_fd_callbacks {
 130     /*!
 131      * \brief Dispatch function for mainloop file descriptor with data ready
 132      *
 133      * \param[in] userdata  User data passed when creating mainloop source
 134      *
 135      * \return Negative value to remove source, anything else to keep it
 136      */
 137     int (*dispatch) (gpointer userdata);
 138 
 139     /*!
 140      * \brief Destroy function for mainloop file descriptor client data
 141      *
 142      * \param[in] userdata  User data passed when creating mainloop source
 143      */
 144     void (*destroy) (gpointer userdata);
 145 };
 146 
 147 mainloop_io_t *mainloop_add_fd(const char *name, int priority, int fd, void *userdata,
 148                                struct mainloop_fd_callbacks *callbacks);
 149 
 150 void mainloop_del_fd(mainloop_io_t * client);
 151 
 152 /*
 153  * Create a new tracked process
 154  * To track a process group, use -pid
 155  */
 156 void mainloop_child_add(pid_t pid,
 157                         int timeout,
 158                         const char *desc,
 159                         void *userdata,
 160                         void (*callback) (mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode));
 161 
 162 void mainloop_child_add_with_flags(pid_t pid,
 163                         int timeout,
 164                         const char *desc,
 165                         void *userdata,
 166                         enum mainloop_child_flags,
 167                         void (*callback) (mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode));
 168 
 169 void *mainloop_child_userdata(mainloop_child_t * child);
 170 int mainloop_child_timeout(mainloop_child_t * child);
 171 const char *mainloop_child_name(mainloop_child_t * child);
 172 
 173 pid_t mainloop_child_pid(mainloop_child_t * child);
 174 void mainloop_clear_child_userdata(mainloop_child_t * child);
 175 gboolean mainloop_child_kill(pid_t pid);
 176 
 177 void pcmk_quit_main_loop(GMainLoop *mloop, unsigned int n);
 178 void pcmk_drain_main_loop(GMainLoop *mloop, guint timer_ms,
 179                           bool (*check)(guint));
 180 
 181 #  define G_PRIORITY_MEDIUM (G_PRIORITY_HIGH/2)
 182 
 183 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
 184 #include <crm/common/mainloop_compat.h>
 185 #endif
 186 
 187 #ifdef __cplusplus
 188 }
 189 #endif
 190 
 191 #endif

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