libCom/RTEMS: ensure lookup of localhost on RTEMS

create /etc/hosts if it doesn't already exist
This commit is contained in:
Michael Davidsaver
2016-02-15 12:29:06 -05:00
parent 0575d3764f
commit 45a741ac6f
+43
View File
@@ -18,8 +18,10 @@
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -335,6 +337,46 @@ initialize_remote_filesystem(char **argv, int hasLocalFilesystem)
#endif
}
static
char rtems_etc_hosts[] = "127.0.0.1 localhost\n";
/* If it doesn't already exist, create /etc/hosts with an entry for 'localhost' */
static
void fixup_hosts(void)
{
FILE *fp;
int ret;
struct stat STAT;
ret=stat("/etc/hosts", &STAT);
if(ret==0)
{
return; /* already exists, assume file */
} else if(errno!=ENOENT) {
perror("error: fixup_hosts stat /etc/hosts");
return;
}
ret = mkdir("/etc", 0775);
if(ret!=0 && errno!=EEXIST)
{
perror("error: fixup_hosts create /etc");
return;
}
if((fp=fopen("/etc/hosts", "w"))==NULL)
{
perror("error: fixup_hosts create /etc/hosts");
}
if(fwrite(rtems_etc_hosts, 1, sizeof(rtems_etc_hosts)-1, fp)!=sizeof(rtems_etc_hosts)-1)
{
perror("error: failed to write /etc/hosts");
}
fclose(fp);
}
/*
* Get to the startup script directory
* The TFTP filesystem requires a trailing '/' on chdir arguments.
@@ -546,6 +588,7 @@ Init (rtems_task_argument ignored)
printf("\n***** Initializing network *****\n");
rtems_bsdnet_initialize_network();
initialize_remote_filesystem(argv, initialize_local_filesystem(argv));
fixup_hosts();
/*
* More environment: iocsh prompt and hostname