ia64/xen-unstable

view tools/xenstore/xs_lib.c @ 6946:e703abaf6e3d

Add behaviour to the remove methods to remove the transaction's path itself. This allows us to write Remove(path) to remove the specified path rather than having to slice the path ourselves.
author emellor@ewan
date Sun Sep 18 14:42:13 2005 +0100 (2005-09-18)
parents 3233e7ecfa9f
children 06d84bf87159
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 static const char *xs_daemon_path(void)
42 {
43 static char buf[PATH_MAX];
44 char *s = getenv("XENSTORED_PATH");
45 if (s)
46 return s;
47 if (snprintf(buf, PATH_MAX, "%s/socket",
48 xs_daemon_rundir()) >= PATH_MAX)
49 return NULL;
50 return buf;
51 }
53 const char *xs_daemon_socket(void)
54 {
55 return xs_daemon_path();
56 }
58 const char *xs_daemon_socket_ro(void)
59 {
60 static char buf[PATH_MAX];
61 const char *s = xs_daemon_path();
62 if (s == NULL)
63 return NULL;
64 if (snprintf(buf, PATH_MAX, "%s_ro", s) >= PATH_MAX)
65 return NULL;
66 return buf;
67 }
69 const char *xs_daemon_store(void)
70 {
71 static char buf[PATH_MAX];
72 if (snprintf(buf, PATH_MAX, "%s/store",
73 xs_daemon_rootdir()) >= PATH_MAX)
74 return NULL;
75 return buf;
76 }
78 const char *xs_daemon_transactions(void)
79 {
80 static char buf[PATH_MAX];
81 if (snprintf(buf, PATH_MAX, "%s/transactions",
82 xs_daemon_rootdir()) >= PATH_MAX)
83 return NULL;
84 return buf;
85 }
87 const char *xs_domain_dev(void)
88 {
89 char *s = getenv("XENSTORED_PATH");
90 return (s ? s : "/proc/xen/xenbus");
91 }
93 /* Simple routines for writing to sockets, etc. */
94 bool xs_write_all(int fd, const void *data, unsigned int len)
95 {
96 while (len) {
97 int done;
99 done = write(fd, data, len);
100 if (done < 0 && errno == EINTR)
101 continue;
102 if (done <= 0)
103 return false;
104 data += done;
105 len -= done;
106 }
108 return true;
109 }
111 /* Convert strings to permissions. False if a problem. */
112 bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
113 const char *strings)
114 {
115 const char *p;
116 char *end;
117 unsigned int i;
119 for (p = strings, i = 0; i < num; i++) {
120 /* "r", "w", or "b" for both. */
121 switch (*p) {
122 case 'r':
123 perms[i].perms = XS_PERM_READ;
124 break;
125 case 'w':
126 perms[i].perms = XS_PERM_WRITE;
127 break;
128 case 'b':
129 perms[i].perms = XS_PERM_READ|XS_PERM_WRITE;
130 break;
131 case 'n':
132 perms[i].perms = XS_PERM_NONE;
133 break;
134 default:
135 errno = EINVAL;
136 return false;
137 }
138 p++;
139 perms[i].id = strtol(p, &end, 0);
140 if (*end || !*p) {
141 errno = EINVAL;
142 return false;
143 }
144 p = end + 1;
145 }
146 return true;
147 }
149 /* Convert permissions to a string (up to len MAX_STRLEN(domid_t)+1). */
150 bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer)
151 {
152 switch (perm->perms) {
153 case XS_PERM_WRITE:
154 *buffer = 'w';
155 break;
156 case XS_PERM_READ:
157 *buffer = 'r';
158 break;
159 case XS_PERM_READ|XS_PERM_WRITE:
160 *buffer = 'b';
161 break;
162 case XS_PERM_NONE:
163 *buffer = 'n';
164 break;
165 default:
166 errno = EINVAL;
167 return false;
168 }
169 sprintf(buffer+1, "%i", (int)perm->id);
170 return true;
171 }
173 /* Given a string and a length, count how many strings (nul terms). */
174 unsigned int xs_count_strings(const char *strings, unsigned int len)
175 {
176 unsigned int num;
177 const char *p;
179 for (p = strings, num = 0; p < strings + len; p++)
180 if (*p == '\0')
181 num++;
183 return num;
184 }