1 /***************************************************************************
2 * Copyright (C) 2007-2010 by Øyvind Harboe *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
18 /* this file contains various functionality useful to standalone systems */
25 #include "time_support.h"
27 #ifdef HAVE_ARPA_INET_H
28 #include <arpa/inet.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
42 #ifdef HAVE_SYS_STAT_H
52 /* loads a file and returns a pointer to it in memory. The file contains
53 * a 0 byte(sentinel) after len bytes - the length of the file. */
54 static int load_file(const char *fileName
, char **data
, size_t *len
)
56 /* ensure returned length is always sane */
60 pFile
= fopen(fileName
, "rb");
62 LOG_ERROR("Can't open %s", fileName
);
65 if (fseek(pFile
, 0, SEEK_END
) != 0) {
66 LOG_ERROR("Can't open %s", fileName
);
70 long fsize
= ftell(pFile
);
72 LOG_ERROR("Can't open %s", fileName
);
78 if (fseek(pFile
, 0, SEEK_SET
) != 0) {
79 LOG_ERROR("Can't open %s", fileName
);
83 *data
= malloc(*len
+ 1);
85 LOG_ERROR("Can't open %s", fileName
);
90 if (fread(*data
, 1, *len
, pFile
) != *len
) {
93 LOG_ERROR("Can't open %s", fileName
);
98 /* 0-byte after buffer (not included in *len) serves as a sentinel */
104 COMMAND_HANDLER(handle_cat_command
)
107 return ERROR_COMMAND_SYNTAX_ERROR
;
109 /* NOTE!!! we only have line printing capability so we print the entire file as a single
114 int retval
= load_file(CMD_ARGV
[0], &data
, &len
);
115 if (retval
== ERROR_OK
) {
116 command_print(CMD_CTX
, "%s", data
);
119 command_print(CMD_CTX
, "%s not found", CMD_ARGV
[0]);
124 COMMAND_HANDLER(handle_trunc_command
)
127 return ERROR_COMMAND_SYNTAX_ERROR
;
129 FILE *config_file
= NULL
;
130 config_file
= fopen(CMD_ARGV
[0], "w");
131 if (config_file
!= NULL
)
138 COMMAND_HANDLER(handle_meminfo_command
)
141 struct mallinfo info
;
144 return ERROR_COMMAND_SYNTAX_ERROR
;
149 command_print(CMD_CTX
, "Diff: %d", prev
- info
.fordblks
);
150 prev
= info
.fordblks
;
152 command_print(CMD_CTX
, "Available ram: %d", info
.fordblks
);
158 COMMAND_HANDLER(handle_append_command
)
161 return ERROR_COMMAND_SYNTAX_ERROR
;
163 int retval
= ERROR_FAIL
;
164 FILE *config_file
= NULL
;
166 config_file
= fopen(CMD_ARGV
[0], "a");
167 if (config_file
!= NULL
) {
168 fseek(config_file
, 0, SEEK_END
);
171 for (i
= 1; i
< CMD_ARGC
; i
++) {
172 if (fwrite(CMD_ARGV
[i
], 1, strlen(CMD_ARGV
[i
]),
173 config_file
) != strlen(CMD_ARGV
[i
]))
175 if (i
!= CMD_ARGC
- 1) {
176 if (fwrite(" ", 1, 1, config_file
) != 1)
180 if ((i
== CMD_ARGC
) && (fwrite("\n", 1, 1, config_file
) == 1))
189 COMMAND_HANDLER(handle_cp_command
)
192 return ERROR_COMMAND_SYNTAX_ERROR
;
194 /* NOTE!!! we only have line printing capability so we print the entire file as a single
199 int retval
= load_file(CMD_ARGV
[0], &data
, &len
);
200 if (retval
!= ERROR_OK
)
203 FILE *f
= fopen(CMD_ARGV
[1], "wb");
205 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
209 size_t chunk
= len
- pos
;
210 static const size_t maxChunk
= 512 * 1024; /* ~1/sec */
211 if (chunk
> maxChunk
)
214 if ((retval
== ERROR_OK
) && (fwrite(data
+ pos
, 1, chunk
, f
) != chunk
))
215 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
217 if (retval
!= ERROR_OK
)
220 command_print(CMD_CTX
, "%zu", len
- pos
);
228 if (retval
== ERROR_OK
)
229 command_print(CMD_CTX
, "Copied %s to %s", CMD_ARGV
[0], CMD_ARGV
[1]);
231 command_print(CMD_CTX
, "copy failed");
238 if (retval
!= ERROR_OK
)
244 COMMAND_HANDLER(handle_rm_command
)
247 return ERROR_COMMAND_SYNTAX_ERROR
;
250 if (rmdir(CMD_ARGV
[0]) == 0)
252 else if (unlink(CMD_ARGV
[0]) == 0)
255 return del
? ERROR_OK
: ERROR_FAIL
;
258 static int ioutil_Jim_Command_ls(Jim_Interp
*interp
,
260 Jim_Obj
* const *argv
)
263 Jim_WrongNumArgs(interp
, 1, argv
, "ls ?dir?");
267 const char *name
= Jim_GetString(argv
[1], NULL
);
270 dirp
= opendir(name
);
273 Jim_Obj
*objPtr
= Jim_NewListObj(interp
, NULL
, 0);
276 struct dirent
*entry
= NULL
;
277 entry
= readdir(dirp
);
281 if ((strcmp(".", entry
->d_name
) == 0) || (strcmp("..", entry
->d_name
) == 0))
284 Jim_ListAppendElement(interp
, objPtr
,
285 Jim_NewStringObj(interp
, entry
->d_name
, strlen(entry
->d_name
)));
289 Jim_SetResult(interp
, objPtr
);
294 static int ioutil_Jim_Command_peek(Jim_Interp
*interp
,
296 Jim_Obj
*const *argv
)
299 Jim_WrongNumArgs(interp
, 1, argv
, "peek ?address?");
304 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
307 int value
= *((volatile int *) address
);
309 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));
314 static int ioutil_Jim_Command_poke(Jim_Interp
*interp
,
316 Jim_Obj
*const *argv
)
319 Jim_WrongNumArgs(interp
, 1, argv
, "poke ?address? ?value?");
324 if (Jim_GetLong(interp
, argv
[1], &address
) != JIM_OK
)
327 if (Jim_GetLong(interp
, argv
[2], &value
) != JIM_OK
)
330 *((volatile int *) address
) = value
;
335 /* not so pretty code to fish out ip number*/
336 static int ioutil_Jim_Command_ip(Jim_Interp
*interp
, int argc
,
337 Jim_Obj
*const *argv
)
339 #if !defined(__CYGWIN__)
340 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
342 struct ifaddrs
*ifa
= NULL
, *ifp
= NULL
;
344 if (getifaddrs(&ifp
) < 0)
347 for (ifa
= ifp
; ifa
; ifa
= ifa
->ifa_next
) {
351 if (ifa
->ifa_addr
->sa_family
== AF_INET
)
352 salen
= sizeof(struct sockaddr_in
);
353 else if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
354 salen
= sizeof(struct sockaddr_in6
);
358 if (getnameinfo(ifa
->ifa_addr
, salen
, ip
, sizeof(ip
), NULL
, 0,
362 Jim_AppendString(interp
, tclOutput
, ip
, strlen(ip
));
369 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "fixme!!!", 0);
370 LOG_ERROR("NOT IMPLEMENTED!!!");
372 Jim_SetResult(interp
, tclOutput
);
377 #ifdef HAVE_SYS_IOCTL_H
379 /* not so pretty code to fish out eth0 mac address */
380 static int ioutil_Jim_Command_mac(Jim_Interp
*interp
, int argc
,
381 Jim_Obj
*const *argv
)
383 struct ifreq
*ifr
, *ifend
;
389 SockFD
= socket(AF_INET
, SOCK_DGRAM
, 0);
393 ifc
.ifc_len
= sizeof(ifs
);
395 if (ioctl(SockFD
, SIOCGIFCONF
, &ifc
) < 0) {
400 ifend
= ifs
+ (ifc
.ifc_len
/ sizeof(struct ifreq
));
401 for (ifr
= ifc
.ifc_req
; ifr
< ifend
; ifr
++) {
402 /* if (ifr->ifr_addr.sa_family == AF_INET) */
404 if (strcmp("eth0", ifr
->ifr_name
) != 0)
406 strncpy(ifreq
.ifr_name
, ifr
->ifr_name
, sizeof(ifreq
.ifr_name
));
407 if (ioctl(SockFD
, SIOCGIFHWADDR
, &ifreq
) < 0) {
414 Jim_Obj
*tclOutput
= Jim_NewStringObj(interp
, "", 0);
417 sprintf(buffer
, "%02x-%02x-%02x-%02x-%02x-%02x",
418 ifreq
.ifr_hwaddr
.sa_data
[0]&0xff,
419 ifreq
.ifr_hwaddr
.sa_data
[1]&0xff,
420 ifreq
.ifr_hwaddr
.sa_data
[2]&0xff,
421 ifreq
.ifr_hwaddr
.sa_data
[3]&0xff,
422 ifreq
.ifr_hwaddr
.sa_data
[4]&0xff,
423 ifreq
.ifr_hwaddr
.sa_data
[5]&0xff);
425 Jim_AppendString(interp
, tclOutput
, buffer
, strlen(buffer
));
427 Jim_SetResult(interp
, tclOutput
);
440 static const struct command_registration ioutil_command_handlers
[] = {
443 .handler
= handle_cat_command
,
445 .help
= "display text file content",
446 .usage
= "file_name",
450 .handler
= handle_trunc_command
,
452 .help
= "truncate a file to zero length",
453 .usage
= "file_name",
457 .handler
= handle_cp_command
,
459 .help
= "copy a file",
460 .usage
= "src_file_name dst_file_name",
463 .name
= "append_file",
464 .handler
= handle_append_command
,
466 .help
= "append a variable number of strings to a file",
467 .usage
= "file_name [<string1>, [<string2>, ...]]",
472 .handler
= handle_meminfo_command
,
474 .help
= "display free heap space",
480 .handler
= handle_rm_command
,
481 .help
= "remove a directory or file",
482 .usage
= "file_name",
486 * Peek and poke are security holes -- they manipulate
487 * server-internal addresses.
494 .jim_handler
= ioutil_Jim_Command_peek
,
495 .help
= "peek at a memory address",
501 .jim_handler
= ioutil_Jim_Command_poke
,
502 .help
= "poke at a memory address",
503 .usage
= "address value",
508 .jim_handler
= ioutil_Jim_Command_ls
,
509 .help
= "show a listing of files",
512 #ifdef HAVE_SYS_IOCTL_H
517 .jim_handler
= ioutil_Jim_Command_mac
,
518 .help
= "show MAC address",
524 .jim_handler
= ioutil_Jim_Command_ip
,
526 .help
= "show IP address",
528 COMMAND_REGISTRATION_DONE
531 int ioutil_init(struct command_context
*cmd_ctx
)
533 return register_commands(cmd_ctx
, NULL
, ioutil_command_handlers
);