ia64/xen-unstable

view tools/xenstore/xs_lib.c @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
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 /* Simple routines for writing to sockets, etc. */
70 bool xs_write_all(int fd, const void *data, unsigned int len)
71 {
72 while (len) {
73 int done;
75 done = write(fd, data, len);
76 if (done < 0 && errno == EINTR)
77 continue;
78 if (done <= 0)
79 return false;
80 data += done;
81 len -= done;
82 }
84 return true;
85 }
87 /* Convert strings to permissions. False if a problem. */
88 bool xs_strings_to_perms(struct xs_permissions *perms, unsigned int num,
89 const char *strings)
90 {
91 const char *p;
92 char *end;
93 unsigned int i;
95 for (p = strings, i = 0; i < num; i++) {
96 /* "r", "w", or "b" for both. */
97 switch (*p) {
98 case 'r':
99 perms[i].perms = XS_PERM_READ;
100 break;
101 case 'w':
102 perms[i].perms = XS_PERM_WRITE;
103 break;
104 case 'b':
105 perms[i].perms = XS_PERM_READ|XS_PERM_WRITE;
106 break;
107 case 'n':
108 perms[i].perms = XS_PERM_NONE;
109 break;
110 default:
111 errno = EINVAL;
112 return false;
113 }
114 p++;
115 perms[i].id = strtol(p, &end, 0);
116 if (*end || !*p) {
117 errno = EINVAL;
118 return false;
119 }
120 p = end + 1;
121 }
122 return true;
123 }
125 /* Convert permissions to a string (up to len MAX_STRLEN(domid_t)+1). */
126 bool xs_perm_to_string(const struct xs_permissions *perm, char *buffer)
127 {
128 switch (perm->perms) {
129 case XS_PERM_WRITE:
130 *buffer = 'w';
131 break;
132 case XS_PERM_READ:
133 *buffer = 'r';
134 break;
135 case XS_PERM_READ|XS_PERM_WRITE:
136 *buffer = 'b';
137 break;
138 case XS_PERM_NONE:
139 *buffer = 'n';
140 break;
141 default:
142 errno = EINVAL;
143 return false;
144 }
145 sprintf(buffer+1, "%i", (int)perm->id);
146 return true;
147 }
149 /* Given a string and a length, count how many strings (nul terms). */
150 unsigned int xs_count_strings(const char *strings, unsigned int len)
151 {
152 unsigned int num;
153 const char *p;
155 for (p = strings, num = 0; p < strings + len; p++)
156 if (*p == '\0')
157 num++;
159 return num;
160 }