source: rtems/cpukit/libmisc/fsmount/fsmount.c @ 59f7e91

4.104.115
Last change on this file since 59f7e91 was 59f7e91, checked in by Joel Sherrill <joel.sherrill@…>, on 01/20/10 at 16:24:11

2010-01-20 Joel Sherrill <joel.sherrill@…>

Coverity Id 27

  • libmisc/fsmount/fsmount.c: Ensure calloc() returns memory and then we do not have to check null when freeing it. Coverity noted it was used before being checked for NULL.
  • Property mode set to 100644
File size: 7.0 KB
Line 
1/**
2 * @file
3 *
4 * File system mount functions.
5 */
6
7/*===============================================================*\
8| Project: RTEMS fsmount                                          |
9+-----------------------------------------------------------------+
10| File: fsmount.c                                                 |
11+-----------------------------------------------------------------+
12|                    Copyright (c) 2003 IMD                       |
13|      Ingenieurbuero fuer Microcomputertechnik Th. Doerfler      |
14|               <Thomas.Doerfler@imd-systems.de>                  |
15|                       all rights reserved                       |
16+-----------------------------------------------------------------+
17| this file contains the fsmount functions. These functions       |
18| are used to mount a list of filesystems (and create their mount |
19| points before)                                                  |
20|                                                                 |
21|  The license and distribution terms for this file may be        |
22|  found in the file LICENSE in this distribution or at           |
23|  http://www.rtems.com/license/LICENSE.                     |
24|                                                                 |
25+-----------------------------------------------------------------+
26|   date                      history                        ID   |
27| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
28| 02.07.03  creation                                         doe  |
29\*===============================================================*/
30
31#ifndef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <rtems.h>
36#include <rtems/fsmount.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <errno.h>
41#include <rtems/imfs.h>
42#include <sys/stat.h>
43
44/*=========================================================================*\
45| Function:                                                                 |
46\*-------------------------------------------------------------------------*/
47int rtems_fsmount_create_mount_point
48(
49/*-------------------------------------------------------------------------*\
50| Purpose:                                                                  |
51|  This function will create the mount point given                          |
52+---------------------------------------------------------------------------+
53| Input Parameters:                                                         |
54\*-------------------------------------------------------------------------*/
55 const char *mount_point
56 )
57/*-------------------------------------------------------------------------*\
58| Return Value:                                                             |
59|    0, if success, -1 and errno if failed                                  |
60\*=========================================================================*/
61{
62  int rc = 0;
63  char *tok_buffer = NULL;
64  char *token = NULL;
65  int    token_len;
66  size_t total_len;
67  IMFS_token_types token_type;
68  struct stat file_info;
69  /*
70   * allocate temp memory to rebuild path name
71   */
72  tok_buffer = calloc(strlen(mount_point)+1,sizeof(char));
73  if ( !tok_buffer )
74    return -1;
75  token = tok_buffer;
76  total_len = 0;
77  do {
78    /*
79     * scan through given string, one segment at a time
80     */
81    token_type = IMFS_get_token(
82      mount_point+total_len,
83      strlen(mount_point+total_len),
84      token,
85      &token_len
86    );
87    total_len += token_len;
88    strncpy(tok_buffer,mount_point,total_len);
89    tok_buffer[total_len] = '\0';
90
91    if ((token_type != IMFS_NO_MORE_PATH) &&
92        (token_type != IMFS_CURRENT_DIR)  &&
93        (token_type != IMFS_INVALID_TOKEN)) {
94      /*
95       * check, whether segment exists
96       */
97      if (0 != stat(tok_buffer,&file_info)) {
98        /*
99         * if not, create directory
100         */
101        rc = mknod(tok_buffer,S_IRWXU | S_IRWXG | S_IRWXO | S_IFDIR,0);
102      }
103    }
104  } while ((rc == 0) &&
105           (token_type != IMFS_NO_MORE_PATH) &&
106           (token_type != IMFS_INVALID_TOKEN));
107
108  /*
109   * return token buffer to heap. Verified to be non-NULL when calloc'ed.
110   */
111  free(tok_buffer);
112  return rc;
113}
114
115/*=========================================================================*\
116| Function:                                                                 |
117\*-------------------------------------------------------------------------*/
118int rtems_fsmount
119(
120/*-------------------------------------------------------------------------*\
121| Purpose:                                                                  |
122|  This function will create the mount points listed and mount the file     |
123|   systems listed in the calling parameters                                |
124+---------------------------------------------------------------------------+
125| Input Parameters:                                                         |
126\*-------------------------------------------------------------------------*/
127 const rtems_fstab_entry *fstab_ptr,
128 size_t fstab_count,
129 size_t *fail_idx
130 )
131/*-------------------------------------------------------------------------*\
132| Return Value:                                                             |
133|    0, if success, -1 and errno if failed                                  |
134\*=========================================================================*/
135{
136  int rc = 0;
137  int tmp_rc;
138  size_t fstab_idx = 0;
139  rtems_filesystem_mount_table_entry_t *tmp_mt_entry;
140  bool terminate = false;
141
142  /*
143   * scan through all fstab entries;
144   */
145  while (!terminate &&
146         (fstab_idx < fstab_count)) {
147    tmp_rc = 0;
148    /*
149     * create mount point
150     */
151    if (tmp_rc == 0) {
152      tmp_rc = rtems_fsmount_create_mount_point(fstab_ptr->mount_point);
153      if (tmp_rc != 0) {
154        if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNTPNT_CRTERR)) {
155          fprintf(stdout,"fsmount: creation of mount point \"%s\" failed: %s\n",
156                 fstab_ptr->mount_point,
157                 strerror(errno));
158        }
159        if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNTPNT_CRTERR)) {
160          terminate = true;
161          rc = tmp_rc;
162        }
163      }
164    }
165    /*
166     * mount device to given mount point
167     */
168    if (tmp_rc == 0) {
169      tmp_rc = mount(&tmp_mt_entry,
170                     fstab_ptr->fs_ops,
171                     fstab_ptr->mount_options,
172                     fstab_ptr->dev,
173                     fstab_ptr->mount_point);
174      if (tmp_rc != 0) {
175        if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_FAILED)) {
176          fprintf(stdout,"fsmount: mounting of \"%s\" to"
177                 " \"%s\" failed: %s\n",
178                 fstab_ptr->dev,
179                 fstab_ptr->mount_point,
180                 strerror(errno));
181        }
182        if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_FAILED)) {
183          terminate = true;
184          rc = tmp_rc;
185        }
186      }
187      else {
188        if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_OK)) {
189          fprintf(stdout,"fsmount: mounting of \"%s\" to"
190                 " \"%s\" succeeded\n",
191                 fstab_ptr->dev,
192                 fstab_ptr->mount_point);
193        }
194        if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_OK)) {
195          terminate = true;
196        }
197      }
198    }
199    /*
200     * proceed to next entry
201     */
202    if (!terminate) {
203      fstab_ptr++;
204      fstab_idx++;
205    }
206  }
207  if (fail_idx != NULL) {
208    *fail_idx = fstab_idx;
209  }
210  return rc;
211}
Note: See TracBrowser for help on using the repository browser.