a tiny fix about the path of unixcmd configuration file
[vde.git] / vde-2 / unixcmd / unixcmd.c
blob4750c22fb5c92904c3dbbeb7bfa18875ed7451a6
1 /*
2 * Copyright (C) 2007 - Renzo Davoli, Luca Bigliardi
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <sys/poll.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <getopt.h>
27 #include <fcntl.h>
28 #include <libgen.h>
29 #include <errno.h>
31 #include <config.h>
32 #include <utils/cmdparse.h>
34 #define STD_RC_DIR INSTALLPATH"/etc"
35 #define STD_SOCK_DIR "/var/run"
37 void usage(char *progname){
38 /* TODO: write it better */
39 printf("Usage: %s OPTIONS command\n", progname);
40 printf("\t-s sockname : management socket path\n");
41 printf("\t-f rcfile : configuration path (default is %s/%s)\n", STD_RC_DIR, progname);
42 printf("\t-v : run parse machine in debug mode\n");
45 int main(int argc,char *argv[])
47 struct sockaddr_un sun;
48 int fd, rv;
49 char *rcfile=NULL;
50 char *sockname=NULL;
51 int debug=0;
53 struct utm *utm;
54 struct utm_out *outbuf;
55 struct utm_buf parsebuf;
57 int option_index = 0;
58 static struct option long_options[] = {
59 {"rcfile", 1, 0, 'f'},
60 {"sock", 1, 0, 's'},
61 {"verbose", 0, 0, 'v'},
63 int c;
64 while ((c=getopt_long (argc, argv, "f:s:v",
65 long_options, &option_index)) >= 0)
67 switch (c) {
68 case 'f': rcfile=strdup(optarg); break;
69 case 's': sockname=strdup(optarg); break;
70 case 'v': debug=1; break;
74 if(argc-optind == 0){ usage(argv[0]); return -1; }
76 if (!rcfile) asprintf(&rcfile,"%s/%s",STD_RC_DIR,basename(argv[0]));
77 if( (utm=utm_alloc(rcfile)) == NULL ) { perror("alloc parse machine"); usage(argv[0]); return -1;}
79 if (!sockname) asprintf(&sockname,"%s/%s",STD_SOCK_DIR,basename(argv[0]));
80 sun.sun_family=PF_UNIX;
81 snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",sockname);
82 fd=socket(PF_UNIX,SOCK_STREAM,0);
83 if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0){ perror("nonblock"); return -1; }
84 if( connect(fd,(struct sockaddr *)(&sun),sizeof(sun)) ){ perror("connect"); return -1; }
86 memset(&parsebuf, 0, sizeof(struct utm_buf));
87 outbuf=utmout_alloc();
89 rv=utm_run(utm,&parsebuf,fd,argc-optind,argv+optind,outbuf,debug);
90 if(outbuf->sz) write(1, outbuf->buf, outbuf->sz);
91 utmout_free(outbuf);
93 return rv;