source: rtems/cpukit/libnetworking/rtems/mkrootfs.c @ 4c02385

5
Last change on this file since 4c02385 was 4c02385, checked in by Christian Mauderer <Christian.Mauderer@…>, on 06/24/16 at 06:52:48

libnetworking: Import current <arpa/inet.h>

Import the <arpa/inet.h> from current FreeBSD. Necessary due to changes
in <netinet/in.h>. Remove BSD hack from <arpa/inet.h>.

Clean up problems with htonl(). These functions are defined in
<arpa/inet.h>. This lead to some problems because they are defined in
<rtems/endian.h> too. Add NTOHL, ... to
<rtems/rtems_bsdnet_internal.h>.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2  ------------------------------------------------------------------------
3
4  Copyright Cybertec Pty Ltd, 2000
5  All rights reserved Cybertec Pty Ltd, 2000
6
7  COPYRIGHT (c) 1989-1998.
8  On-Line Applications Research Corporation (OAR).
9
10  The license and distribution terms for this file may be
11  found in the file LICENSE in this distribution or at
12
13  http://www.rtems.org/license/LICENSE.
14
15  This software with is provided ``as is'' and with NO WARRANTY.
16
17  ------------------------------------------------------------------------
18
19  Set of helpers when creating a root file system. The root filesystem
20  in RTEMS is the In Memory Filesystem (IMFS). We could copy an exiting
21  filesystem to here, how-ever a number of files can have target
22  specific initialisation info which we need to write.
23
24 */
25
26#if HAVE_CONFIG_H
27#include "config.h"
28#endif
29
30#include <errno.h>
31#include <stdio.h>
32#include <string.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <fcntl.h>
36#include <unistd.h>
37#include <sys/socket.h>
38#include <netinet/in.h>
39
40#include <rtems/mkrootfs.h>
41#include <rtems/libio.h>
42#include <rtems/endian.h>
43
44/*
45 * A table a list of names and their modes.
46 */
47
48typedef struct rtems_rootfs_dir_table
49{
50  const char *name;
51  mode_t      mode;
52} rtems_rootfs_dir_table;
53
54/*
55 * Table of directorys to make.
56 */
57
58static const rtems_rootfs_dir_table default_directories[] =
59{
60  { "/bin",     S_IFDIR | S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH },
61  { "/etc",     S_IFDIR | S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH },
62  { "/dev",     S_IFDIR | S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH },
63  { "/usr/bin", S_IFDIR | S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH }
64};
65
66#define MKFILE_MODE (S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH)
67#define MKDIR_MODE  (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
68
69/*
70 * Create enough files to support the networking stack.
71 * Points to a table of strings.
72 */
73
74int
75rtems_rootfs_file_append (const char *file,
76                          mode_t     omode,
77                          const int  line_cnt,
78                          const char **lines)
79{
80  struct stat sb;
81  int         fd;
82  int         i;
83
84  /*
85   * See is a file exists. If it does not, create the
86   * file and the path to the file.
87   */
88
89  fd = -1;
90
91  if (stat(file, &sb))
92  {
93    if (errno == ENOENT)
94    {
95      /*
96       * Get the path to the file if one exists and create the
97       * path. If it exists nothing happens.
98       */
99
100      size_t i = strlen (file);
101
102      while (i)
103      {
104        if (file[i] == '/')
105        {
106          char path[128];
107
108          if (i >= sizeof path)
109          {
110            printf ("root fs, path too long `%s'\n", file);
111            return -1;
112          }
113
114          strncpy (path, file, i);
115          path[i] = '\0';
116
117          if (rtems_mkdir (path, MKDIR_MODE))
118            return -1;
119          break;
120        }
121        i--;
122      }
123
124      if ((fd = open (file, O_CREAT | O_APPEND | O_WRONLY, omode)) < 0)
125      {
126        printf ("root fs, cannot create file `%s' : %s\n",
127                file, strerror (errno));
128        return -1;
129      }
130    }
131  }
132
133  if (fd < 0)
134  {
135    if ((fd = open (file, O_APPEND | O_WRONLY)) < 0)
136    {
137      printf ("root fs, cannot open file `%s' : %s\n",
138              file, strerror (errno));
139      return -1;
140    }
141  }
142
143  for (i = 0; i < line_cnt; i++)
144  {
145    size_t len = strlen (lines[i]);
146
147    if (len)
148    {
149      if (write (fd, lines[i], strlen (lines[i])) < 0)
150      {
151        close (fd);
152        printf ("root fs, cannot write to `%s' : %s\n",
153                file, strerror (errno));
154        return -1;
155      }
156    }
157  }
158
159  return close (fd);
160}
161
162/*
163 * Write hosts record.
164 */
165
166int
167rtems_rootfs_append_host_rec (in_addr_t cip,
168                              const char    *cname,
169                              const char    *dname)
170{
171  char           buf[128];
172  char           *bufp = buf;
173  const char     *bufl[1];
174  struct in_addr ip;
175
176  ip.s_addr = cip;
177
178  if (cname && strlen (cname))
179  {
180    snprintf (bufp, sizeof (buf), "%s\t\t%s", inet_ntoa (ip), cname);
181    bufp += strlen (buf);
182
183    if (dname && strlen (dname))
184    {
185      snprintf (bufp, sizeof (buf), "\t\t%s.%s", cname, dname);
186      bufp += strlen (buf);
187    }
188
189    strcat (buf, "\n");
190
191    bufl[0] = buf;
192
193    if (rtems_rootfs_file_append ("/etc/hosts", MKFILE_MODE, 1, bufl) < 0)
194      return -1;
195  }
196  else
197  {
198    printf ("rootfs hosts rec append, no cname supplied\n");
199    return -1;
200  }
201
202  return 0;
203}
204
205/*
206 * Create a root file system.
207 */
208
209int
210rtems_create_root_fs (void)
211{
212  const char *lines[1];
213  size_t      i;
214
215  /*
216   * Create the directories.
217   */
218
219  for (i = 0;
220       i < (sizeof (default_directories) / sizeof (rtems_rootfs_dir_table));
221       i++)
222    if (rtems_mkdir (default_directories[i].name,
223                            default_directories[i].mode))
224      return -1;
225
226  /*
227   * The TCP/IP stack likes this one. If DNS does not work
228   * use the host file.
229   */
230
231  lines[0] = "hosts,bind\n";
232
233  if (rtems_rootfs_file_append ("/etc/host.conf", MKFILE_MODE, 1, lines))
234    return -1;
235
236  /*
237   * Create a `/etc/hosts' file.
238   */
239
240  if (rtems_rootfs_append_host_rec (htonl (0x7f000001), "localhost", "localdomain"))
241    return -1;
242
243  return 0;
244}
Note: See TracBrowser for help on using the repository browser.