Several BugFixes (see bug-reports from "accounts")
[vde.git] / vde-2 / src / unixcmd.c
blob83424d80d6342ac45d14dda8ca45ab4e194d3468
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/socket.h>
24 #include <sys/un.h>
25 #include <getopt.h>
26 #include <fcntl.h>
27 #include <libgen.h>
28 #include <errno.h>
30 #include <config.h>
31 #include <vde.h>
32 #include <vdecommon.h>
34 void usage(char *progname){
35 /* TODO: write it better */
36 printf("Usage: %s OPTIONS command\n", progname);
37 printf("\t-s sockname management socket path (default is %s/%s)\n", VDE_SOCK_DIR, basename(progname));
38 printf("\t-f rcfile configuration path (default is %s/%s)\n", VDE_RC_DIR, basename(progname));
39 printf("\t-v run parse machine in debug mode\n");
42 int main(int argc,char *argv[])
44 struct sockaddr_un sun;
45 int fd, rv;
46 char *rcfile=NULL;
47 char *sockname=NULL;
48 int debug=0;
50 struct utm *utm;
51 struct utm_out *outbuf;
52 struct utm_buf parsebuf;
54 int option_index = 0;
55 static struct option long_options[] = {
56 {"rcfile", 1, 0, 'f'},
57 {"sock", 1, 0, 's'},
58 {"verbose", 0, 0, 'v'},
60 int c;
61 while ((c=getopt_long (argc, argv, "f:s:v",
62 long_options, &option_index)) >= 0)
64 switch (c) {
65 case 'f': rcfile=strdup(optarg); break;
66 case 's': sockname=strdup(optarg); break;
67 case 'v': debug=1; break;
71 if(argc-optind == 0){ usage(argv[0]); return -1; }
73 if (!rcfile) asprintf(&rcfile,"%s/%s",VDE_RC_DIR,basename(argv[0]));
74 if( (utm=utm_alloc(rcfile)) == NULL ) { perror("alloc parse machine"); usage(argv[0]); return -1;}
76 if (!sockname) asprintf(&sockname,"%s/%s",VDE_SOCK_DIR,basename(argv[0]));
77 sun.sun_family=PF_UNIX;
78 snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",sockname);
79 fd=socket(PF_UNIX,SOCK_STREAM,0);
80 if(fcntl(fd, F_SETFL, O_NONBLOCK) < 0){ perror("nonblock"); return -1; }
81 if( connect(fd,(struct sockaddr *)(&sun),sizeof(sun)) ){ perror("connect"); return -1; }
83 memset(&parsebuf, 0, sizeof(struct utm_buf));
84 outbuf=utmout_alloc();
86 rv=utm_run(utm,&parsebuf,fd,argc-optind,argv+optind,outbuf,debug);
87 if(outbuf->sz) write(1, outbuf->buf, outbuf->sz);
88 utmout_free(outbuf);
89 close(fd);
91 return rv;