"Fossies" - the Fresh Open Source Software Archive

Member "src/Core/Unix/FreeBSD/CoreFreeBSD.cpp" (10 Oct 2018, 5250 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


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 "CoreFreeBSD.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include <fstream>
   14 #include <stdio.h>
   15 #include <unistd.h>
   16 #include <sys/param.h>
   17 #include <sys/ucred.h>
   18 #include <sys/mount.h>
   19 #include <sys/wait.h>
   20 #include "CoreFreeBSD.h"
   21 #include "Core/Unix/CoreServiceProxy.h"
   22 
   23 namespace VeraCrypt
   24 {
   25     CoreFreeBSD::CoreFreeBSD ()
   26     {
   27     }
   28 
   29     CoreFreeBSD::~CoreFreeBSD ()
   30     {
   31     }
   32 
   33     DevicePath CoreFreeBSD::AttachFileToLoopDevice (const FilePath &filePath, bool readOnly) const
   34     {
   35         list <string> args;
   36         args.push_back ("-a");
   37         args.push_back ("-t");
   38         args.push_back ("vnode");
   39 
   40         if (readOnly)
   41         {
   42             args.push_back ("-o");
   43             args.push_back ("readonly");
   44         }
   45 
   46         args.push_back ("-f");
   47         args.push_back (filePath);
   48 
   49         string dev = StringConverter::Trim (Process::Execute ("mdconfig", args));
   50 
   51         if (dev.find ("/") == string::npos)
   52             dev = string ("/dev/") + dev;
   53 
   54         return dev;
   55     }
   56 
   57     void CoreFreeBSD::DetachLoopDevice (const DevicePath &devicePath) const
   58     {
   59         list <string> args;
   60         args.push_back ("-d");
   61         args.push_back ("-u");
   62         args.push_back (StringConverter::GetTrailingNumber (devicePath));
   63 
   64         for (int t = 0; true; t++)
   65         {
   66             try
   67             {
   68                 Process::Execute ("mdconfig", args);
   69                 break;
   70             }
   71             catch (ExecutedProcessFailed&)
   72             {
   73                 if (t > 5)
   74                     throw;
   75                 Thread::Sleep (200);
   76             }
   77         }
   78     }
   79 
   80     HostDeviceList CoreFreeBSD::GetHostDevices (bool pathListOnly) const
   81     {
   82         HostDeviceList devices;
   83 #ifdef TC_MACOSX
   84         const string busType = "rdisk";
   85 #else
   86         foreach (const string &busType, StringConverter::Split ("ad da"))
   87 #endif
   88         {
   89             for (int devNumber = 0; devNumber < 64; devNumber++)
   90             {
   91                 stringstream devPath;
   92                 devPath << "/dev/" << busType << devNumber;
   93 
   94                 if (FilesystemPath (devPath.str()).IsBlockDevice() || FilesystemPath (devPath.str()).IsCharacterDevice())
   95                 {
   96                     make_shared_auto (HostDevice, device);
   97                     device->Path = devPath.str();
   98                     if (!pathListOnly)
   99                     {
  100                         try
  101                         {
  102                             device->Size = GetDeviceSize (device->Path);
  103                         }
  104                         catch (...)
  105                         {
  106                             device->Size = 0;
  107                         }
  108                         device->MountPoint = GetDeviceMountPoint (device->Path);
  109                         device->SystemNumber = 0;
  110                     }
  111                     devices.push_back (device);
  112 
  113                     for (int partNumber = 1; partNumber < 32; partNumber++)
  114                     {
  115 #ifdef TC_MACOSX
  116                         const string partLetter = "";
  117 #else
  118                         foreach (const string &partLetter, StringConverter::Split (",a,b,c,d,e,f,g,h", ",", true))
  119 #endif
  120                         {
  121                             stringstream partPath;
  122                             partPath << devPath.str() << "s" << partNumber << partLetter;
  123 
  124                             if (FilesystemPath (partPath.str()).IsBlockDevice() || FilesystemPath (partPath.str()).IsCharacterDevice())
  125                             {
  126                                 make_shared_auto (HostDevice, partition);
  127                                 partition->Path = partPath.str();
  128                                 if (!pathListOnly)
  129                                 {
  130                                     try
  131                                     {
  132                                         partition->Size = GetDeviceSize (partition->Path);
  133                                     }
  134                                     catch (...)
  135                                     {
  136                                         partition->Size = 0;
  137                                     }
  138                                     partition->MountPoint = GetDeviceMountPoint (partition->Path);
  139                                     partition->SystemNumber = 0;
  140                                 }
  141 
  142                                 device->Partitions.push_back (partition);
  143                             }
  144                         }
  145                     }
  146                 }
  147             }
  148         }
  149 
  150         return devices;
  151     }
  152 
  153     MountedFilesystemList CoreFreeBSD::GetMountedFilesystems (const DevicePath &devicePath, const DirectoryPath &mountPoint) const
  154     {
  155 
  156         static Mutex mutex;
  157         ScopeLock sl (mutex);
  158 
  159         struct statfs *sysMountList;
  160         int count = getmntinfo (&sysMountList, MNT_NOWAIT);
  161         throw_sys_if (count == 0);
  162 
  163         MountedFilesystemList mountedFilesystems;
  164 
  165         for (int i = 0; i < count; i++)
  166         {
  167             make_shared_auto (MountedFilesystem, mf);
  168 
  169             if (sysMountList[i].f_mntfromname[0])
  170                 mf->Device = DevicePath (sysMountList[i].f_mntfromname);
  171             else
  172                 continue;
  173 
  174             if (sysMountList[i].f_mntonname[0])
  175                 mf->MountPoint = DirectoryPath (sysMountList[i].f_mntonname);
  176 
  177             mf->Type = sysMountList[i].f_fstypename;
  178 
  179             if ((devicePath.IsEmpty() || devicePath == mf->Device) && (mountPoint.IsEmpty() || mountPoint == mf->MountPoint))
  180                 mountedFilesystems.push_back (mf);
  181         }
  182 
  183         return mountedFilesystems;
  184     }
  185 
  186     void CoreFreeBSD::MountFilesystem (const DevicePath &devicePath, const DirectoryPath &mountPoint, const string &filesystemType, bool readOnly, const string &systemMountOptions) const
  187     {
  188         try
  189         {
  190             // Try to mount FAT by default as mount is unable to probe filesystem type on BSD
  191             CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType.empty() ? "msdos" : filesystemType, readOnly, systemMountOptions);
  192         }
  193         catch (ExecutedProcessFailed&)
  194         {
  195             if (!filesystemType.empty())
  196                 throw;
  197 
  198             CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType, readOnly, systemMountOptions);
  199         }
  200     }
  201 
  202 #ifdef TC_FREEBSD
  203     auto_ptr <CoreBase> Core (new CoreServiceProxy <CoreFreeBSD>);
  204     auto_ptr <CoreBase> CoreDirect (new CoreFreeBSD);
  205 #endif
  206 }