1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/* Copyright 2001 Jeff Dike and others
* Licensed under the GPL
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <readline/readline.h>
#include <readline/history.h>
static char uml_name[11];
static struct sockaddr_un sun;
static int do_switch(char *file, char *name)
{
struct stat buf;
if(stat(file, &buf) == -1){
fprintf(stderr, "Warning: couldn't stat file: %s - ", file);
perror("");
return(-1);
}
sun.sun_family = AF_UNIX;
strncpy(sun.sun_path, file, sizeof(sun.sun_path));
strncpy(uml_name, name, sizeof(uml_name));
return(0);
}
static int switch_common(char *name)
{
char file[MAXPATHLEN + 1], dir[MAXPATHLEN + 1], tmp[MAXPATHLEN + 1], *home;
int try_file = 1;
if((home = getenv("HOME")) != NULL){
snprintf(dir, sizeof(dir), "%s/.uml", home);
snprintf(file, sizeof(file), "%s/%s/mconsole", dir, name);
if(strncmp(name, dir, strlen(dir))){
if(!do_switch(file, name)) return(0);
try_file = 0;
}
}
snprintf(tmp, sizeof(tmp), "/tmp/uml/%s/mconsole", name);
if(strncmp(name, "/tmp/uml/", strlen("/tmp/uml/"))){
if(!do_switch(tmp, name)) return(0);
}
if(!do_switch(name, name)) return(0);
if(!try_file) return(-1);
return(do_switch(file, name));
}
#define MCONSOLE_MAGIC (0xcafebabe)
#define MCONSOLE_MAX_DATA (512)
#define MCONSOLE_VERSION (1)
#define MIN(a,b) ((a)<(b) ? (a):(b))
struct mconsole_request {
unsigned long magic;
int version;
int len;
char data[MCONSOLE_MAX_DATA];
};
struct mconsole_reply {
int err;
int more;
int len;
char data[MCONSOLE_MAX_DATA];
};
static void default_cmd(int fd, char *command)
{
struct mconsole_request request;
struct mconsole_reply reply;
char name[128];
int n;
if((sscanf(command, "%128[^: \f\n\r\t\v]:", name) == 1) &&
(*(name + 1) == ':')){
if(switch_common(name)) return;
command = strchr(command, ':');
*command++ = '\0';
while(isspace(*command)) command++;
}
request.magic = MCONSOLE_MAGIC;
request.version = MCONSOLE_VERSION;
request.len = MIN(strlen(command), sizeof(reply.data) - 1);
strncpy(request.data, command, request.len);
request.data[request.len] = '\0';
if(sendto(fd, &request, sizeof(request), 0, (struct sockaddr *) &sun,
sizeof(sun)) < 0){
fprintf(stderr, "Sending command to '%s' : ", sun.sun_path);
perror("");
return;
}
do {
int len = sizeof(sun);
n = recvfrom(fd, &reply, sizeof(reply), 0, (struct sockaddr *) &sun, &len);
if(n < 0){
perror("recvmsg");
return;
}
if(reply.err) printf("ERR ");
else printf("OK ");
printf("%s", reply.data);
} while(reply.more);
printf("\n");
}
static void help_cmd(int fd, char *command)
{
default_cmd(fd, command);
printf("Additional local mconsole commands:\n");
printf(" quit - Quit mconsole\n");
printf(" switch <socket-name> - Switch control to the given machine\n");
}
static void switch_cmd(int fd, char *command)
{
char *ptr;
ptr = &command[strlen("switch")];
while(isspace(*ptr)) ptr++;
if(switch_common(ptr)) return;
printf("Switched to '%s'\n", ptr);
}
static void quit_cmd(int fd, char *command)
{
exit(0);
}
struct cmd {
char *command;
void (*proc)(int, char *);
};
static struct cmd cmds[] = {
{ "quit", quit_cmd },
{ "help", help_cmd },
{ "switch", switch_cmd },
{ NULL, default_cmd }
};
/* sends a command */
int issue_command(int fd, char *command)
{
char *ptr;
int i;
/* Trim trailing spaces left by readline's filename completion */
ptr = &command[strlen(command) - 1];
while(isspace(*ptr)) *ptr-- = '\0';
for(i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++){
if((cmds[i].command == NULL) ||
!strncmp(cmds[i].command, command, strlen(cmds[i].command))){
(*cmds[i].proc)(fd, command);
break;
}
}
/* in future, return command status */
return 0;
}
/* sends a command in argv style array */
int issue_commandv(int fd, char **argv)
{
char *command;
int len, i, status;
len = 1; /* space for trailing null */
for(i = 0; argv[i] != NULL; i++)
len += strlen(argv[i]) + 1; /* space for space */
command = malloc(len);
if(command == NULL){
perror("issue_command");
return(-1);
}
command[0] = '\0';
for(i = 0; argv[i] != NULL; i++) {
strcat(command, argv[i]);
if(argv[i+1] != NULL) strcat(command, " ");
}
status = issue_command(fd, command);
free(command);
return status;
}
static void Usage(void)
{
fprintf(stderr, "Usage : uml_mconsole socket-name [command]\n");
exit(1);
}
int main(int argc, char **argv)
{
struct sockaddr_un here;
char *sock;
int fd;
if(argc < 2) Usage();
strcpy(uml_name, "[None]");
sock = argv[1];
switch_common(sock);
if((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){
perror("socket");
exit(1);
}
here.sun_family = AF_UNIX;
memset(here.sun_path, 0, sizeof(here.sun_path));
sprintf(&here.sun_path[1], "%5d", getpid());
if(bind(fd, (struct sockaddr *) &here, sizeof(here)) < 0){
perror("bind");
exit(1);
}
if(argc>2)
return issue_commandv(fd, argv+2);
while(1){
char *command, prompt[1 + sizeof(uml_name) + 2 + 1];
sprintf(prompt, "(%s) ", uml_name);
command = readline(prompt);
if(command == NULL) break;
if(*command) add_history(command);
issue_command(fd,command);
free(command);
}
printf("\n");
return(0);
}
|