1
/* sockdebug.c - Network UPS Tools driver-server socket debugger
3
Copyright (C) 2003 Russell Kroll <rkroll@exploits.org>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
#include <sys/types.h>
27
#include <sys/socket.h>
30
#include "parseconf.h"
34
static void sock_arg(int numarg, char **arg)
38
printf("numarg=%d : ", numarg);
40
for (i = 0; i < numarg; i++)
41
printf("[%s] ", arg[i]);
46
static int socket_connect(const char *sockfn)
49
struct sockaddr_un sa;
51
memset(&sa, '\0', sizeof(sa));
52
sa.sun_family = AF_UNIX;
53
snprintf(sa.sun_path, sizeof(sa.sun_path), "%s", sockfn);
55
fd = socket(AF_UNIX, SOCK_STREAM, 0);
62
ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
70
ret = fcntl(fd, F_GETFL, 0);
77
ret = fcntl(fd, F_SETFL, ret | O_NDELAY);
88
static void read_sock(int fd)
93
ret = read(fd, buf, sizeof(buf));
96
fprintf(stderr, "read on socket returned 0\n");
101
perror("read sockfd");
105
for (i = 0; i < ret; i++) {
107
switch (pconf_char(&sock_ctx, buf[i])) {
109
sock_arg(sock_ctx.numargs, sock_ctx.arglist);
113
printf("Parse error: [%s]\n", sock_ctx.errmsg);
119
int main(int argc, char **argv)
124
fprintf(stderr, "usage: %s <socket name>\n", argv[0]);
125
fprintf(stderr, " %s /var/state/ups/newapc-ttyS1.newsock\n",
130
sockfd = socket_connect(argv[1]);
132
printf("connected: fd %d\n", sockfd);
134
pconf_init(&sock_ctx, NULL);
144
FD_SET(fileno(stdin), &rfds);
145
FD_SET(sockfd, &rfds);
148
maxfd = (sockfd > fileno(stdin)) ? sockfd : fileno(stdin);
150
ret = select(maxfd + 1, &rfds, NULL, NULL, &tv);
152
if (FD_ISSET(sockfd, &rfds))
155
if (FD_ISSET(fileno(stdin), &rfds)) {
158
fgets(buf, sizeof(buf), stdin);
160
ret = write(sockfd, buf, strlen(buf));
162
if (ret != strlen(buf)) {
163
perror("write to socket");