12 #define ENOATTR ENODATA
13 #define _FILE_OFFSET_BITS 64
24 #include <sys/resource.h>
29 #ifdef HAS_VALGRIND_HEADERS
30 #include <valgrind/valgrind.h>
88 #define CVMFS_OPT(t, p, v) { t, offsetof(struct CvmfsOptions, p), v }
89 #define CVMFS_SWITCH(t, p) { t, offsetof(struct CvmfsOptions, p), 1 }
98 CVMFS_SWITCH(
"simple_options_parsing", simple_options_parsing),
150 static void Usage(
const string &exename) {
152 "The CernVM File System\n"
154 "Copyright (c) 2009- CERN, all rights reserved\n\n"
155 "Please visit http://cernvm.cern.ch for details.\n\n"
156 "Usage: %s [-h] [-V] [-s] [-f] [-d] [-k] [-o mount options] "
157 "<repository name> <mount point>\n\n"
158 "CernVM-FS general options:\n"
159 " --help|-h Print Help output (this)\n"
160 " --version|-V Print CernVM-FS version\n"
161 " -s Run singlethreaded\n"
162 " -f Run in foreground\n"
163 " -d Enable debugging\n"
164 " -k Parse options\n"
165 "CernVM-FS mount options:\n"
166 " -o config=FILES colon-separated path list of config files\n"
167 " -o uid=UID Drop credentials to another user\n"
168 " -o gid=GID Drop credentials to another group\n"
169 " -o system_mount Indicate that mount is system-wide\n"
170 " -o grab_mountpoint give ownership of the mountpoint to the user "
171 "before mounting (required for autofs)\n"
172 " -o parse Parse and print cvmfs parameters\n"
173 " -o cvmfs_suid Enable suid mode\n\n"
174 " -o disable_watchdog Do not spawn a post mortem crash handler\n"
175 " -o foreground Run in foreground\n"
176 " -o libfuse=[2,3] Enforce a certain libfuse version\n"
177 "Fuse mount options:\n"
178 " -o allow_other allow access to other users\n"
179 " -o allow_root allow access to root\n"
180 " -o nonempty allow mounts over non-empty directory\n",
181 CVMFS_VERSION, exename.c_str());
191 bool retval = (sscanf(mountpoint.c_str(),
"/dev/fd/%u%n", &fd, &len) == 1) &&
193 (static_cast<unsigned>(len) == mountpoint.length());
196 "CernVM-FS: pre-mounted on file descriptor %d", fd);
203 static void stub_init(
void *userdata,
struct fuse_conn_info *conn) {
224 struct fuse_file_info *fi)
238 struct fuse_file_info *fi)
246 struct fuse_file_info *fi)
254 off_t off,
struct fuse_file_info *fi)
262 struct fuse_file_info *fi)
269 static void stub_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off,
270 struct fuse_file_info *fi)
278 struct fuse_file_info *fi)
292 static void stub_getxattr(fuse_req_t req, fuse_ino_t ino,
const char *name,
293 size_t size, uint32_t position)
317 #
if CVMFS_USE_LIBFUSE == 2
318 unsigned long nlookup
328 #if (FUSE_VERSION >= 29)
329 static void stub_forget_multi(
332 struct fuse_forget_data *forgets
347 int key,
struct fuse_args *outargs)
351 arglen = strlen(arg);
353 case FUSE_OPT_KEY_OPT:
355 if ((arglen > 0) && (arg[0] !=
'-')) {
358 unsigned olen = strlen(*o);
359 if ((arglen > olen && arg[olen] ==
'=') &&
360 (strncasecmp(arg, *o, olen) == 0))
366 case FUSE_OPT_KEY_NONOPT:
380 Usage(outargs->argv[0]);
393 fuse_opt_add_arg(outargs,
"-d");
406 struct fuse_args *mount_options =
new fuse_args();
408 memset(&cvmfs_options, 0,
sizeof(cvmfs_options));
410 mount_options->argc = argc;
411 mount_options->argv = argv;
412 mount_options->allocated = 0;
417 delete mount_options;
420 if (cvmfs_options.
config) {
422 free(cvmfs_options.
config);
435 fuse_opt_add_arg(mount_options,
"-d");
438 return mount_options;
443 for (
int i = 0; i < mount_options->argc; i++) {
444 char *arg = mount_options->argv[i];
445 char *p = strstr(arg, opt);
450 if ((c ==
',') || (c ==
' '))
452 if ((c ==
'o') && (p >= arg+2) && (*(p-2) ==
'-'))
461 memset(loader_operations, 0,
sizeof(*loader_operations));
483 return dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
488 #ifdef HAS_VALGRIND_HEADERS
491 if (!RUNNING_ON_VALGRIND) {
495 #ifdef HAS_VALGRIND_HEADERS
504 std::string local_lib_path =
"./";
505 if (getenv(
"CVMFS_LIBRARY_PATH") != NULL) {
506 local_lib_path = getenv(
"CVMFS_LIBRARY_PATH");
507 if (!local_lib_path.empty() && (*local_lib_path.rbegin() !=
'/'))
508 local_lib_path.push_back(
'/');
511 #if CVMFS_USE_LIBFUSE == 2
512 string library_name = string(
"cvmfs_fuse") + ((debug_mode) ?
"_debug" :
"");
514 string library_name = string(
"cvmfs_fuse3") + ((debug_mode) ?
"_debug" :
"");
517 string error_messages;
519 vector<string> library_paths;
520 if (library_paths.empty()) {
521 library_paths.push_back(local_lib_path + library_name);
522 library_paths.push_back(
"/usr/lib/" + library_name);
523 library_paths.push_back(
"/usr/lib64/" + library_name);
527 library_paths.push_back(
"/usr/local/lib/" + library_name);
531 vector<string>::const_iterator i = library_paths.begin();
532 vector<string>::const_iterator iend = library_paths.end();
533 for (; i != iend; ++i) {
539 error_messages += string(dlerror()) +
"\n";
544 "failed to load cvmfs library, tried: '%s'\n%s",
545 JoinStrings(library_paths,
"' '").c_str(), error_messages.c_str());
554 if (loader_exports) {
557 load_event->
so_version = (*exports_ptr)->so_version;
558 loader_exports->
history.push_back(load_event);
582 SendMsg2Socket(fd_progress,
"Waiting for active file system calls\n");
596 SendMsg2Socket(fd_progress,
"Waiting for the delivery of SIGUSR1...\n");
634 using namespace loader;
648 if ((argc > 1) && (strstr(argv[1],
"__") == argv[1])) {
649 if (
string(argv[1]) ==
string(
"__RELOAD__")) {
652 bool stop_and_go =
false;
653 if ((argc > 3) && (
string(argv[3]) ==
"stop_and_go"))
665 if (std::string(argv[argc - 1]) == std::string(
"--debug")) {
672 if ((retval != 0) && (stop_and_go)) {
673 CreateFile(
string(argv[2]) +
".paused.crashed", 0600);
678 if (
string(argv[1]) ==
string(
"__MK_ALIEN_CACHE__")) {
681 string alien_cache_dir = argv[2];
688 int retval =
MkdirDeep(alien_cache_dir, 0770);
691 alien_cache_dir.c_str());
694 retval = chown(alien_cache_dir.c_str(), uid_owner, gid_owner);
697 alien_cache_dir.c_str(), uid_owner, gid_owner);
703 uid_owner, gid_owner);
715 debug_mode_ = getenv(
"__CVMFS_DEBUG_MODE__") != NULL;
723 struct fuse_args *mount_options;
725 if (!mount_options) {
741 for (
unsigned i = 0, s = tokens.size(); i < s; ++i) {
742 options_manager->
ParsePath(tokens[i],
false);
750 fuse_opt_add_arg(mount_options, volname.c_str());
752 fuse_opt_add_arg(mount_options,
"-odaemon_timeout=300");
753 fuse_opt_add_arg(mount_options,
"-onoapplexattr");
757 if (options_manager->
GetValue(
"CVMFS_MOUNT_RW", ¶meter) &&
758 options_manager->
IsOn(parameter))
760 fuse_opt_add_arg(mount_options,
"-orw");
762 fuse_opt_add_arg(mount_options,
"-oro");
764 fuse_opt_add_arg(mount_options,
"-onodev");
765 if (options_manager->
GetValue(
"CVMFS_SUID", ¶meter) &&
766 options_manager->
IsOn(parameter))
773 "must be root to mount with suid option");
775 fuse_opt_add_arg(mount_options,
"-osuid");
779 if (options_manager->
GetValue(
"CVMFS_CPU_AFFINITY", ¶meter)) {
784 for (vector<string>::iterator i = cpus.begin(); i != cpus.end(); i++) {
788 "CernVM-FS: setting CPU Affinity to %s", parameter.c_str());
789 int err = sched_setaffinity(0,
sizeof(mask), &mask);
792 "Setting CPU Affinity failed with error %d", errno);
796 "CPU affinity setting not supported on macOS");
816 options_manager->
Dump().c_str());
821 if (options_manager->
GetValue(
"CVMFS_SYSLOG_LEVEL", ¶meter))
825 if (options_manager->
GetValue(
"CVMFS_SYSLOG_FACILITY", ¶meter))
831 if (options_manager->
GetValue(
"CVMFS_CHECK_PERMISSIONS", ¶meter)) {
832 if (options_manager->
IsOn(parameter)) {
833 fuse_opt_add_arg(mount_options,
"-odefault_permissions");
839 "Mount point %s does not exist",
mount_point_->c_str());
844 if (options_manager->
GetValue(
"CVMFS_NFILES", ¶meter)) {
848 }
else if (retval == -1) {
851 "Failed to set maximum number of open files, "
852 "insufficient permissions");
855 unsigned soft_limit, hard_limit;
858 "Failed to set requested number of open files, "
859 "using maximum number %u", hard_limit);
860 if (hard_limit > soft_limit) {
867 if (options_manager->
GetValue(
"CVMFS_OOM_SCORE_ADJ", ¶meter)) {
868 string proc_path =
"/proc/" +
StringifyInt(getpid()) +
"/oom_score_adj";
869 int fd_oom = open(proc_path.c_str(), O_WRONLY);
872 "failed to open %s", proc_path.c_str());
874 bool retval =
SafeWrite(fd_oom, parameter.data(), parameter.length());
877 "failed to set OOM score adjustment to %s", parameter.c_str());
884 if (options_manager->
GetValue(
"CVMFS_SYSTEMD_NOKILL", ¶meter) &&
885 options_manager->
IsOn(parameter))
896 "Failed to grab mountpoint %s (%d)",
902 #if CVMFS_USE_LIBFUSE != 2
903 int premount_fd = -1;
911 "Failed to stat mountpoint %s (%d)",
915 premount_fd = open(
"/dev/fuse", O_RDWR);
916 if (premount_fd == -1) {
918 "Failed to open /dev/fuse (%d)", errno);
922 snprintf(opts,
sizeof(opts),
923 "fd=%i,rootmode=%o,user_id=0,group_id=0%s%s",
924 premount_fd, info.st_mode & S_IFMT,
926 ",default_permissions" :
"",
928 unsigned long flags = MS_NOSUID | MS_NODEV | MS_RELATIME;
932 if (mount(
"cvmfs2",
mount_point_->c_str(),
"fuse", flags, opts) == -1) {
934 "Failed to mount -t fuse -o %s cvmfs2 %s (%d)",
941 int fd_mountinfo = -1;
950 "Failed to drop credentials");
962 if (options_manager->
GetValue(
"CVMFS_USYSLOG", ¶meter))
968 "CernVM-FS: running in single threaded mode");
972 "CernVM-FS: running in debug mode");
975 #ifndef FUSE_CAP_POSIX_ACL
976 if (options_manager->
GetValue(
"CVMFS_ENFORCE_ACLS", ¶meter) &&
977 options_manager->
IsOn(parameter))
980 "CernVM-FS: ACL support requested but not available in this "
981 "version of libfuse");
989 if (options_manager->
GetValue(
"CVMFS_RELOAD_SOCKETS", ¶meter))
995 "Failed to initialize loader socket");
1002 if (options_manager->
GetValue(
"CVMFS_FUSE3_MAX_THREADS", ¶meter)) {
1005 if (options_manager->
GetValue(
"CVMFS_FUSE3_IDLE_THREADS", ¶meter)) {
1008 #ifdef CVMFS_ENABLE_FUSE3_LOOP_CONFIG
1020 "CernVM-FS: ignoring fuse3 thread settings (libfuse too old)");
1025 delete options_manager;
1026 options_manager = NULL;
1029 #if CVMFS_USE_LIBFUSE == 2
1030 struct fuse_chan *channel;
1040 "CernVM-FS: loading Fuse module... ");
1050 "\nCernVM-FS: repository %s already mounted on %s",
1067 const bool retrievable =
true;
1070 "failed to re-gain root permissions for mounting");
1078 struct fuse_lowlevel_ops loader_operations;
1080 #if (FUSE_VERSION >= 29)
1082 loader_operations.forget_multi = stub_forget_multi;
1085 #if CVMFS_USE_LIBFUSE == 2
1086 channel = fuse_mount(
mount_point_->c_str(), mount_options);
1089 "failed to create Fuse channel");
1095 session = fuse_lowlevel_new(mount_options, &loader_operations,
1096 sizeof(loader_operations), NULL);
1099 "failed to create Fuse session");
1107 session = fuse_session_new(mount_options, &loader_operations,
1108 sizeof(loader_operations), NULL);
1111 "failed to create Fuse session");
1116 if (premount_fd >= 0) {
1117 char premount_str[64];
1118 snprintf(premount_str,
sizeof(premount_str),
"/dev/fd/%d", premount_fd);
1119 retval = fuse_session_mount(session, premount_str);
1121 retval = fuse_session_mount(session,
mount_point_->c_str());
1125 "failed to mount file system");
1137 "failed to drop permissions after mounting");
1145 fd_mountinfo = open(
"/proc/self/mountinfo", O_RDONLY);
1146 if (fd_mountinfo > 0) {
1148 while (
GetLineFd(fd_mountinfo, &line)) {
1149 std::vector<std::string> tokens =
SplitString(line,
' ');
1150 if (tokens.size() < 5)
continue;
1153 for (; i < tokens.size(); ++i) {
1154 if (tokens[i] ==
"-")
break;
1156 if (tokens.size() < i + 3)
continue;
1157 if (tokens[i + 2] !=
"cvmfs2")
continue;
1161 close(fd_mountinfo);
1169 "CernVM-FS: linking %s to repository %s",
1178 retval = fuse_set_signal_handlers(session);
1180 #if CVMFS_USE_LIBFUSE == 2
1181 fuse_session_add_chan(session, channel);
1184 retval = fuse_session_loop(session);
1186 #if CVMFS_USE_LIBFUSE == 2
1187 retval = fuse_session_loop_mt(session);
1189 #ifdef CVMFS_ENABLE_FUSE3_LOOP_CONFIG
1190 struct fuse_loop_config *fuse_loop_cfg = fuse_loop_cfg_create();
1192 fuse_loop_cfg_set_clone_fd(fuse_loop_cfg, 1);
1201 retval = fuse_session_loop_mt(session, fuse_loop_cfg);
1202 fuse_loop_cfg_destroy(fuse_loop_cfg);
1204 retval = fuse_session_loop_mt(session, 1 );
1205 #endif // CVMFS_ENABLE_FUSE3_LOOP_CONFIG
1214 #if CVMFS_USE_LIBFUSE == 2
1215 fuse_remove_signal_handlers(session);
1216 fuse_session_remove_chan(channel);
1217 fuse_session_destroy(session);
1222 fuse_remove_signal_handlers(session);
1223 fuse_session_unmount(session);
1224 fuse_session_destroy(session);
1226 fuse_opt_free_args(mount_options);
1227 delete mount_options;
1229 mount_options = NULL;
1249 #if CVMFS_USE_LIBFUSE != 2
1250 if (premount_fd >= 0) {
1264 #if CVMFS_USE_LIBFUSE != 2
1265 if (premount_fd >= 0) {
1268 "failed to re-gain root permissions for umounting");
1272 "failed to umount %s (%d)",
mount_point_->c_str(), errno);
1282 repository_name_ = NULL;
1294 g_cvmfs_stub_exports->fn_main =
FuseMain;
1298 delete g_cvmfs_stub_exports;
1299 g_cvmfs_stub_exports = NULL;
bool MakeCacheDirectories(const std::string &path, const mode_t mode)
bool simple_options_parsing_
std::string repository_name
static void stub_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
void SetLogSyslogFacility(const int local_facility)
static void stub_destroy(void *userdata)
const char * Code2Ascii(const ObjectFetcherFailures::Failures error)
void SetLogSyslogLevel(const int level)
static void stub_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
virtual void ParsePath(const std::string &config_file, const bool external)=0
Session * session() const
static struct fuse_opt cvmfs_array_opts[]
Failures Reload(const int fd_progress, const bool stop_and_go, const ReloadMode reload_mode)
static CvmfsExports * LoadLibrary(const bool debug_mode, LoaderExports *loader_exports)
static void stub_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi)
int simple_options_parsing
int(* fnAltProcessFlavor)(int argc, char **argv)
int FuseMain(int argc, char *argv[])
void CreateFile(const std::string &path, const int mode, const bool ignore_failure)
string Trim(const string &raw, bool trim_newline)
string JoinStrings(const vector< string > &strings, const string &joint)
static void stub_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, size_t size)
bool IsOn(const std::string ¶m_value) const
bool SafeWrite(int fd, const void *buf, size_t nbyte)
void SendMsg2Socket(const int fd, const std::string &msg)
assert((mem||(size==0))&&"Out Of Memory")
void ** fuse_channel_or_session
MountPoint * mount_point_
std::string loader_version
void SetLogMicroSyslog(const std::string &filename)
void ParseDefault(const std::string &fqrn)
bool(* fnSaveState)(const int fd_progress, StateList *saved_states)
struct cvmcache_object_info __attribute__
static void stub_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi)
int SetLimitNoFile(unsigned limit_nofile)
static void stub_statfs(fuse_req_t req, fuse_ino_t ino)
int64_t String2Int64(const string &value)
void GetLimitNoFile(unsigned *soft_limit, unsigned *hard_limit)
static void stub_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
static void stub_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
static void stub_readlink(fuse_req_t req, fuse_ino_t ino)
static void stub_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
bool CheckPremounted(const std::string &mountpoint)
vector< string > SplitString(const string &str, char delim)
bool(* fnRestoreState)(const int fd_progress, const StateList &saved_states)
#define CVMFS_OPT(t, p, v)
static fuse_args * ParseCmdLine(int argc, char *argv[])
const loader::LoaderExports * loader_exports_
struct fuse_lowlevel_ops cvmfs_operations
bool MkdirDeep(const std::string &path, const mode_t mode, bool verify_writable)
void WaitForSignal(int signum)
static void SetFuseOperations(struct fuse_lowlevel_ops *loader_operations)
int(* fnInit)(const LoaderExports *loader_exports)
bool simple_options_parsing
static int Init(const loader::LoaderExports *loader_exports)
static void stub_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
bool(* fnMaintenanceMode)(const int fd_progress)
void(* fnFreeSavedState)(const int fd_progress, const StateList &saved_states)
string StringifyInt(const int64_t value)
int MainReload(const std::string &socket_path, const bool stop_and_go, const bool debug)
bool GetValue(const std::string &key, std::string *value) const
bool DirectoryExists(const std::string &path)
static void stub_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
bool GetLineFd(const int fd, std::string *line)
static void CloseLibrary()
uint64_t String2Uint64(const string &value)
std::string(* fnGetErrorMsg)()
bool SwitchCredentials(const uid_t uid, const gid_t gid, const bool temporarily)
static int ParseFuseOptions(void *data __attribute__((unused)), const char *arg, int key, struct fuse_args *outargs)
static void stub_release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
static bool MatchFuseOption(const fuse_args *mount_options, const char *opt)
#define CVMFS_SWITCH(t, p)
void SetLogSyslogPrefix(const std::string &prefix)
std::string MakeCanonicalPath(const std::string &path)
static void stub_init(void *userdata, struct fuse_conn_info *conn)
CvmfsExports * cvmfs_exports_
static void * OpenLibrary(const string &path)
void BlockSignal(int signum)
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
std::string * repository_name_