debuggers.hg

view tools/xenstore/xs_lib.c @ 6644:29808fef9148

merge?
author cl349@firebug.cl.cam.ac.uk
date Sat Sep 03 18:24:46 2005 +0000 (2005-09-03)
parents dd668f7527cb f27205ea60ef
children b6c98fe62e1a
line source
1 /*
2 Common routines between Xen store user library and daemon.
3 Copyright (C) 2005 Rusty Russell IBM Corporation
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
20 #include "xs_lib.h"
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
27 /* Common routines for the Xen store daemon and client library. */
29 static const char *xs_daemon_rootdir(void)
30 {
31 char *s = getenv("XENSTORED_ROOTDIR");
32 return (s ? s : "/var/lib/xenstored");
33 }
35 static const char *xs_daemon_rundir(void)
36 {
37 char *s = getenv("XENSTORED_RUNDIR");
38 return (s ? s : "/var/run/xenstored");
39 }
41 const char *xs_daemon_socket(void)
42 {
43 static char buf[PATH_MAX];
44 sprintf(buf, "%s/socket", xs_daemon_rundir());
45 return buf;
46 }
48 const char *xs_daemon_socket_ro(void)
49 {
50 static char buf[PATH_MAX];
51 sprintf(buf, "%s/socket_ro", xs_daemon_rundir());
52 return buf;
53 }
55 const char *xs_daemon_store(void)
56 {
57 static char buf[PATH_MAX];
58 sprintf(buf, "%s/store", xs_daemon_rootdir());
59 return buf;
60 }
62 const char *xs_daemon_transactions(void)
63 {
64 static char buf[PATH_MAX];
65 sprintf(buf, "%s/transactions", xs_daemon_rootdir());
66 return buf;
67 }
69 const char *xs_domain_dev(void)
70 {
71 char *s = getenv("XENSTORED_DOMAIN_DEV");
72 return (s ? s : "/proc/xen/xenbus");
73 }
75 /* Simple routines for writing to sockets, etc. */
76 bool xs_write_all(int fd, const void *data, unsigned int len)
77 {
78 while (len) {
79 int done;
81 done = write(fd, data, len);
82 if (done < 0 && errno == EINTR)
83 continue;
84 if (done <= 0)
85 return false;
86 data += done;
87 len -= done;
88 }
90 return true;
91 }
93 /* Convert strings to permissions. False if a problem. */
94 bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
95 const char *strings)
96 {
97 const char *p;
98 char *end;
99 unsigned int i;
101 for (p = strings, i = 0; i < num; i++) {
102 /* "r", "w", or "b" for both. */
103 switch (*p) {
104 case 'r':
105 perms[i].perms = XS_PERM_READ;
106 break;
107 case 'w':
108 perms[i].perms = XS_PERM_WRITE;
109 break;
110 case 'b':
111 perms[i].perms = XS_PERM_READ|XS_PERM_WRITE;
112 break;
113 case 'n':
114 perms[i].perms = XS_PERM_NONE;
115 break;
116 default:
117 errno = EINVAL;
118 return false;
119 }
120 p++;
121 perms[i].id = strtol(p, &end, 0);
122 if (*end || !*p) {
123 errno = EINVAL;
124 return false;
125 }
126 p = end + 1;
127 }
128 return true;
129 }
131 /* Convert permissions to a string (up to len MAX_STRLEN(domid_t)+1). */
132 bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer)
133 {
134 switch (perm->perms) {
135 case XS_PERM_WRITE:
136 *buffer = 'w';
137 break;
138 case XS_PERM_READ:
139 *buffer = 'r';
140 break;
141 case XS_PERM_READ|XS_PERM_WRITE:
142 *buffer = 'b';
143 break;
144 case XS_PERM_NONE:
145 *buffer = 'n';
146 break;
147 default:
148 errno = EINVAL;
149 return false;
150 }
151 sprintf(buffer+1, "%i", (int)perm->id);
152 return true;
153 }
155 /* Given a string and a length, count how many strings (nul terms). */
156 unsigned int xs_count_strings(const char *strings, unsigned int len)
157 {
158 unsigned int num;
159 const char *p;
161 for (p = strings, num = 0; p < strings + len; p++)
162 if (*p == '\0')
163 num++;
165 return num;
166 }