1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/*
* DCAP - dCache Access Protocol client interface
*
* Copyright (C) 2000,2004 DESY Hamburg DMG-Division.
*
* AUTHOR: Tigran Mkrtchayn (tigran.mkrtchyan@desy.de)
*
* This program can be distributed under the terms of the GNU LGPL.
* See the file COPYING.LIB
*
*/
/*
* $Id: node_plays.c,v 1.34 2006-09-22 13:25:46 tigran Exp $
*/
#include <stdlib.h>
#include <string.h>
#include "dcap.h"
#include "dcap_types.h"
#include "debug_level.h"
#include "xutil.h"
#ifdef WIN32
# define PATH_SEPARATOR '\\'
#else
# define PATH_SEPARATOR '/'
#endif /* WIN32 */
static RDLOCK(nodeRWlock);
/* Local function prototypes */
static void real_node_unplug( struct vsp_node *);
static int node_init(struct vsp_node *node, const char *path);
static void real_node_unplug( struct vsp_node *node);
static struct vsp_node *vspNode = NULL;
static struct vsp_node *lastNode = NULL;
void node_dupToAll(struct vsp_node *node, int fd)
{
unsigned int i;
for( i = 0; i < node->reference; i++) {
if( node->fd_set[i] != fd ) {
node->fd_set[i] = dup2(fd, node->fd_set[i] );
}
}
}
void node_attach_fd( struct vsp_node *node, int fd)
{
node->fd_set[node->reference] = fd;
++(node->reference);
node->dataFd = fd;
}
void node_detach_fd( struct vsp_node *node, int fd)
{
unsigned int i;
for( i = 0; i < node->reference; i++) {
if( node->fd_set[i] == fd ) {
--node->reference;
if( node->reference != 0 ) {
node->fd_set[i] = node->fd_set[node->reference];
}
node->dataFd = fd;
}
}
}
int node_init(struct vsp_node *node, const char *path)
{
node->next = NULL;
node->pnfsId = NULL;
node->ahead = NULL;
node->fd = -1;
node->dataFd = -1;
node->asciiCommand = 0;
node->pos = 0;
node->whence = -1;
node->seek = 0;
node->stagelocation = NULL;
node->unsafeWrite = getenv("DCACHE_USE_UNSAFE") != NULL ? 1 : 0;
node->url = NULL;
node->tunnel = NULL;
node->sndBuf = 0;
node->rcvBuf = 0;
node->ipc = NULL;
node->uid = -1;
node->gid = -1;
node->reference = 0;
node->sum = NULL;
node->file_name = xbasename(path);
node->directory = xdirname(path);
node->isPassive = 0;
return 0;
}
struct vsp_node *
new_vsp_node(const char *path)
{
struct vsp_node *node;
/* allocate and clear memory */
node = (struct vsp_node *) calloc(1,sizeof(struct vsp_node));
if (node == NULL) {
dc_errno = DENODE;
return NULL;
}
if(node_init(node, path) < 0) {
dc_errno = DENODE;
free(node);
return NULL;
}
rw_wrlock(&nodeRWlock);
if (vspNode == NULL) {
vspNode = node;
node->prev = NULL;
} else {
node->prev = lastNode;
lastNode->next = node;
}
lastNode = node;
m_init(&node->mux);
m_lock(&node->mux);
rw_unlock(&nodeRWlock);
return node;
}
struct vsp_node *
delete_vsp_node(int fd)
{
struct vsp_node *node;
unsigned int i;
rw_wrlock(&nodeRWlock);
node = vspNode;
while (node != NULL) {
for( i = 0; i < node->reference; i++ ) {
if (node->fd_set[i] == fd) {
node_detach_fd(node, fd);
real_node_unplug(node);
m_lock(&node->mux);
rw_unlock(&nodeRWlock);
return node;
}
}
node = node->next;
}
/* node not exist */
rw_unlock(&nodeRWlock);
return NULL;
}
struct vsp_node *
get_vsp_node(int fd)
{
struct vsp_node *node;
unsigned int i;
rw_rdlock(&nodeRWlock);
node = vspNode;
while (node != NULL) {
for( i = 0; i < node->reference; i++ ) {
if (node->fd_set[i] == fd) {
node->dataFd = fd;
m_lock(&node->mux);
rw_unlock(&nodeRWlock);
return node;
}
}
node = node->next;
}
rw_unlock(&nodeRWlock);
return NULL;
}
void node_destroy( struct vsp_node *node)
{
if(node == NULL) return;
if( node->reference > 0 ) {
dc_debug(DC_INFO, "[%d] reference %d destroy canceled", node->dataFd, node->reference);
m_unlock(&node->mux);
return;
}
dc_debug(DC_INFO, "[%d] destroing node", node->dataFd);
free(node->pnfsId);
free(node->directory);
free(node->file_name);
if(node->url != NULL) {
free(node->url->file);
free(node->url->host);
if( node->url->prefix != NULL ) {
free( node->url->prefix);
}
free(node->url);
}
if(node->ipc != NULL) {
free(node->ipc);
}
if( node->stagelocation != NULL ) {
free(node->stagelocation);
}
if(node->ahead != NULL) {
if(node->ahead->buffer != NULL) {
free(node->ahead->buffer);
}
free(node->ahead);
}
if( node->sum != NULL ) {
free( node->sum );
}
m_unlock(&node->mux);
free(node);
}
void node_unplug( struct vsp_node *node)
{
rw_wrlock(&nodeRWlock);
real_node_unplug(node);
rw_unlock(&nodeRWlock);
return;
}
void real_node_unplug( struct vsp_node *node)
{
if(node == NULL) return;
if( node->reference > 0 ) {
dc_debug(DC_INFO, "[%d] reference %d unplug canceled", node->dataFd, node->reference);
return;
}
dc_debug(DC_INFO, "[%d] unpluging node", node->dataFd);
if (node->next != NULL) {
/* we are not a last in the chain */
node->next->prev = node->prev;
}else{
/* we are last in the chain */
lastNode = node->prev;
}
if (node->prev != NULL) {
/* we are not a first in the chain */
node->prev->next = node->next;
}else{
/* we are first in the chain */
vspNode = node->next;
}
}
fdList getAllFD()
{
struct vsp_node *node;
int *all = NULL;
int count;
int nc;
int i;
fdList list;
rw_wrlock(&nodeRWlock);
/* find out houw many fd's at all */
node = vspNode;
count = 0;
while (node != NULL) {
count += node->reference;
node = node->next;
}
if( count > 0 ) {
all = (int *)malloc( count*sizeof(int) );
if( all != NULL ) {
node = vspNode;
nc = 0;
while( node != NULL ) {
for( i = 0; i < node->reference; i++) {
all[nc] = node->fd_set[i];
++nc;
}
node = node->next;
}
}
}
rw_unlock(&nodeRWlock);
list.len = count;
list.fds = all;
return list;
}
|