Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:18

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtems_fstab File System Mount Support
0007  *
0008  * @brief File System Mount Functions
0009  *
0010  * This file contains the fsmount functions. These functions
0011  * are used to mount a list of filesystems (and create their mount
0012  * points before).
0013  */
0014 
0015 /*
0016  * Copyright (c) 2003 IMD Ingenieurbuero fuer Microcomputertechnik
0017  * All rights reserved.
0018  *
0019  * Redistribution and use in source and binary forms, with or without
0020  * modification, are permitted provided that the following conditions
0021  * are met:
0022  * 1. Redistributions of source code must retain the above copyright
0023  *    notice, this list of conditions and the following disclaimer.
0024  * 2. Redistributions in binary form must reproduce the above copyright
0025  *    notice, this list of conditions and the following disclaimer in the
0026  *    documentation and/or other materials provided with the distribution.
0027  *
0028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0029  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0030  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0031  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0032  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0033  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0034  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0035  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0036  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0037  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0038  * POSSIBILITY OF SUCH DAMAGE.
0039  */
0040 
0041 #ifndef HAVE_CONFIG_H
0042 #include "config.h"
0043 #endif
0044 
0045 #include <rtems.h>
0046 #include <rtems/fsmount.h>
0047 #include <rtems/libio.h>
0048 #include <stdio.h>
0049 #include <stdlib.h>
0050 #include <string.h>
0051 #include <errno.h>
0052 
0053 /*=========================================================================*\
0054 | Function:                                                                 |
0055 \*-------------------------------------------------------------------------*/
0056 int rtems_fsmount
0057 (
0058 /*-------------------------------------------------------------------------*\
0059   | Purpose:                                                                  |
0060   |  This function will create the mount points listed and mount the file     |
0061   |   systems listed in the calling parameters                                |
0062   +---------------------------------------------------------------------------+
0063   | Input Parameters:                                                         |
0064   \*-------------------------------------------------------------------------*/
0065   const rtems_fstab_entry *fstab_ptr,
0066   size_t fstab_count,
0067   size_t *fail_idx
0068  )
0069 /*-------------------------------------------------------------------------*\
0070   | Return Value:                                                             |
0071   |    0, if success, -1 and errno if failed                                  |
0072   \*=========================================================================*/
0073 {
0074   int rc = 0;
0075   int tmp_rc;
0076   size_t fstab_idx = 0;
0077   bool terminate = false;
0078 
0079   /*
0080    * scan through all fstab entries;
0081    */
0082   while (!terminate &&
0083          (fstab_idx < fstab_count)) {
0084     tmp_rc = 0;
0085     /*
0086      * create mount point
0087      */
0088     if (tmp_rc == 0) {
0089       tmp_rc = rtems_mkdir(fstab_ptr->target, S_IRWXU | S_IRWXG | S_IRWXO);
0090       if (tmp_rc != 0) {
0091         if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNTPNT_CRTERR)) {
0092           fprintf(stdout,"fsmount: creation of mount point \"%s\" failed: %s\n",
0093                   fstab_ptr->target,
0094                   strerror(errno));
0095         }
0096         if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNTPNT_CRTERR)) {
0097           terminate = true;
0098           rc = tmp_rc;
0099         }
0100       }
0101     }
0102     /*
0103      * mount device to given mount point
0104      */
0105     if (tmp_rc == 0) {
0106       tmp_rc = mount(fstab_ptr->source,
0107                      fstab_ptr->target,
0108                      fstab_ptr->type,
0109                      fstab_ptr->options,
0110                      NULL);
0111       if (tmp_rc != 0) {
0112         if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_FAILED)) {
0113           fprintf(stdout,"fsmount: mounting of \"%s\" to"
0114                   " \"%s\" failed: %s\n",
0115                   fstab_ptr->source,
0116                   fstab_ptr->target,
0117                   strerror(errno));
0118         }
0119         if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_FAILED)) {
0120           terminate = true;
0121           rc = tmp_rc;
0122         }
0123       }
0124       else {
0125         if (0 != (fstab_ptr->report_reasons & FSMOUNT_MNT_OK)) {
0126           fprintf(stdout,"fsmount: mounting of \"%s\" to"
0127                   " \"%s\" succeeded\n",
0128                   fstab_ptr->source,
0129                   fstab_ptr->target);
0130         }
0131         if (0 != (fstab_ptr->abort_reasons & FSMOUNT_MNT_OK)) {
0132           terminate = true;
0133         }
0134       }
0135     }
0136     /*
0137      * proceed to next entry
0138      */
0139     if (!terminate) {
0140       fstab_ptr++;
0141       fstab_idx++;
0142     }
0143   }
0144   if (fail_idx != NULL) {
0145     *fail_idx = fstab_idx;
0146   }
0147   return rc;
0148 }