1 /* Copyright 2005 Renzo Davoli - VDE-2
2 * --pidfile/-p and cleanup management by Mattia Belletti (C) 2004.
3 * Licensed under the GPLv2
15 #include <sys/types.h>
17 #include <sys/ioctl.h>
18 #include <sys/socket.h>
28 #include <sockutils.h>
34 static struct swmodule swmi
;
38 static char *pidfile
= NULL
;
39 static char pidfile_path
[_POSIX_PATH_MAX
];
40 static int daemonize
= 0;
41 static unsigned int console_type
=-1;
42 static unsigned int mgmt_ctl
=-1;
43 static unsigned int mgmt_data
=-1;
44 static int mgmt_mode
= 0600;
45 static char *mgmt_socket
= NULL
;
46 static char header
[]="VDE switch V.%s\n(C) R.Davoli 2005 - GPLv2\n";
47 static char prompt
[]="\nvde: ";
49 static struct comlist
*clh
=NULL
;
51 void addcl(int ncl
,struct comlist
*cl
)
54 static struct comlist
**clt
=&clh
;
55 for (i
=0;i
<ncl
;i
++,cl
++) {
62 void printlog(int priority
, const char *format
, ...)
66 va_start (arg
, format
);
69 vsyslog(priority
,format
,arg
);
71 fprintf(stderr
,"%s: ",prog
);
72 vfprintf(stderr
,format
,arg
);
78 void printoutc(int fd
, const char *format
, ...)
82 va_start (arg
, format
);
84 printlog(LOG_INFO
,format
,arg
);
86 char outbuf
[MAXCMD
+1];
87 vsnprintf(outbuf
,MAXCMD
,format
,arg
);
89 write(fd
,outbuf
,strlen(outbuf
));
93 void setmgmtperm(char *path
)
95 chmod(path
,mgmt_mode
);
98 static int help(int fd
,char *arg
)
102 printoutc(fd
,"%-18s %-15s %s","COMMAND PATH","SYNTAX","HELP");
103 printoutc(fd
,"%-18s %-15s %s","------------","--------------","------------");
104 for (p
=clh
;p
!=NULL
;p
=p
->next
)
105 if (strncmp(p
->path
,arg
,n
) == 0)
106 printoutc(fd
,"%-18s %-15s %s",p
->path
,p
->syntax
,p
->help
);
110 static int handle_cmd(int type
,int fd
,char *inbuf
)
114 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
115 if (*inbuf
!= '\0' && *inbuf
!= '#') {
116 for (p
=clh
;p
!=NULL
&& (p
->doit
==NULL
|| strncmp(p
->path
,inbuf
,strlen(p
->path
))!=0); p
=p
->next
)
120 inbuf
+= strlen(p
->path
);
121 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
122 if (p
->type
& WITHFD
) {
123 printoutc(fd
,"0000 DATA END WITH '.'");
124 switch(p
->type
& ~WITHFD
){
125 case NOARG
: rv
=p
->doit(fd
); break;
126 case INTARG
: rv
=p
->doit(fd
,atoi(inbuf
)); break;
127 case STRARG
: rv
=p
->doit(fd
,inbuf
); break;
132 case NOARG
: rv
=p
->doit(); break;
133 case INTARG
: rv
=p
->doit(atoi(inbuf
)); break;
134 case STRARG
: rv
=p
->doit(inbuf
); break;
138 if (rv
>= 0 && (rv
> 0 || fd
>= 0))
139 printoutc(fd
,"1%03d %s",rv
,strerror(rv
));
144 static int runscript(int fd
,char *path
)
146 FILE *f
=fopen(path
,"r");
151 while (fgets(buf
,MAXCMD
,f
) != NULL
) {
152 if (strlen(buf
) > 1 && buf
[strlen(buf
)-1]=='\n') buf
[strlen(buf
)-1]= '\0';
153 if (fd
>= 0) printoutc(fd
,"vde[%s]: %s",path
,buf
);
154 handle_cmd(mgmt_data
, fd
, buf
);
160 void loadrcfile(void)
163 runscript(-1,rcfile
);
166 snprintf(path
,PATH_MAX
,"%s/.vderc",getenv("HOME"));
167 if (access(path
,R_OK
) == 0)
170 if (access(STDRCFILE
,R_OK
) == 0)
171 runscript(-1,STDRCFILE
);
176 static void handle_input(unsigned char type
,int fd
,int revents
,int *unused
)
179 if (type
!= mgmt_ctl
) {
182 if (revents
& POLLIN
) {
183 n
= read(fd
, buf
, sizeof(buf
));
185 printlog(LOG_WARNING
,"Reading from mgmt %s",strerror(errno
));
189 if (type
== console_type
) {
190 printlog(LOG_WARNING
,"EOF on stdin, cleaning up and exiting");
198 if (n
>0 && buf
[n
-1] == '\n') buf
[n
-1] = 0;
199 cmdout
=handle_cmd(type
,(type
==console_type
)?STDOUT_FILENO
:fd
,buf
);
201 write(fd
,prompt
,strlen(prompt
));
203 if(type
==mgmt_data
) {
204 printoutc(fd
,"9999 END OF SESSION");
211 } else {/* mgmt ctl */
212 struct sockaddr addr
;
217 new = accept(fd
, &addr
, &len
);
219 printlog(LOG_WARNING
,"mgmt accept %s",strerror(errno
));
222 if(fcntl(new, F_SETFL
, O_NONBLOCK
) < 0){
223 printlog(LOG_WARNING
,"mgmt fcntl - setting O_NONBLOCK %s",strerror(errno
));
228 add_fd(new,mgmt_data
,-1);
229 snprintf(buf
,MAXCMD
,header
,PACKAGE_VERSION
);
230 write(new,buf
,strlen(buf
));
231 write(new,prompt
,strlen(prompt
));
235 static void save_pidfile()
237 if(pidfile
[0] != '/')
238 strncat(pidfile_path
, pidfile
, PATH_MAX
- strlen(pidfile_path
));
240 strcpy(pidfile_path
, pidfile
);
242 int fd
= open(pidfile_path
,
243 O_WRONLY
| O_CREAT
| O_EXCL
,
244 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
248 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
252 if((f
= fdopen(fd
, "w")) == NULL
) {
253 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
257 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
258 printlog(LOG_ERR
, "Error in writing pidfile");
265 static void cleanup(unsigned char type
,int fd
,int arg
)
268 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
269 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
273 if (type
== mgmt_ctl
&& mgmt_socket
!= NULL
) {
279 #define MGMTMODEARG 0x100
281 static struct option long_options
[] = {
282 {"daemon", 0, 0, 'd'},
283 {"pidfile", 1, 0, 'p'},
284 {"rcfile", 1, 0, 'f'},
286 {"mgmtmode", 1, 0, MGMTMODEARG
}
289 #define Nlong_options (sizeof(long_options)/sizeof(struct option));
291 static void usage(void)
294 "(opts from consmgmt module)\n"
295 " -d, --daemon Daemonize vde_switch once run\n"
296 " -p, --pidfile PIDFILE Write pid of daemon to PIDFILE\n"
297 " -f, --rcfile Configuration file (overrides %s and ~/.vderc)\n"
298 " -M, --mgmt SOCK path of the management UNIX socket\n"
299 " --mgmtmode MODE management UNIX socket access mode (octal)\n"
303 static int parseopt(int c
, char *optarg
)
311 pidfile
=strdup(optarg
);
314 rcfile
=strdup(optarg
);
317 mgmt_socket
=strdup(optarg
);
320 sscanf(optarg
,"%o",&mgmt_mode
);
328 static void init(void)
331 openlog(basename(prog
), LOG_PID
, 0);
333 syslog(LOG_INFO
,"VDE_SWITCH started");
335 /* add stdin (if tty), connect and data fds to the set of fds we wait for
337 if(isatty(0) && !daemonize
)
339 console_type
=add_type(&swmi
,0);
340 add_fd(0,console_type
,-1);
343 /* saves current path in pidfile_path, because otherwise with daemonize() we
345 if(getcwd(pidfile_path
, PATH_MAX
-1) == NULL
) {
346 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
349 strcat(pidfile_path
, "/");
350 if (daemonize
&& daemon(0, 1)) {
351 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
355 /* once here, we're sure we're the true process which will continue as a
356 * * server: save PID file if needed */
357 if(pidfile
) save_pidfile();
359 if(mgmt_socket
!= NULL
) {
361 struct sockaddr_un sun
;
364 if((mgmtconnfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0){
365 printlog(LOG_ERR
,"mgmt socket: %s",strerror(errno
));
368 if(setsockopt(mgmtconnfd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &one
,
370 printlog(LOG_ERR
,"mgmt setsockopt: %s",strerror(errno
));
373 if(fcntl(mgmtconnfd
, F_SETFL
, O_NONBLOCK
) < 0){
374 printlog(LOG_ERR
,"Setting O_NONBLOCK on mgmt fd: %s",strerror(errno
));
377 sun
.sun_family
= PF_UNIX
;
378 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",mgmt_socket
);
379 if(bind(mgmtconnfd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
380 if((errno
== EADDRINUSE
) && still_used(&sun
)) return;
381 else if(bind(mgmtconnfd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
382 printlog(LOG_ERR
,"mgmt bind %s",strerror(errno
));
386 chmod(sun
.sun_path
,mgmt_mode
);
387 if(listen(mgmtconnfd
, 15) < 0){
388 printlog(LOG_ERR
,"mgmt listen: %s",strerror(errno
));
391 mgmt_ctl
=add_type(&swmi
,0);
392 mgmt_data
=add_type(&swmi
,0);
393 add_fd(mgmtconnfd
,mgmt_ctl
,-1);
397 static int vde_logout()
402 static int vde_shutdown()
404 printlog(LOG_WARNING
,"Shutdown from mgmt command");
408 static int showinfo(int fd
)
410 printoutc(fd
,header
,PACKAGE_VERSION
);
411 printoutc(fd
,"pid %d MAC %02x:%02x:%02x:%02x:%02x:%02x uptime %d",getpid(),
412 switchmac
[0], switchmac
[1], switchmac
[2], switchmac
[3], switchmac
[4], switchmac
[5],
415 printoutc(fd
,"mgmt %s perm 0%03o",mgmt_socket
,mgmt_mode
);
419 static struct comlist cl
[]={
420 {"help","[arg]","Help (limited to arg when specified)",help
,STRARG
| WITHFD
},
421 {"logout","","logout from this mgmt terminal",vde_logout
,NOARG
},
422 {"shutdown","","shutdown of the switch",vde_shutdown
,NOARG
},
423 {"showinfo","","show switch version and info",showinfo
,NOARG
|WITHFD
},
424 {"load","path","load a configuration script",runscript
,STRARG
|WITHFD
},
427 void start_consmgmt(void)
429 swmi
.swmname
="console-mgmt";
430 swmi
.swmnopts
=Nlong_options
;
431 swmi
.swmopts
=long_options
;
433 swmi
.parseopt
=parseopt
;
435 swmi
.handle_input
=handle_input
;
436 swmi
.cleanup
=cleanup
;