summaryrefslogtreecommitdiffstats
path: root/usr.sbin/pccard/pccardd/server.c
blob: d5a271fb5e425acedb8a91f5baccdb134f76b2bb (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
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
/*
 *	pccardd UNIX-domain socket interface
 *	Copyright (C) 1996 by Tatsumi Hosokawa <hosokawa@mt.cs.keio.ac.jp>
 *
 * $Id: server.c,v 1.3 1999/02/07 08:02:44 kuriyama Exp $
 */

#ifndef lint
static const char rcsid[] =
  "$FreeBSD$";
#endif /* not lint */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <setjmp.h>

#include "cardd.h"

static void
cardnum(char *buf)
{
	int     i = 0;
	struct slot *sp;

	for (sp = slots; sp; sp = sp->next)
		i++;
	if (i > MAXSLOT)
		i = MAXSLOT;
	sprintf(buf, "%2d", i);
}

static struct slot *
find_slot(int slot)
{
	struct slot *sp;

	/* Search the list until we find the slot or get to the end */
	for (sp = slots; sp && sp->slot != slot; sp = sp->next)
		continue;

	return ( sp );
}

static void
cardname(char *buf, int slot)
{
	struct slot *sp;
	char   *manuf, *vers, *drv, *stat;

	/* Look for the slot */
	if ( (sp = find_slot(slot)) == NULL)
		return;

	/* Fill in the information in the buff */
	if (sp->cis) {

		manuf = sp->cis->manuf;
		vers = sp->cis->vers;
		if (sp->config && sp->config->driver &&
		    sp->config->driver->name)
			drv = sp->config->driver->name;
		else
			drv = "";
	} else
		manuf = vers = drv = "";

	switch (sp->state) {
	case empty:
		stat = "0";
		break;
	case filled:
		stat = "1";
		break;
	case inactive:
		stat = "2";
		break;
	default:
		stat = "9";
	}
	sprintf(buf, "%d~%s~%s~%s~%s", slot, manuf, vers, drv, stat);
}

static void
cardpwr(int slot, int pwon)
{
	struct slot *sp;

	/* Look for the slot */
	if ( (sp = find_slot(slot)) == NULL)
		return;

	if (ioctl(sp->fd, PIOCSVIR, &pwon) < 0)
		logerr("invaild arguments for cardpwr");
}

static int sock = 0;
static int slen = 0;
static struct sockaddr_un sun;

void
set_socket(int s)
{
	sock = s;
}

void
stat_changed(struct slot *sp)
{
	int     len;
	char    buf[512];

	if (!slen)
		return;

	cardname(buf, sp->slot);
	len = strlen(buf);
	if (sendto(sock, buf, len, 0, (struct sockaddr *) & sun, slen) != len) {
		logerr("sendto failed");
		slen = 0;
	}
}

void
process_client(void)
{
	char    buf[512], obuf[512];
	int     len;
	int     snum;

	if (!sock)
		return;
	slen = sizeof(sun);
	len = recvfrom(sock, buf, sizeof(buf),
		       0, (struct sockaddr *)&sun, &slen);
	if (len < 0)
		logerr("recvfrom failed");
	buf[len] = '\0';
	obuf[0] = '\0';
	switch (buf[0]) {	/* Protocol implementation */
	case 'S':	/* How many slots? */
		cardnum(obuf);
		break;
	case 'N':	/* Card name request */
		sscanf(buf + 1, "%d", &snum);
		if (snum >= 0 && snum <= MAXSLOT)
			cardname(obuf, snum);
		else 
			logerr("Illegal slot requests for N command");
		break;
	case 'P':	/* Virtual insertion request */
		sscanf(buf + 1, "%d", &snum);
		if (snum >= 0 && snum <= MAXSLOT) {
			logmsg("slot %d: spring has come", snum);
			cardpwr(snum, 1);
		} else
			logerr("Illegal slot requests for P command");
		break;
	case 'Q':	/* Virtual removal request */
		sscanf(buf + 1, "%d", &snum);
		if (snum >= 0 && snum <= MAXSLOT) {
			logmsg("slot %d: hibernation", snum);
			cardpwr(snum, 0);
		} else
			logerr("Illegal slot requests for Q command");
		break;
	default:
		logerr("Unknown control message from socket");
		break;
	}
	len = strlen(obuf);
	if (len) {
		if (sendto(sock, obuf, len, 0, (struct sockaddr *)&sun, slen)
		    != len) {
			logerr("sendto failed");
			slen = 0;
		}
	} else if (sendto(sock, 0, 0, 0, (struct sockaddr *)&sun, slen)
		   != len) {
			logerr("sendto failed");
			slen = 0;
	}
}
OpenPOWER on IntegriCloud