initial libnfs checkin
[deb_libnfs.git] / lib / init.c
... / ...
CommitLineData
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdarg.h>
21#include <unistd.h>
22#include <string.h>
23#include <strings.h>
24#include <rpc/xdr.h>
25#include "slist.h"
26#include "libnfs.h"
27#include "libnfs-raw.h"
28#include "libnfs-private.h"
29
30struct rpc_context *rpc_init_context(void)
31{
32 struct rpc_context *rpc;
33
34 rpc = malloc(sizeof(struct rpc_context));
35 if (rpc == NULL) {
36 printf("Failed to allocate rpc context\n");
37 return NULL;
38 }
39 bzero(rpc, sizeof(struct rpc_context));
40
41 rpc->encodebuflen = 65536;
42 rpc->encodebuf = malloc(rpc->encodebuflen);
43 if (rpc->encodebuf == NULL) {
44 printf("Failed to allocate a buffer for rpc encoding\n");
45 free(rpc);
46 return NULL;
47 }
48
49 rpc->auth = authunix_create_default();
50 if (rpc->auth == NULL) {
51 printf("failed to create authunix\n");
52 free(rpc->encodebuf);
53 free(rpc);
54 return NULL;
55 }
56 rpc->xid = 1;
57 rpc->fd = -1;
58
59 return rpc;
60}
61
62
63void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
64{
65 if (rpc->auth != NULL) {
66 auth_destroy(rpc->auth);
67 }
68 rpc->auth = auth;
69}
70
71
72void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
73{
74 va_list ap;
75 char *str;
76
77 if (rpc->error_string != NULL) {
78 free(rpc->error_string);
79 }
80 va_start(ap, error_string);
81 vasprintf(&str, error_string, ap);
82 rpc->error_string = str;
83 va_end(ap);
84}
85
86char *rpc_get_error(struct rpc_context *rpc)
87{
88 return rpc->error_string;
89}
90
91void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
92{
93 struct rpc_pdu *pdu;
94
95 while((pdu = rpc->outqueue) != NULL) {
96 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
97 SLIST_REMOVE(&rpc->outqueue, pdu);
98 rpc_free_pdu(rpc, pdu);
99 }
100 while((pdu = rpc->waitpdu) != NULL) {
101 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
102 SLIST_REMOVE(&rpc->waitpdu, pdu);
103 rpc_free_pdu(rpc, pdu);
104 }
105}
106
107
108void rpc_destroy_context(struct rpc_context *rpc)
109{
110 struct rpc_pdu *pdu;
111
112 while((pdu = rpc->outqueue) != NULL) {
113 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
114 SLIST_REMOVE(&rpc->outqueue, pdu);
115 rpc_free_pdu(rpc, pdu);
116 }
117 while((pdu = rpc->waitpdu) != NULL) {
118 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
119 SLIST_REMOVE(&rpc->waitpdu, pdu);
120 rpc_free_pdu(rpc, pdu);
121 }
122
123 auth_destroy(rpc->auth);
124 rpc->auth =NULL;
125
126 if (rpc->fd != -1) {
127 close(rpc->fd);
128 }
129
130 if (rpc->encodebuf != NULL) {
131 free(rpc->encodebuf);
132 rpc->encodebuf = NULL;
133 }
134
135 if (rpc->error_string != NULL) {
136 free(rpc->error_string);
137 rpc->error_string = NULL;
138 }
139
140 free(rpc);
141}
142
143