libvirtd.h 7.7 KB
Newer Older
D
Daniel P. Berrange 已提交
1
/*
D
Daniel P. Berrange 已提交
2
 * libvirtd.h: daemon data structure definitions
D
Daniel P. Berrange 已提交
3
 *
4
 * Copyright (C) 2006-2009 Red Hat, Inc.
D
Daniel P. Berrange 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */


#ifndef QEMUD_INTERNAL_H__
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# define QEMUD_INTERNAL_H__

# include <config.h>

# include <gnutls/gnutls.h>
# include <gnutls/x509.h>
# include "gnutls_1_0_compat.h"
# if HAVE_SASL
#  include <sasl/sasl.h>
# endif

# if HAVE_POLKIT0
#  include <dbus/dbus.h>
# endif

# ifdef HAVE_SYS_SYSLIMITS_H
#  include <sys/syslimits.h>
# endif

# include <rpc/types.h>
# include <rpc/xdr.h>
# include "remote_protocol.h"
# include "logging.h"
# include "threads.h"

# ifdef __GNUC__
#  ifdef HAVE_ANSIDECL_H
#   include <ansidecl.h>
#  endif

#  ifndef __GNUC_PREREQ
#   if defined __GNUC__ && defined __GNUC_MINOR__
#    define __GNUC_PREREQ(maj, min)                                        \
59
    ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
60 61 62 63
#   else
#    define __GNUC_PREREQ(maj,min) 0
#   endif
#  endif
64 65 66 67 68 69

/**
 * ATTRIBUTE_UNUSED:
 *
 * Macro to flag conciously unused parameters to functions
 */
70 71 72
#  ifndef ATTRIBUTE_UNUSED
#   define ATTRIBUTE_UNUSED __attribute__((__unused__))
#  endif
73 74 75 76 77 78 79 80 81 82 83

/**
 * ATTRIBUTE_FMT_PRINTF
 *
 * Macro used to check printf like functions, if compiling
 * with gcc.
 *
 * We use gnulib which guarentees we always have GNU style
 * printf format specifiers even on broken Win32 platforms
 * hence we have to force 'gnu_printf' for new GCC
 */
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#  ifndef ATTRIBUTE_FMT_PRINTF
#   if __GNUC_PREREQ (4, 4)
#    define ATTRIBUTE_FMT_PRINTF(fmtpos,argpos) __attribute__((__format__ (gnu_printf, fmtpos,argpos)))
#   else
#    define ATTRIBUTE_FMT_PRINTF(fmtpos,argpos) __attribute__((__format__ (printf, fmtpos,argpos)))
#   endif
#  endif

#  ifndef ATTRIBUTE_RETURN_CHECK
#   if __GNUC_PREREQ (3, 4)
#    define ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
#   else
#    define ATTRIBUTE_RETURN_CHECK
#   endif
#  endif

# else
#  ifndef ATTRIBUTE_UNUSED
#   define ATTRIBUTE_UNUSED
#  endif
#  ifndef ATTRIBUTE_FMT_PRINTF
#   define ATTRIBUTE_FMT_PRINTF(...)
#  endif
#  ifndef ATTRIBUTE_RETURN_CHECK
#   define ATTRIBUTE_RETURN_CHECK
#  endif
# endif

# define qemudDebug DEBUG
113

114 115 116 117 118 119 120 121 122 123 124
/* Whether we're passing reads & writes through a sasl SSF */
enum qemud_sasl_ssf {
    QEMUD_SASL_SSF_NONE = 0,
    QEMUD_SASL_SSF_READ = 1,
    QEMUD_SASL_SSF_WRITE = 2,
};

enum qemud_sock_type {
    QEMUD_SOCK_TYPE_UNIX = 0,
    QEMUD_SOCK_TYPE_TCP = 1,
    QEMUD_SOCK_TYPE_TLS = 2,
125 126
};

127 128 129 130 131
struct qemud_client_message {
    char buffer [REMOTE_MESSAGE_MAX + REMOTE_MESSAGE_HEADER_XDR_LEN];
    unsigned int bufferLength;
    unsigned int bufferOffset;

132
    unsigned int async : 1;
133
    unsigned int streamTX : 1;
134

135 136
    remote_message_header hdr;

137 138 139
    struct qemud_client_message *next;
};

140 141
struct qemud_client;

142 143 144
/* Allow for filtering of incoming messages to a custom
 * dispatch processing queue, instead of client->dx.
 */
145 146
typedef int (*qemud_client_filter_func)(struct qemud_client *client,
                                        struct qemud_client_message *msg, void *opaque);
147 148 149 150 151 152 153
struct qemud_client_filter {
    qemud_client_filter_func query;
    void *opaque;

    struct qemud_client_filter *next;
};

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
struct qemud_client_stream {
    virStreamPtr st;
    int procedure;
    int serial;

    unsigned int recvEOF : 1;
    unsigned int closed : 1;

    struct qemud_client_filter filter;

