~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/sockdebug.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* sockdebug.c - Network UPS Tools driver-server socket debugger
 
2
 
 
3
   Copyright (C) 2003  Russell Kroll <rkroll@exploits.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
*/
 
19
 
 
20
 
 
21
#include <fcntl.h>
 
22
#include <stdio.h>
 
23
#include <unistd.h>
 
24
#include <sys/un.h> 
 
25
#include <sys/stat.h>
 
26
#include <sys/types.h>
 
27
#include <sys/socket.h>
 
28
 
 
29
#include "common.h"
 
30
#include "parseconf.h"
 
31
 
 
32
        PCONF_CTX       sock_ctx;
 
33
 
 
34
static void sock_arg(int numarg, char **arg)
 
35
{
 
36
        int     i;
 
37
 
 
38
        printf("numarg=%d : ", numarg);
 
39
 
 
40
        for (i = 0; i < numarg; i++)
 
41
                printf("[%s] ", arg[i]);
 
42
 
 
43
        printf("\n");
 
44
}
 
45
 
 
46
static int socket_connect(const char *sockfn)
 
47
{
 
48
        int     ret, fd;
 
49
        struct  sockaddr_un sa;
 
50
 
 
51
        memset(&sa, '\0', sizeof(sa));
 
52
        sa.sun_family = AF_UNIX;
 
53
        snprintf(sa.sun_path, sizeof(sa.sun_path), "%s", sockfn);
 
54
 
 
55
        fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
56
 
 
57
        if (fd < 0) {
 
58
                perror("socket");
 
59
                exit(1);
 
60
        }
 
61
 
 
62
        ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
 
63
 
 
64
        if (ret < 0) {
 
65
                perror("connect");
 
66
                exit(1);
 
67
        }
 
68
 
 
69
#if 0
 
70
        ret = fcntl(fd, F_GETFL, 0);
 
71
 
 
72
        if (ret < 0) {
 
73
                perror("fcntl(get)");
 
74
                exit(1);
 
75
        }
 
76
 
 
77
        ret = fcntl(fd, F_SETFL, ret | O_NDELAY);
 
78
 
 
79
        if (ret < 0) {
 
80
                perror("fcntl(set)");
 
81
                exit(1);
 
82
        }
 
83
#endif
 
84
 
 
85
        return fd;
 
86
}
 
87
 
 
88
static void read_sock(int fd)
 
89
{
 
90
        int     i, ret;
 
91
        char    buf[SMALLBUF];
 
92
 
 
93
        ret = read(fd, buf, sizeof(buf));
 
94
 
 
95
        if (ret == 0) {
 
96
                fprintf(stderr, "read on socket returned 0\n");
 
97
                exit(0);
 
98
        }
 
99
 
 
100
        if (ret < 0) {
 
101
                perror("read sockfd"); 
 
102
                exit(1);
 
103
        }
 
104
 
 
105
        for (i = 0; i < ret; i++) {
 
106
 
 
107
                switch (pconf_char(&sock_ctx, buf[i])) {
 
108
                        case 1:
 
109
                                sock_arg(sock_ctx.numargs, sock_ctx.arglist);
 
110
                                break;
 
111
 
 
112
                        case -1:
 
113
                                printf("Parse error: [%s]\n", sock_ctx.errmsg);
 
114
                                break;
 
115
                }
 
116
        }
 
117
}
 
118
 
 
119
int main(int argc, char **argv)
 
120
{
 
121
        int     ret, sockfd;
 
122
 
 
123
        if (argc != 2) {
 
124
                fprintf(stderr, "usage: %s <socket name>\n", argv[0]);
 
125
                fprintf(stderr, "       %s /var/state/ups/newapc-ttyS1.newsock\n",
 
126
                        argv[0]);
 
127
                exit(1);
 
128
        }
 
129
 
 
130
        sockfd = socket_connect(argv[1]);
 
131
 
 
132
        printf("connected: fd %d\n", sockfd);
 
133
 
 
134
        pconf_init(&sock_ctx, NULL);
 
135
 
 
136
        for (;;) {
 
137
                struct  timeval tv;
 
138
                fd_set  rfds;
 
139
                int     maxfd;
 
140
 
 
141
                tv.tv_sec = 2;
 
142
                tv.tv_usec = 0;
 
143
                FD_ZERO(&rfds);
 
144
                FD_SET(fileno(stdin), &rfds);
 
145
                FD_SET(sockfd, &rfds);
 
146
 
 
147
                /* paranoia */
 
148
                maxfd = (sockfd > fileno(stdin)) ? sockfd : fileno(stdin);
 
149
 
 
150
                ret = select(maxfd + 1, &rfds, NULL, NULL, &tv);
 
151
 
 
152
                if (FD_ISSET(sockfd, &rfds))
 
153
                        read_sock(sockfd);
 
154
 
 
155
                if (FD_ISSET(fileno(stdin), &rfds)) {
 
156
                        char    buf[SMALLBUF];
 
157
 
 
158
                        fgets(buf, sizeof(buf), stdin);
 
159
 
 
160
                        ret = write(sockfd, buf, strlen(buf));
 
161
 
 
162
                        if (ret != strlen(buf)) {
 
163
                                perror("write to socket");
 
164
                                exit(1);
 
165
                        }
 
166
                }
 
167
        }
 
168
 
 
169
        return 0;
 
170
}