source: rtems/cpukit/libnetworking/rtems/mkrootfs.c @ 615d8cc

4.10
Last change on this file since 615d8cc was 615d8cc, checked in by Sebastian Huber <sebastian.huber@…>, on 07/01/10 at 14:29:09

2010-06-08 Sebastian Huber <sebastian.huber@…>

  • libcsupport/include/rtems/libio.h: Documentation.

2010-06-08 Sebastian Huber <sebastian.huber@…>

PR 1524/filesystem

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