Tomato 1.26 beta (1786)
[tomato.git] / release / src / router / rp-l2tp / handlers / cmd-control.c
blobbec578cedd71a9a5c7aa7a5b7246e3cd9d14bcb3
1 /***********************************************************************
3 * cmd-control.c
5 * Simple command-line program for sending commands to L2TP daemon
7 * Copyright (C) 2002 by Roaring Penguin Software Inc.
9 * This software may be distributed under the terms of the GNU General
10 * Public License, Version 2, or (at your option) any later version.
12 * LIC: GPL
14 ***********************************************************************/
16 static char const RCSID[] =
17 "$Id: cmd-control.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
18 #include <stdio.h>
19 #include <sys/socket.h>
20 #include <sys/un.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <stdlib.h>
24 #include <syslog.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <sys/uio.h>
31 /**********************************************************************
32 * %FUNCTION: send_cmd
33 * %ARGUMENTS:
34 * cmd -- command to send to server
35 * %RETURNS:
36 * file descriptor for channel to server
37 * %DESCRIPTION:
38 * Sends a command to the server
39 ***********************************************************************/
40 static int
41 send_cmd(char const *cmd)
43 struct sockaddr_un addr;
44 int fd;
45 struct iovec v[2];
47 /* Do not send zero-length command */
48 if (!*cmd) {
49 errno = ESPIPE;
50 return -1;
53 memset(&addr, 0, sizeof(addr));
54 addr.sun_family = AF_LOCAL;
55 strncpy(addr.sun_path, "/var/run/l2tpctrl", sizeof(addr.sun_path) - 1);
57 fd = socket(AF_LOCAL, SOCK_STREAM, 0);
58 if (fd < 0) {
59 perror("socket");
60 return -1;
62 if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
63 perror("connect");
64 close(fd);
65 return -1;
67 v[0].iov_base = (char *) cmd;
68 v[0].iov_len = strlen(cmd);
69 v[1].iov_base = "\n";
70 v[1].iov_len = 1;
71 writev(fd, v, 2);
72 return fd;
75 int
76 main(int argc, char *argv[])
78 int fd;
79 int n;
80 char buf[4096];
82 if (argc != 2) {
83 fprintf(stderr, "Usage: %s command\n", argv[0]);
84 return 1;
87 fd = send_cmd(argv[1]);
88 if (fd < 0) {
89 return 1;
92 for(;;) {
93 n = read(fd, buf, sizeof(buf));
94 if (n < 0) {
95 perror("read");
96 printf("\n");
97 close(fd);
98 return 1;
100 if (n == 0) {
101 printf("\n");
102 close(fd);
103 return 0;
105 write(1, buf, n);