    struct qemud_client_message *rx;
    int tx;

    struct qemud_client_stream *next;
};

D
Daniel P. Berrange 已提交
170 171
/* Stores the per-client connection state */
struct qemud_client {
172
    virMutex lock;
173

174 175
    int magic;

D
Daniel P. Berrange 已提交
176
    int fd;
177
    int watch;
178 179 180
    unsigned int readonly :1;
    unsigned int closing :1;
    unsigned int domain_events_registered :1;
181 182 183 184

    struct sockaddr_storage addr;
    socklen_t addrlen;

185 186
    int type; /* qemud_sock_type */
    gnutls_session_t tlssession;
187
    int auth;
188
    unsigned int handshake :1; /* If we're in progress for TLS handshake */
189
# if HAVE_SASL
190
    sasl_conn_t *saslconn;
191 192 193 194 195 196 197
    int saslSSF;
    const char *saslDecoded;
    unsigned int saslDecodedLength;
    unsigned int saslDecodedOffset;
    const char *saslEncoded;
    unsigned int saslEncodedLength;
    unsigned int saslEncodedOffset;
198
    char *saslUsername;
199
# endif
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214
    /* Count of meages in 'dx' or 'tx' queue
     * ie RPC calls in progress. Does not count
     * async events which are not used for
     * throttling calculations */
    int nrequests;
    /* Zero or one messages being received. Zero if
     * nrequests >= max_clients and throttling */
    struct qemud_client_message *rx;
    /* Zero or many messages waiting for a worker
     * to process them */
    struct qemud_client_message *dx;
    /* Zero or many messages waiting for transmit
     * back to client, including async events */
    struct qemud_client_message *tx;
215 216 217
    /* Filters to capture messages that would otherwise
     * end up on the 'dx' queue */
    struct qemud_client_filter *filters;
218

219 220 221 222
    /* Data streams */
    struct qemud_client_stream *streams;


223 224 225 226 227
    /* This is only valid if a remote open call has been made on this
     * connection, otherwise it will be NULL.  Also if remote close is
     * called, it will be set back to NULL if that succeeds.
     */
    virConnectPtr conn;
228
    int refs;
229

D
Daniel P. Berrange 已提交
230 231
};

232
# define QEMUD_CLIENT_MAGIC 0x7788aaee
233

D
Daniel P. Berrange 已提交
234 235 236

struct qemud_socket {
    int fd;
237
    int watch;
D
Daniel P. Berrange 已提交
238
    int readonly;
239
    int type; /* qemud_sock_type */
240
    int auth;
241
    int port;
D
Daniel P. Berrange 已提交
242 243 244
    struct qemud_socket *next;
};

245 246
struct qemud_worker {
    pthread_t thread;
247 248 249
    unsigned int hasThread :1;
    unsigned int processingCall :1;
    unsigned int quitRequest :1;
250 251 252 253 254

    /* back-pointer to our server */
    struct qemud_server *server;
};

D
Daniel P. Berrange 已提交
255 256
/* Main server state */
struct qemud_server {
257 258
    virMutex lock;
    virCond job;
259

260 261
    int privileged;

262
    int nworkers;
263 264
    int nactiveworkers;
    struct qemud_worker *workers;
D
Daniel P. Berrange 已提交
265 266 267
    int nsockets;
    struct qemud_socket *sockets;
    int nclients;
268
    struct qemud_client **clients;
269

270
    int sigread;
271
    int sigwrite;
272
    char *logDir;
273 274 275
    pthread_t eventThread;
    unsigned int hasEventThread :1;
    unsigned int quitEventThread :1;
276
# ifdef HAVE_AVAHI
277
    struct libvirtd_mdns *mdns;
278 279
# endif
# if HAVE_SASL
280
    char **saslUsernameWhitelist;
281 282
# endif
# if HAVE_POLKIT0
283
    DBusConnection *sysbus;
284
# endif
D
Daniel P. Berrange 已提交
285 286
};

287
void qemudLog(int priority, const char *fmt, ...)
288
    ATTRIBUTE_FMT_PRINTF(2,3);
289

290

291

292
int qemudRegisterClientEvent(struct qemud_server *server,
293 294
                             struct qemud_client *client);
void qemudUpdateClientEvent(struct qemud_client *client);
295

296 297 298 299 300
void qemudDispatchClientFailure(struct qemud_client *client);

void
qemudClientMessageQueuePush(struct qemud_client_message **queue,
                            struct qemud_client_message *msg);
301 302
struct qemud_client_message *
qemudClientMessageQueueServe(struct qemud_client_message **queue);
303

304 305 306
void
qemudClientMessageRelease(struct qemud_client *client,
                          struct qemud_client_message *msg);
307

308

309
# if HAVE_POLKIT
310
int qemudGetSocketIdentity(int fd, uid_t *uid, pid_t *pid);
311
# endif
312

D
Daniel P. Berrange 已提交
313
#endif