"Fossies" - the Fresh Open Source Software Archive

Member "stress-ng-0.09.56/core-mounts.c" (15 Mar 2019, 2551 Bytes) of package /linux/privat/stress-ng-0.09.56.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "core-mounts.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) 2013-2019 Canonical, Ltd.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License
    6  * as published by the Free Software Foundation; either version 2
    7  * of the License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
   17  *
   18  * This code is a complete clean re-write of the stress tool by
   19  * Colin Ian King <colin.king@canonical.com> and attempts to be
   20  * backwardly compatible with the stress tool by Amos Waterland
   21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
   22  * functionality.
   23  *
   24  */
   25 #include "stress-ng.h"
   26 
   27 /*
   28  *  mount_add()
   29  *  add a new mount point to table
   30  */
   31 static void mount_add(
   32     char *mnts[],
   33     const int max,
   34     int *n,
   35     const char *name)
   36 {
   37     char *mnt;
   38 
   39     if (*n >= max)
   40         return;
   41     mnt = strdup(name);
   42     if (!mnt)
   43         return;
   44     mnts[*n] = mnt;
   45     (*n)++;
   46 }
   47 
   48 /*
   49  *  mount_free()
   50  *  free mount info
   51  */
   52 void mount_free(char *mnts[], const int n)
   53 {
   54     int i;
   55 
   56     for (i = 0; i < n; i++) {
   57         free(mnts[i]);
   58         mnts[i] = NULL;
   59     }
   60 }
   61 
   62 /*
   63  *  mount_get()
   64  *  populate mnts with up to max mount points
   65  *  from /etc/mtab
   66  */
   67 #if defined(HAVE_GETMNTINFO)
   68 int mount_get(char *mnts[], const int max)
   69 {
   70     int i, n = 0, ret;
   71 #if defined(__NetBSD__) || defined(__minix__)
   72     struct statvfs *statbufs;
   73 #else
   74     struct statfs *statbufs;
   75 #endif
   76 
   77     ret = getmntinfo(&statbufs, 0);
   78     if (ret > max)
   79         ret = max;
   80 
   81     for (i = 0; i < ret; i++) {
   82         mount_add(mnts, max, &n, statbufs[i].f_mntonname);
   83     }
   84     return ret;
   85 }
   86 #elif defined(HAVE_GETMNTENT) && defined(HAVE_MNTENT_H)
   87 int mount_get(char *mnts[], const int max)
   88 {
   89     FILE *mounts;
   90     struct mntent* mnt;
   91     int n = 0;
   92 
   93     mounts = setmntent("/etc/mtab", "r");
   94     /* Failed, so assume / is available */
   95     if (!mounts) {
   96         mount_add(mnts, max, &n, "/");
   97         return n;
   98     }
   99     while ((mnt = getmntent(mounts)) != NULL)
  100         mount_add(mnts, max, &n, mnt->mnt_dir);
  101 
  102     (void)endmntent(mounts);
  103     return n;
  104 }
  105 #else
  106 int mount_get(char *mnts[], const int max)
  107 {
  108     int n = 0;
  109 
  110     mount_add(mnts, max, &n, "/");
  111     mount_add(mnts, max, &n, "/dev");
  112     mount_add(mnts, max, &n, "/tmp");
  113 
  114     return n;
  115 }
  116 #endif