1 /* Copyright 2005,2006,2007 Renzo Davoli - VDE-2
2 * 2007 co-authors Ludovico Gardenghi, Filippo Giunchedi, Luca Bigliardi
3 * --pidfile/-p and cleanup management by Mattia Belletti (C) 2004.
4 * Licensed under the GPLv2
17 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
32 #include <sockutils.h>
39 #ifndef HAVE_OPEN_MEMSTREAM
40 #include <utils/open_memstream.h>
43 static struct swmodule swmi
;
47 static char *pidfile
= NULL
;
48 static char pidfile_path
[PATH_MAX
];
49 static int daemonize
= 0;
50 static unsigned int console_type
=-1;
51 static unsigned int mgmt_ctl
=-1;
52 static unsigned int mgmt_data
=-1;
53 static int mgmt_mode
= 0600;
54 static char *mgmt_socket
= NULL
;
55 static char header
[]="VDE switch V.%s\n(C) Virtual Square Team (coord. R. Davoli) 2005,2006,2007 - GPLv2\n";
56 static char prompt
[]="\nvde$ ";
58 static struct comlist
*clh
=NULL
;
59 static struct comlist
**clt
=&clh
;
62 static struct dbgcl
*dbgclh
=NULL
;
63 static struct dbgcl
**dbgclt
=&dbgclh
;
64 #define MGMTPORTNEW (dl)
65 #define MGMTPORTDEL (dl+1)
66 static struct dbgcl dl
[]= {
67 {"mgmt/+",NULL
,D_MGMT
|D_PLUS
},
68 {"mgmt/-",NULL
,D_MGMT
|D_MINUS
}
72 static struct plugin
*pluginh
=NULL
;
73 static struct plugin
**plugint
=&pluginh
;
76 void addcl(int ncl
,struct comlist
*cl
)
79 for (i
=0;i
<ncl
;i
++,cl
++) {
86 void delcl(int ncl
,struct comlist
*cl
)
89 for (i
=0;i
<ncl
;i
++,cl
++) {
90 register struct comlist
**p
=&clh
;
103 void adddbgcl(int ncl
,struct dbgcl
*cl
)
106 for (i
=0;i
<ncl
;i
++,cl
++) {
113 void deldbgcl(int ncl
,struct dbgcl
*cl
)
116 for (i
=0;i
<ncl
;i
++,cl
++) {
117 register struct dbgcl
**p
=&dbgclh
;
120 if (cl
->fds
) free(cl
->fds
);
121 if (cl
->fun
) free(cl
->fun
);
133 void addplugin(struct plugin
*cl
)
140 void delplugin(struct plugin
*cl
)
142 register struct plugin
**p
=&pluginh
;
154 void printlog(int priority
, const char *format
, ...)
158 va_start (arg
, format
);
161 vsyslog(priority
,format
,arg
);
163 fprintf(stderr
,"%s: ",prog
);
164 vfprintf(stderr
,format
,arg
);
165 fprintf(stderr
,"\n");
171 void printoutc(int fd
, const char *format
, ...)
175 va_start (arg
, format
);
178 printlog(LOG_INFO
,format
,arg
);
181 char outbuf
[MAXCMD
+1];
182 vsnprintf(outbuf
,MAXCMD
,format
,arg
);
184 write(fd
,outbuf
,strlen(outbuf
));
191 void printoutc(FILE *f
, const char *format
, ...)
195 va_start (arg
, format
);
197 vfprintf(f
,format
,arg
);
200 printlog(LOG_INFO
,format
,arg
);
205 static char _dbgnl
='\n';
206 void debugout(struct dbgcl
* cl
, const char *format
, ...)
212 struct iovec iov
[]={{NULL
,0},{NULL
,0},{&_dbgnl
,1}};
214 va_start (arg
, format
);
215 iov
[0].iov_len
=asprintf(&header
,"3%03o %s ",cl
->tag
& 0777,cl
->path
);
216 iov
[0].iov_base
=header
;
217 iov
[1].iov_len
=vasprintf(&msg
,format
,arg
);
221 for (i
=0; i
<cl
->nfds
; i
++)
222 writev(cl
->fds
[i
],iov
,3);
227 void eventout(struct dbgcl
* cl
, ...)
231 for (i
=0; i
<cl
->nfun
; i
++) {
233 (cl
->fun
[i
])(cl
,arg
);
238 int packetfilter(struct dbgcl
* cl
, ...)
244 (void) va_arg(arg
,int); /*port*/
245 (void) va_arg(arg
,char *); /*buf*/
248 for (i
=0; i
<cl
->nfun
&& len
>0; i
++) {
250 int rv
=(cl
->fun
[i
])(cl
,arg
);
262 void setmgmtperm(char *path
)
264 chmod(path
,mgmt_mode
);
267 static int help(FILE *fd
,char *arg
)
271 printoutc(fd
,"%-18s %-15s %s","COMMAND PATH","SYNTAX","HELP");
272 printoutc(fd
,"%-18s %-15s %s","------------","--------------","------------");
273 for (p
=clh
;p
!=NULL
;p
=p
->next
)
274 if (strncmp(p
->path
,arg
,n
) == 0)
275 printoutc(fd
,"%-18s %-15s %s",p
->path
,p
->syntax
,p
->help
);
279 static int handle_cmd(int type
,int fd
,char *inbuf
)
283 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
284 if (*inbuf
!= '\0' && *inbuf
!= '#') {
289 f
=open_memstream(&outbuf
,&outbufsize
);
292 for (p
=clh
;p
!=NULL
&& (p
->doit
==NULL
|| strncmp(p
->path
,inbuf
,strlen(p
->path
))!=0); p
=p
->next
)
296 inbuf
+= strlen(p
->path
);
297 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
298 if (p
->type
& WITHFD
) {
299 if (p
->type
& WITHFILE
) {
300 printoutc(f
,"0000 DATA END WITH '.'");
301 switch(p
->type
& ~(WITHFILE
| WITHFD
)){
302 case NOARG
: rv
=p
->doit(f
,fd
); break;
303 case INTARG
: rv
=p
->doit(f
,fd
,atoi(inbuf
)); break;
304 case STRARG
: rv
=p
->doit(f
,fd
,inbuf
); break;
308 switch(p
->type
& ~WITHFD
){
309 case NOARG
: rv
=p
->doit(fd
); break;
310 case INTARG
: rv
=p
->doit(fd
,atoi(inbuf
)); break;
311 case STRARG
: rv
=p
->doit(fd
,inbuf
); break;
314 } else if (p
->type
& WITHFILE
) {
315 printoutc(f
,"0000 DATA END WITH '.'");
316 switch(p
->type
& ~WITHFILE
){
317 case NOARG
: rv
=p
->doit(f
); break;
318 case INTARG
: rv
=p
->doit(f
,atoi(inbuf
)); break;
319 case STRARG
: rv
=p
->doit(f
,inbuf
); break;
324 case NOARG
: rv
=p
->doit(); break;
325 case INTARG
: rv
=p
->doit(atoi(inbuf
)); break;
326 case STRARG
: rv
=p
->doit(inbuf
); break;
330 if (rv
>= 0 && (rv
> 0 || fd
>= 0))
331 printoutc(f
,"1%03d %s",rv
,strerror(rv
));
334 write(fd
,outbuf
,outbufsize
);
341 static int runscript(int fd
,char *path
)
343 FILE *f
=fopen(path
,"r");
348 while (fgets(buf
,MAXCMD
,f
) != NULL
) {
349 if (strlen(buf
) > 1 && buf
[strlen(buf
)-1]=='\n') buf
[strlen(buf
)-1]= '\0';
351 char *scriptprompt
=NULL
;
352 asprintf(&scriptprompt
,"vde[%s]: %s",path
,buf
);
353 write(fd
,scriptprompt
,strlen(scriptprompt
));
356 handle_cmd(mgmt_data
, fd
, buf
);
362 void loadrcfile(void)
365 runscript(-1,rcfile
);
368 snprintf(path
,PATH_MAX
,"%s/.vderc",getenv("HOME"));
369 if (access(path
,R_OK
) == 0)
372 if (access(STDRCFILE
,R_OK
) == 0)
373 runscript(-1,STDRCFILE
);
378 void mgmtnewfd(int new)
381 if(fcntl(new, F_SETFL
, O_NONBLOCK
) < 0){
382 printlog(LOG_WARNING
,"mgmt fcntl - setting O_NONBLOCK %s",strerror(errno
));
387 add_fd(new,mgmt_data
,-1);
388 EVENTOUT(MGMTPORTNEW
,new);
389 snprintf(buf
,MAXCMD
,header
,PACKAGE_VERSION
);
390 write(new,buf
,strlen(buf
));
391 write(new,prompt
,strlen(prompt
));
395 static int debugdel(int fd
,char *arg
);
397 static char *EOS
="9999 END OF SESSION";
398 static void handle_input(unsigned char type
,int fd
,int revents
,int *unused
)
401 if (type
!= mgmt_ctl
) {
404 if (revents
& POLLIN
) {
405 n
= read(fd
, buf
, sizeof(buf
));
407 printlog(LOG_WARNING
,"Reading from mgmt %s",strerror(errno
));
411 if (type
== console_type
) {
412 printlog(LOG_WARNING
,"EOF on stdin, cleaning up and exiting");
423 if (n
>0 && buf
[n
-1] == '\n') buf
[n
-1] = 0;
424 cmdout
=handle_cmd(type
,(type
==console_type
)?STDOUT_FILENO
:fd
,buf
);
426 write(fd
,prompt
,strlen(prompt
));
428 if(type
==mgmt_data
) {
429 write(fd
,EOS
,strlen(EOS
));
431 EVENTOUT(MGMTPORTDEL
,fd
);
440 } else {/* mgmt ctl */
441 struct sockaddr addr
;
446 new = accept(fd
, &addr
, &len
);
448 printlog(LOG_WARNING
,"mgmt accept %s",strerror(errno
));
451 if(fcntl(new, F_SETFL
, O_NONBLOCK
) < 0){
452 printlog(LOG_WARNING
,"mgmt fcntl - setting O_NONBLOCK %s",strerror(errno
));
457 add_fd(new,mgmt_data
,-1);
458 EVENTOUT(MGMTPORTNEW
,new);
459 snprintf(buf
,MAXCMD
,header
,PACKAGE_VERSION
);
460 write(new,buf
,strlen(buf
));
461 write(new,prompt
,strlen(prompt
));
465 static void save_pidfile()
467 if(pidfile
[0] != '/')
468 strncat(pidfile_path
, pidfile
, PATH_MAX
- strlen(pidfile_path
));
470 strcpy(pidfile_path
, pidfile
);
472 int fd
= open(pidfile_path
,
473 O_WRONLY
| O_CREAT
| O_EXCL
,
474 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
478 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
482 if((f
= fdopen(fd
, "w")) == NULL
) {
483 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
487 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
488 printlog(LOG_ERR
, "Error in writing pidfile");
495 static void cleanup(unsigned char type
,int fd
,int arg
)
498 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
499 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
503 if (type
== mgmt_ctl
&& mgmt_socket
!= NULL
) {
509 #define MGMTMODEARG 0x100
511 static struct option long_options
[] = {
512 {"daemon", 0, 0, 'd'},
513 {"pidfile", 1, 0, 'p'},
514 {"rcfile", 1, 0, 'f'},
516 {"mgmtmode", 1, 0, MGMTMODEARG
},
518 {"debugclients",1,0,'D'},
522 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
524 static void usage(void)
527 "(opts from consmgmt module)\n"
528 " -d, --daemon Daemonize vde_switch once run\n"
529 " -p, --pidfile PIDFILE Write pid of daemon to PIDFILE\n"
530 " -f, --rcfile Configuration file (overrides %s and ~/.vderc)\n"
531 " -M, --mgmt SOCK path of the management UNIX socket\n"
532 " --mgmtmode MODE management UNIX socket access mode (octal)\n"
534 " -D, --debugclients # number of debug clients allowed\n"
539 static int parseopt(int c
, char *optarg
)
547 pidfile
=strdup(optarg
);
550 rcfile
=strdup(optarg
);
553 mgmt_socket
=strdup(optarg
);
556 sscanf(optarg
,"%o",&mgmt_mode
);
564 static void init(void)
567 openlog(basename(prog
), LOG_PID
, 0);
569 syslog(LOG_INFO
,"VDE_SWITCH started");
571 /* add stdin (if tty), connect and data fds to the set of fds we wait for
573 if(isatty(0) && !daemonize
)
575 console_type
=add_type(&swmi
,0);
576 add_fd(0,console_type
,-1);
579 /* saves current path in pidfile_path, because otherwise with daemonize() we
581 if(getcwd(pidfile_path
, PATH_MAX
-1) == NULL
) {
582 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
585 strcat(pidfile_path
, "/");
586 if (daemonize
&& daemon(0, 0)) {
587 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
591 /* once here, we're sure we're the true process which will continue as a
592 * * server: save PID file if needed */
593 if(pidfile
) save_pidfile();
595 if(mgmt_socket
!= NULL
) {
597 struct sockaddr_un sun
;
600 if((mgmtconnfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0){
601 printlog(LOG_ERR
,"mgmt socket: %s",strerror(errno
));
604 if(setsockopt(mgmtconnfd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &one
,
606 printlog(LOG_ERR
,"mgmt setsockopt: %s",strerror(errno
));
609 if(fcntl(mgmtconnfd
, F_SETFL
, O_NONBLOCK
) < 0){
610 printlog(LOG_ERR
,"Setting O_NONBLOCK on mgmt fd: %s",strerror(errno
));
613 sun
.sun_family
= PF_UNIX
;
614 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",mgmt_socket
);
615 if(bind(mgmtconnfd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
616 if((errno
== EADDRINUSE
) && still_used(&sun
)) return;
617 else if(bind(mgmtconnfd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
618 printlog(LOG_ERR
,"mgmt bind %s",strerror(errno
));
622 chmod(sun
.sun_path
,mgmt_mode
);
623 if(listen(mgmtconnfd
, 15) < 0){
624 printlog(LOG_ERR
,"mgmt listen: %s",strerror(errno
));
627 mgmt_ctl
=add_type(&swmi
,0);
628 mgmt_data
=add_type(&swmi
,0);
629 add_fd(mgmtconnfd
,mgmt_ctl
,-1);
633 static int vde_logout()
638 static int vde_shutdown()
640 printlog(LOG_WARNING
,"Shutdown from mgmt command");
644 static int showinfo(FILE *fd
)
646 printoutc(fd
,header
,PACKAGE_VERSION
);
647 printoutc(fd
,"pid %d MAC %02x:%02x:%02x:%02x:%02x:%02x uptime %d",getpid(),
648 switchmac
[0], switchmac
[1], switchmac
[2], switchmac
[3], switchmac
[4], switchmac
[5],
651 printoutc(fd
,"mgmt %s perm 0%03o",mgmt_socket
,mgmt_mode
);
652 printoutc(fd
,"unsent_pktq_len %d",packetq_count());
657 static int debuglist(FILE *f
,int fd
,char *path
)
659 #define DEBUGFORMAT1 "%-22s %-3s %-6s %s"
660 #define DEBUGFORMAT2 "%-22s %03o %-6s %s"
664 printoutc(f
,DEBUGFORMAT1
,"CATEGORY", "TAG", "STATUS", "HELP");
665 printoutc(f
,DEBUGFORMAT1
,"------------","---","------", "----");
666 for (p
=dbgclh
; p
!=NULL
; p
=p
->next
){
667 if (p
->help
&& strncmp(p
->path
, path
, strlen(path
)) == 0) {
668 for (i
=0; i
<p
->nfds
&& p
->fds
[i
] != fd
; i
++)
671 printoutc(f
, DEBUGFORMAT2
, p
->path
, p
->tag
&0777, i
<p
->nfds
? "ON" : "OFF", p
->help
);
677 /* EINVAL -> no matches
678 * EEXIST -> all the matches already include fd
679 * ENOMEM -> fd buffer realloc failed
681 static int debugadd(int fd
,char *path
) {
684 for (p
=dbgclh
; p
!=NULL
; p
=p
->next
) {
685 if (p
->help
&& strncmp(p
->path
, path
, strlen(path
)) == 0) {
687 if (rv
==EINVAL
) rv
=EEXIST
;
688 for(i
=0;i
<p
->nfds
&& (p
->fds
[i
] != fd
); i
++)
692 int newsize
=p
->maxfds
+DBGCLSTEP
;
693 p
->fds
=realloc(p
->fds
,newsize
*sizeof(int));
698 if (rv
!= ENOMEM
) rv
=0;
704 if (rv
!= ENOMEM
) rv
=0;
712 /* EINVAL -> no matches
713 * ENOENT -> all the matches do not include fd
715 static int debugdel(int fd
,char *path
) {
718 for (p
=dbgclh
; p
!=NULL
; p
=p
->next
){
719 if (strncmp(p
->path
, path
, strlen(path
)) == 0) {
721 if (rv
==EINVAL
) rv
=ENOENT
;
722 for(i
=0;i
<p
->nfds
&& (p
->fds
[i
] != fd
); i
++)
725 p
->nfds
--; /* the last one */
726 p
->fds
[i
]=p
->fds
[p
->nfds
]; /* swap it with the deleted element*/
734 int eventadd(int (*fun
)(),char *path
,void *arg
) {
737 for (p
=dbgclh
; p
!=NULL
; p
=p
->next
) {
738 if (strncmp(p
->path
, path
, strlen(path
)) == 0) {
740 if (rv
==EINVAL
) rv
=EEXIST
;
741 for(i
=0;i
<p
->nfun
&& (p
->fun
[i
] != fun
); i
++)
745 int newsize
=p
->maxfun
+DBGCLSTEP
;
746 p
->fun
=realloc(p
->fun
,newsize
*sizeof(int));
747 p
->funarg
=realloc(p
->funarg
,newsize
*sizeof(void *));
748 if (p
->fun
&& p
->funarg
) {
753 if (rv
!= ENOMEM
) rv
=0;
759 if (rv
!= ENOMEM
) rv
=0;
767 /* EINVAL -> no matches
768 * ENOENT -> all the matches do not include fun
770 int eventdel(int (*fun
)(),char *path
,void *arg
) {
773 for (p
=dbgclh
; p
!=NULL
; p
=p
->next
){
774 if (strncmp(p
->path
, path
, strlen(path
)) == 0) {
776 if (rv
==EINVAL
) rv
=ENOENT
;
777 for(i
=0;i
<p
->nfun
&& (p
->fun
[i
] != fun
) && (p
->funarg
[i
] != arg
); i
++)
780 p
->nfun
--; /* the last one */
781 p
->fun
[i
]=p
->fun
[p
->nfun
]; /* swap it with the deleted element*/
792 static int pluginlist(FILE *f
,char *arg
)
794 #define PLUGINFMT "%-22s %s"
797 printoutc(f
,PLUGINFMT
,"NAME", "HELP");
798 printoutc(f
,PLUGINFMT
,"------------","----");
799 for (p
=pluginh
; p
!=NULL
; p
=p
->next
){
800 if (strncmp(p
->name
, arg
, strlen(arg
)) == 0) {
801 printoutc(f
,PLUGINFMT
,p
->name
,p
->help
);
808 static int pluginadd(char *arg
) {
812 if ((handle
=dlopen(arg
,RTLD_LAZY
)) != NULL
) {
813 if ((p
=(struct plugin
*) dlsym(handle
,"vde_plugin_data")) != NULL
) {
814 if (p
->handle
!= NULL
) { /* this dyn library is already loaded*/
829 static int plugindel(char *arg
) {
831 struct plugin
**p
=&pluginh
;
833 if (strncmp((*p
)->name
, arg
, strlen(arg
)) == 0
834 && ((*p
)->handle
!= NULL
)) {
835 struct plugin
*this=*p
;
837 dlclose(this->handle
);
846 static struct comlist cl
[]={
847 {"help","[arg]","Help (limited to arg when specified)",help
,STRARG
| WITHFILE
},
848 {"logout","","logout from this mgmt terminal",vde_logout
,NOARG
},
849 {"shutdown","","shutdown of the switch",vde_shutdown
,NOARG
},
850 {"showinfo","","show switch version and info",showinfo
,NOARG
|WITHFILE
},
851 {"load","path","load a configuration script",runscript
,STRARG
|WITHFD
},
853 {"debug","============","DEBUG MENU",NULL
,NOARG
},
854 {"debug/list","","list debug categories",debuglist
,STRARG
|WITHFILE
|WITHFD
},
855 {"debug/add","dbgpath","enable debug info for a given category",debugadd
,WITHFD
|STRARG
},
856 {"debug/del","dbgpath","disable debug info for a given category",debugdel
,WITHFD
|STRARG
},
859 {"plugin","============","PLUGINS MENU",NULL
,NOARG
},
860 {"plugin/list","","list plugins",pluginlist
,STRARG
|WITHFILE
},
861 {"plugin/add","library","load a plugin",pluginadd
,STRARG
},
862 {"plugin/del","name","unload a plugin",plugindel
,STRARG
},
866 void start_consmgmt(void)
868 swmi
.swmname
="console-mgmt";
869 swmi
.swmnopts
=Nlong_options
;
870 swmi
.swmopts
=long_options
;
872 swmi
.parseopt
=parseopt
;
874 swmi
.handle_input
=handle_input
;
875 swmi
.cleanup
=cleanup
;