Debian Codegraph @

libnpclient/fsys.c

raw
1 /*
2 * Copyright (C) 2006 by Latchesar Ionkov <[email protected]>
3 * Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /* fsys.c - simpler, single-threaded version of Lucho's original */
26
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdarg.h>
35 #include <pthread.h>
36 #include <errno.h>
37 #include <sys/socket.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40
41 #include "9p.h"
42 #include "npfs.h"
43 #include "npclient.h"
44 #include "xpthread.h"
45 #include "npcimpl.h"
46
47 static int npc_rpc(Npcfsys *fs, Npfcall *tc, Npfcall **rcp);
48 static void npc_incref_fsys(Npcfsys *fs);
49 static void npc_decref_fsys(Npcfsys *fs);
50
51 Npcfsys *
52 npc_create_fsys(int rfd, int wfd, int msize, int flags)
53 {
54 Npcfsys *fs;
55
56 fs = malloc(sizeof(*fs));
57 if (!fs) {
58 np_uerror(ENOMEM);
59 return NULL;
60 }
61
62 np_uerror (0);
63 pthread_mutex_init(&fs->lock, NULL);
64 fs->msize = msize;
65 fs->trans = NULL;
66 fs->tagpool = NULL;
67 fs->fidpool = NULL;
68 fs->refcount = 1;
69 fs->rpc = npc_rpc;
70 fs->incref = npc_incref_fsys;
71 fs->decref = npc_decref_fsys;
72 fs->disconnect = NULL;
73 fs->flags = flags;
74
75 fs->trans = np_fdtrans_create(rfd, wfd);
76 if (!fs->trans)
77 goto error;
78 fs->tagpool = npc_create_pool(P9_NOTAG);
79 if (!fs->tagpool)
80 goto error;
81 fs->fidpool = npc_create_pool(P9_NOFID);
82 if (!fs->fidpool)
83 goto error;
84 return fs;
85
86 error:
87 npc_decref_fsys(fs); /* will close fds if trans successfully created */
88 (void)close (rfd); /* close here anyway for consistancy */
89 if (rfd != wfd)
90 (void)close (wfd);
91 return NULL;
92 }
93
94 static void
95 npc_incref_fsys(Npcfsys *fs)
96 {
97 xpthread_mutex_lock(&fs->lock);
98 fs->refcount++;
99 xpthread_mutex_unlock(&fs->lock);
100 }
101
102 static void
103 npc_decref_fsys(Npcfsys *fs)
104 {
105 xpthread_mutex_lock(&fs->lock);
106 fs->refcount--;
107 if (fs->refcount) {
108 xpthread_mutex_unlock(&fs->lock);
109 return;
110 }
111 if (fs->trans) {
112 np_trans_destroy(fs->trans); /* closes fds */
113 fs->trans = NULL;
114 }
115 if (fs->tagpool) {
116 npc_destroy_pool(fs->tagpool);
117 fs->tagpool = NULL;
118 }
119
120 if (fs->fidpool) {
121 npc_destroy_pool(fs->fidpool);
122 fs->fidpool = NULL;
123 }
124 xpthread_mutex_unlock(&fs->lock);
125 pthread_mutex_destroy(&fs->lock);
126 free(fs);
127 }
128
129 static int
130 npc_rpc(Npcfsys *fs, Npfcall *tc, Npfcall **rcp)
131 {
132 Npfcall *rc = NULL;
133 u16 tag = P9_NOTAG;
134 int n, ret = -1;
135
136 if (!fs->trans) {
137 np_uerror(ECONNABORTED);
138 goto done;
139 }
140 if (tc->type != P9_TVERSION)
141 tag = npc_get_id(fs->tagpool);
142 np_set_tag(tc, tag);
143
144 xpthread_mutex_lock(&fs->lock);
145 n = np_trans_send (fs->trans, tc);
146 if (n >= 0)
147 n = np_trans_recv(fs->trans, &rc, fs->msize);
148 xpthread_mutex_unlock(&fs->lock);
149 if (n < 0)
150 goto done;
151 if (rc == NULL) {
152 np_uerror (EPROTO); /* premature EOF */
153 goto done;
154 }
155 if (tc->tag != rc->tag) {
156 np_uerror (EPROTO); /* unmatched response */
157 goto done;
158 }
159 if (rc->type == P9_RLERROR) {
160 np_uerror (rc->u.rlerror.ecode);
161 goto done;
162 }
163 *rcp = rc;
164 ret = 0;
165 done:
166 if (tag != P9_NOTAG)
167 npc_put_id(fs->tagpool, tag);
168 if (ret < 0 && rc != NULL)
169 free (rc);
170 return ret;
171 }