aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xenstore/xenstored_solaris.c
blob: 06052d124a0c8eee2a959addfeb09026186994d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/******************************************************************************
 *
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright (C) 2005 Rusty Russell IBM Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2 of the
 * License.
 */

#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/mman.h>
#include <strings.h>
#include <ucred.h>
#include <stdio.h>

#include <xen/sys/xenbus.h>

#include "talloc.h"
#include "xenstored_core.h"
#include "xenstored_probes.h"

evtchn_port_t xenbus_evtchn(void)
{
	int fd;
	evtchn_port_t port; 

	fd = open("/dev/xen/xenbus", O_RDONLY); 
	if (fd == -1)
		return -1;

	port = ioctl(fd, IOCTL_XENBUS_XENSTORE_EVTCHN);

	close(fd); 
	return port;
}

void *xenbus_map(void)
{
	int fd;
	void *addr;

	fd = open("/dev/xen/xenbus", O_RDWR);
	if (fd == -1)
		return NULL;

	addr = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
		MAP_SHARED, fd, 0);

	if (addr == MAP_FAILED)
		addr = NULL;

	close(fd);

	return addr;
}

void xenbus_notify_running(void)
{
	int fd;

	fd = open("/dev/xen/xenbus", O_RDONLY);

	(void) ioctl(fd, IOCTL_XENBUS_NOTIFY_UP);

	close(fd);
}

static pid_t cred(const struct connection *conn)
{
	ucred_t *ucred = NULL;
	pid_t pid;

	if (conn->domain)
		return (0);

	if (getpeerucred(conn->fd, &ucred) == -1)
		return (0);

	pid = ucred_getpid(ucred);

	ucred_free(ucred);
	return (pid);
}

/*
 * The strings are often a number of nil-separated strings. We'll just
 * replace the separators with spaces - not quite right, but good
 * enough.
 */
static char *
mangle(const struct connection *conn, const struct buffered_data *in)
{
	char *str;
	int i;

	if (in->hdr.msg.len == 0)
		return (talloc_strdup(conn, ""));

	if ((str = talloc_zero_size(conn, in->hdr.msg.len + 1)) == NULL)
		return (NULL);

	memcpy(str, in->buffer, in->hdr.msg.len);
	
	/*
	 * The protocol is absurdly inconsistent in whether the length
	 * includes the terminating nil or not; replace all nils that
	 * aren't the last one.
	 */
	for (i = 0; i < (in->hdr.msg.len - 1); i++) {
		if (str[i] == '\0')
			str[i] = ' ';
	}

	return (str);
}

void
dtrace_io(const struct connection *conn, const struct buffered_data *in,
    int io_out)
{
	if (!io_out) {
		if (XENSTORE_MSG_ENABLED()) {
			char *mangled = mangle(conn, in);
			XENSTORE_MSG(in->hdr.msg.tx_id, conn->id, cred(conn),
			    in->hdr.msg.type, mangled);
		}

		goto out;
	}

	switch (in->hdr.msg.type) {
	case XS_ERROR:
		if (XENSTORE_ERROR_ENABLED()) {
			char *mangled = mangle(conn, in);
			XENSTORE_ERROR(in->hdr.msg.tx_id, conn->id,
			    cred(conn), mangled);
		}
		break;

	case XS_WATCH_EVENT:
		if (XENSTORE_WATCH_EVENT_ENABLED()) {
			char *mangled = mangle(conn, in);
			XENSTORE_WATCH_EVENT(conn->id, cred(conn), mangled);
		}
		break;

	default:
		if (XENSTORE_REPLY_ENABLED()) {
			char *mangled = mangle(conn, in);
			XENSTORE_REPLY(in->hdr.msg.tx_id, conn->id, cred(conn),
			    in->hdr.msg.type, mangled);
		}
		break;
	}

out:
	/*
	 * 6589130 dtrace -G fails for certain tail-calls on x86
	 */
	asm("nop");
}