summaryrefslogtreecommitdiff
path: root/src/pmdas/trace/src/client.c
blob: c3224dd9a1df1c55a21cb7a57e45e96b2fe820b5 (plain)
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
/*
 * Copyright (c) 2012-2013 Red Hat.
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pmapi.h"
#include "impl.h"
#include "trace_dev.h"
#include "client.h"
#include "comms.h"

extern fd_set	fds;

int		nclients;		/* number of entries in array */
int		maxfd;			/* largest fd currently in use */
client_t	*clients;		/* array of clients */

#define MIN_CLIENTS_ALLOC 8
static int	clientsize;

static int newClient(void);

client_t *
acceptClient(int reqfd)
{
    int		i, fd;
    __pmSockLen	addrlen;

    i = newClient();
    addrlen = __pmSockAddrSize();
    fd = __pmAccept(reqfd, clients[i].addr, &addrlen);
    if (fd == -1) {
	__pmNotifyErr(LOG_ERR, "acceptClient(%d) accept: %s",
		reqfd, netstrerror());
	return NULL;
    }
    if (fd > maxfd)
	maxfd = fd;
    FD_SET(fd, &fds);
    clients[i].fd = fd;
    clients[i].status.connected = 1;
    clients[i].status.padding = 0;
    clients[i].status.protocol = 1;	/* sync */
    return &clients[i];
}

static int
newClient(void)
{
    int i, j;

    for (i = 0; i < nclients; i++)
	if (!clients[i].status.connected)
	    break;

    if (i == clientsize) {
	clientsize = clientsize ? clientsize * 2 : MIN_CLIENTS_ALLOC;
	clients = (client_t *) realloc(clients, sizeof(client_t)*clientsize);
	if (clients == NULL)
	    __pmNoMem("newClient", sizeof(client_t)*clientsize, PM_FATAL_ERR);
	for (j = i; j < clientsize; j++)
	    clients[j].addr = NULL;
    }
    clients[i].addr = __pmSockAddrAlloc();
    if (clients[i].addr == NULL)
	__pmNoMem("newClient", __pmSockAddrSize(), PM_FATAL_ERR);
    if (i >= nclients)
	nclients = i + 1;
    return i;
}

void
deleteClient(client_t *cp)
{
    int	i;

    for (i = 0; i < nclients; i++)
	if (cp == &clients[i])
	    break;

    if (i == nclients) {
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL0) {
	    __pmNotifyErr(LOG_ERR, "deleteClient: tried to delete non-existent client");
	}
#endif
	return;
    }
    if (cp->fd != -1) {
	__pmtracenomoreinput(cp->fd);
	FD_CLR(cp->fd, &fds);
	close(cp->fd);
    }
    if (cp->fd == maxfd) {
	maxfd = -1;
	for (i = 0; i < nclients; i++)
	    if (clients[i].fd > maxfd)
		maxfd = clients[i].fd;
    }
    __pmSockAddrFree(cp->addr);
    cp->addr = NULL;
    cp->status.connected = 0;
    cp->status.padding = 0;
    cp->status.protocol = 1;	/* sync */
#ifdef PCP_DEBUG
	if (pmDebug & DBG_TRACE_APPL0)
	    __pmNotifyErr(LOG_DEBUG, "deleteClient: client removed (fd=%d)", cp->fd);
#endif
    cp->fd = -1;
}

void
showClients(void)
{
    int i;

    fprintf(stderr, "%s: %d connected clients:\n", pmProgname, nclients);
    fprintf(stderr, "     fd  type   conn  client connection from\n"
		    "     ==  =====  ====  ======================\n");
    for (i=0; i < nclients; i++) {
	char *hostName;
	char *hostAddr;

	fprintf(stderr, "    %3d", clients[i].fd);
	fprintf(stderr, "  %s  ", clients[i].status.protocol == 1 ? "sync ":"async");
	fprintf(stderr, "%s  ", clients[i].status.connected == 1 ? "up  ":"down");
	hostName = __pmGetNameInfo(clients[i].addr);
	if (hostName == NULL) {
	    hostAddr = __pmSockAddrToString(clients[i].addr);
	    fprintf(stderr, "%s", hostAddr);
	    free(hostAddr);
	} else {
	    fprintf(stderr, "%-40.40s", hostName);
	    free(hostName);
	}
	if (clients[i].denyOps != 0) {
	    fprintf(stderr, "  ");
	    if (clients[i].denyOps & TR_OP_SEND)
		fprintf(stderr, "send ");
        }

	fputc('\n', stderr);
    }
    fputc('\n', stderr);
}