Add a new nfs_stat64() function that operates on a always-64-bit stat structure
[deb_libnfs.git] / lib / init.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
2606f9bb 4
84004dbf
RS
5
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU Lesser General Public License for more details.
10
11 You should have received a copy of the GNU Lesser General Public License
12 along with this program; if not, see <http://www.gnu.org/licenses/>.
13*/
00748f36
RS
14#ifdef HAVE_CONFIG_H
15#include "config.h"
16#endif
84004dbf 17
108c622a
RS
18#ifdef AROS
19#include "aros_compat.h"
20#endif
21
a8a1b858
M
22#ifdef WIN32
23#include "win32_compat.h"
bff8fe46
RS
24#endif
25
a8a1b858 26#define _GNU_SOURCE
eecdc4f3 27
00748f36
RS
28#ifdef HAVE_UNISTD_H
29#include <unistd.h>
30#endif
31
bff8fe46
RS
32#ifdef HAVE_STRINGS_H
33#include <strings.h>
34#endif
35
84004dbf
RS
36#include <stdio.h>
37#include <stdarg.h>
84004dbf 38#include <string.h>
98f5fee8 39#include <stdlib.h>
f3a75078 40#include <assert.h>
a2756193 41#include <time.h>
84004dbf 42#include "slist.h"
763cd6e3 43#include "libnfs-zdr.h"
84004dbf
RS
44#include "libnfs.h"
45#include "libnfs-raw.h"
46#include "libnfs-private.h"
47
48struct rpc_context *rpc_init_context(void)
49{
50 struct rpc_context *rpc;
a2756193 51 static uint32_t salt = 0;
84004dbf
RS
52
53 rpc = malloc(sizeof(struct rpc_context));
54 if (rpc == NULL) {
84004dbf
RS
55 return NULL;
56 }
ea98629a 57 memset(rpc, 0, sizeof(struct rpc_context));
84004dbf 58
f3a75078 59 rpc->magic = RPC_CONTEXT_MAGIC;
35280fd7
RS
60
61 /* Allow 1M of data (for writes) and some */
62 rpc->encodebuflen = 1024 * 1024 + 4096;
84004dbf
RS
63 rpc->encodebuf = malloc(rpc->encodebuflen);
64 if (rpc->encodebuf == NULL) {
84004dbf
RS
65 free(rpc);
66 return NULL;
67 }
68
eecdc4f3 69 rpc->auth = authunix_create_default();
84004dbf 70 if (rpc->auth == NULL) {
84004dbf
RS
71 free(rpc->encodebuf);
72 free(rpc);
73 return NULL;
74 }
6fda9871 75 rpc->xid = salt + time(NULL) + getpid() << 16;
a2756193 76 salt += 0x01000000;
84004dbf 77 rpc->fd = -1;
1c8b4547 78 rpc->tcp_syncnt = RPC_PARAM_UNDEFINED;
9126c9c0
PL
79#ifdef WIN32
80 rpc->uid = 65534;
81 rpc->gid = 65534;
82#else
83 rpc->uid = getuid();
84 rpc->gid = getgid();
85#endif
84004dbf
RS
86
87 return rpc;
88}
89
90
9e00b8c6
RS
91struct rpc_context *rpc_init_udp_context(void)
92{
93 struct rpc_context *rpc;
94
95 rpc = rpc_init_context();
96 if (rpc != NULL) {
97 rpc->is_udp = 1;
98 }
99
100 return rpc;
101}
102
67ba2239 103void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
84004dbf 104{
f3a75078
RS
105 assert(rpc->magic == RPC_CONTEXT_MAGIC);
106
84004dbf
RS
107 if (rpc->auth != NULL) {
108 auth_destroy(rpc->auth);
109 }
8704724f 110 rpc->auth = auth;
84004dbf
RS
111}
112
9126c9c0
PL
113static void rpc_set_uid_gid(struct rpc_context *rpc, int uid, int gid) {
114 if (uid != rpc->uid || gid != rpc->gid) {
115 struct AUTH *auth = libnfs_authunix_create("libnfs", uid, gid, 0, NULL);
116 if (auth != NULL) {
117 rpc_set_auth(rpc, auth);
118 rpc->uid = uid;
119 rpc->gid = gid;
120 }
121 }
122}
123
124void rpc_set_uid(struct rpc_context *rpc, int uid) {
125 rpc_set_uid_gid(rpc, uid, rpc->gid);
126}
127
128void rpc_set_gid(struct rpc_context *rpc, int gid) {
129 rpc_set_uid_gid(rpc, rpc->uid, gid);
130}
84004dbf
RS
131
132void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
133{
134 va_list ap;
84004dbf 135
f3a75078
RS
136 assert(rpc->magic == RPC_CONTEXT_MAGIC);
137
84004dbf
RS
138 if (rpc->error_string != NULL) {
139 free(rpc->error_string);
140 }
141 va_start(ap, error_string);
b85c7de2
RS
142 rpc->error_string = malloc(1024);
143 vsnprintf(rpc->error_string, 1024, error_string, ap);
84004dbf
RS
144 va_end(ap);
145}
146
147char *rpc_get_error(struct rpc_context *rpc)
148{
f3a75078
RS
149 assert(rpc->magic == RPC_CONTEXT_MAGIC);
150
84004dbf
RS
151 return rpc->error_string;
152}
153
154void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
155{
156 struct rpc_pdu *pdu;
157
f3a75078
RS
158 assert(rpc->magic == RPC_CONTEXT_MAGIC);
159
84004dbf
RS
160 while((pdu = rpc->outqueue) != NULL) {
161 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
162 SLIST_REMOVE(&rpc->outqueue, pdu);
163 rpc_free_pdu(rpc, pdu);
164 }
165 while((pdu = rpc->waitpdu) != NULL) {
166 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
167 SLIST_REMOVE(&rpc->waitpdu, pdu);
168 rpc_free_pdu(rpc, pdu);
169 }
170}
171
d678b73e
RS
172static void rpc_free_fragment(struct rpc_fragment *fragment)
173{
174 if (fragment->data != NULL) {
175 free(fragment->data);
176 }
177 free(fragment);
178}
179
180void rpc_free_all_fragments(struct rpc_context *rpc)
181{
f3a75078
RS
182 assert(rpc->magic == RPC_CONTEXT_MAGIC);
183
d678b73e
RS
184 while (rpc->fragments != NULL) {
185 struct rpc_fragment *fragment = rpc->fragments;
186
187 SLIST_REMOVE(&rpc->fragments, fragment);
188 rpc_free_fragment(fragment);
189 }
190}
191
183451cf 192int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size)
d678b73e
RS
193{
194 struct rpc_fragment *fragment;
195
f3a75078
RS
196 assert(rpc->magic == RPC_CONTEXT_MAGIC);
197
d678b73e
RS
198 fragment = malloc(sizeof(struct rpc_fragment));
199 if (fragment == NULL) {
200 return -1;
201 }
202
203 fragment->size = size;
204 fragment->data = malloc(fragment->size);
205 if(fragment->data == NULL) {
206 free(fragment);
207 return -1;
208 }
209
210 memcpy(fragment->data, data, fragment->size);
211 SLIST_ADD_END(&rpc->fragments, fragment);
212 return 0;
213}
84004dbf
RS
214
215void rpc_destroy_context(struct rpc_context *rpc)
216{
217 struct rpc_pdu *pdu;
218
f3a75078
RS
219 assert(rpc->magic == RPC_CONTEXT_MAGIC);
220
84004dbf
RS
221 while((pdu = rpc->outqueue) != NULL) {
222 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
223 SLIST_REMOVE(&rpc->outqueue, pdu);
224 rpc_free_pdu(rpc, pdu);
225 }
226 while((pdu = rpc->waitpdu) != NULL) {
227 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
228 SLIST_REMOVE(&rpc->waitpdu, pdu);
229 rpc_free_pdu(rpc, pdu);
230 }
231
d678b73e
RS
232 rpc_free_all_fragments(rpc);
233
84004dbf
RS
234 auth_destroy(rpc->auth);
235 rpc->auth =NULL;
236
237 if (rpc->fd != -1) {
eecdc4f3 238 close(rpc->fd);
84004dbf
RS
239 }
240
241 if (rpc->encodebuf != NULL) {
242 free(rpc->encodebuf);
243 rpc->encodebuf = NULL;
244 }
245
246 if (rpc->error_string != NULL) {
247 free(rpc->error_string);
248 rpc->error_string = NULL;
249 }
250
7ff2f3a0
RS
251 if (rpc->udp_dest != NULL) {
252 free(rpc->udp_dest);
253 rpc->udp_dest = NULL;
254 }
255
f3a75078 256 rpc->magic = 0;
84004dbf
RS
257 free(rpc);
258}
259
260