Documentation fixes.
[virt-hostinfo.git] / hostinfod / hostinfod.h
1 /* virt-hostinfo
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifndef HOSTINFOD_H
20 #define HOSTINFOD_H
21
22 #include <time.h>
23
24 #include <apr_general.h>
25 #include <apr_errno.h>
26 #include <apr_pools.h>
27 #include <apr_network_io.h>
28 #include <apr_poll.h>
29 #include <apr_hash.h>
30
31 #include <libvirt/libvirt.h>
32
33 enum guest_state {
34   guest_state_connecting,       /* Connecting to socket. */
35   guest_state_request,          /* Waiting or reading the request. */
36   guest_state_reply,            /* Sending the reply. */
37   guest_state_dead              /* Connection is dead. */
38 };
39
40 struct guest_description {
41   apr_pool_t *pool;             /* Pool for lifetime of guest connection. */
42
43   apr_pool_t *rpool;            /* Pool for lifetime of single req/resp. */
44
45   const char *name;             /* "driver-name" */
46   const char *sock_path;        /* Full path to socket. */
47   int sock;                     /* Real socket. */
48   apr_socket_t *aprsock;        /* APR socket. */
49   apr_pollfd_t pollfd;          /* APR poll descriptor. */
50   enum guest_state state;       /* State of the connection. */
51   int counter;                  /* Used in reread_socket_dir. */
52
53   unsigned request_max;         /* Max. length of request buffer. */
54   unsigned request_posn;        /* Position in request buffer. */
55   char *request;                /* Request buffer. */
56
57   unsigned reply_size;          /* Size of reply buffer. */
58   unsigned reply_posn;          /* Position in reply buffer. */
59   char *reply;                  /* Reply buffer. */
60
61   /* Increments every time guest does something bad, decremented once per min */
62   unsigned penalty;
63   struct timespec last_penalty_decr;
64
65   /* Hash records last time each command was run by this guest. */
66   apr_hash_t *lasttime;
67 };
68
69 enum arg_type {
70   arg_type_string,
71   arg_type_int,
72   arg_type_bool,
73 };
74
75 struct arg {
76   enum arg_type type;
77   union {
78     long i;                     /* For int, bool types. */
79     char *str;                  /* For string type. */
80   } u;
81 };
82
83 /* main.c */
84 extern const char *conf_file;
85 extern char *socket_dir;
86 extern char *guests_file;
87 extern char *libvirt_uri;
88 extern int libvirt_uri_set_on_cmdline;
89 extern int verbose;
90 extern int verbose_set_on_cmdline;
91 extern int foreground;
92 extern int foreground_set_on_cmdline;
93 extern int messages_to_stderr;
94 extern apr_pool_t *pool;        /* pool for global memory allocation */
95 extern void initialize (void);
96 extern struct timespec *diff_timespec (struct timespec *r, const struct timespec *a, const struct timespec *b); /* r = a - b */
97
98 /* error.c */
99 extern void init_syslog (void);
100 extern void error (const char *fs, ...)
101   __attribute__((format (printf,1,2)));
102 extern void perrorf (const char *fs, ...)
103   __attribute__((format (printf,1,2)));
104 extern void warning (const char *fs, ...)
105   __attribute__((format (printf,1,2)));
106 extern void pwarningf (const char *fs, ...)
107   __attribute__((format (printf,1,2)));
108 extern void debug (const char *fs, ...)
109   __attribute__((format (printf,1,2)));
110 extern void message (const char *fs, ...)
111   __attribute__((format (printf,1,2)));
112 extern void paprerror (apr_status_t r, const char *fs, ...)
113   __attribute__((format (printf,2,3)));
114
115 /* configuration.c */
116 extern void read_main_conf_file (void);
117 extern void check_guests_file (struct guest_description *hval, const char *cmd, double *interval, int *enabled);
118
119 /* monitor_sockets.c */
120 extern int sockets_inotify_fd;
121 extern void monitor_socket_dir (void);
122
123 /* commands.c */
124 extern apr_hash_t *commands;
125 extern void execute_command (const struct timespec *now, struct guest_description *hval, const char *command);
126 extern int get_args (apr_array_header_t *args, const char *argfs, ...);
127 extern void send_error (struct guest_description *hval, int code);
128 extern void send_reply (struct guest_description *hval, int code, const char *fs, ...)
129   __attribute__((format (printf,3,4)));
130
131 typedef void (*command_fn) (struct guest_description *hval, const char *cmd, apr_array_header_t *args);
132
133 #define REGISTER_COMMAND(name)                                          \
134   static void register_##name (void) __attribute__((constructor));      \
135                                                                         \
136   static void                                                           \
137   register_##name (void)                                                \
138   {                                                                     \
139     if (pool == NULL)                                                   \
140       initialize ();                                                    \
141                                                                         \
142     if (commands == NULL)                                               \
143       commands = apr_hash_make (pool);                                  \
144                                                                         \
145     /* The assignment below just causes a warning                       \
146      * if the callback has the wrong type.                              \
147      */                                                                 \
148     command_fn cb = name;                                               \
149     apr_hash_set (commands, #name, APR_HASH_KEY_STRING, cb);            \
150   }
151
152 /* virt.c */
153 extern void init_libvirt (void);
154 extern virConnectPtr conn;
155 extern virNodeInfo nodeinfo;
156
157 #endif /* HOSTINFOD_H */