public override bool TryRenameFile(string source, string destination) { return(Syscall.rename(source, destination) == 0); }
static int Main(string [] args) { string assembly = null; string directory = null; string lockfile = null; string name = null; string logname = null; foreach (string s in args) { if (s.Length > 3 && s [0] == '-' && s [2] == ':') { string arg = s.Substring(3); switch (Char.ToLower(s [1])) { case 'd': directory = arg; break; case 'l': lockfile = arg; break; case 'n': name = arg; break; case 'm': logname = arg; break; default: Usage(); break; } } else { if (assembly != null) { Usage(); } assembly = s; } } if (logname == null) { logname = assembly; } if (assembly == null) { error(logname, "Assembly name is missing"); Usage(); } if (directory != null) { if (Syscall.chdir(directory) != 0) { error(logname, "Could not change to directory {0}", directory); return(1); } } // Use lockfile to allow only one instance if (lockfile == null) { lockfile = String.Format("/tmp/{0}.lock", Path.GetFileName(assembly)); } int lfp = Syscall.open(lockfile, OpenFlags.O_RDWR | OpenFlags.O_CREAT | OpenFlags.O_EXCL, FilePermissions.S_IRUSR | FilePermissions.S_IWUSR | FilePermissions.S_IRGRP); if (lfp < 0) { // Provide some useful info if (File.Exists(lockfile)) { error(logname, String.Format("Lock file already exists: {0}", lockfile)); } else { error(logname, String.Format("Cannot open/create lock file exclusively: {0}", lockfile)); } return(1); } if (Syscall.lockf(lfp, LockfCommand.F_TLOCK, 0) < 0) { info(logname, "Daemon is already running."); return(0); } try { // Write pid to lock file string pid = Syscall.getpid().ToString() + Environment.NewLine; IntPtr buf = Marshal.StringToCoTaskMemAnsi(pid); Syscall.write(lfp, buf, (ulong)pid.Length); Marshal.FreeCoTaskMem(buf); // Create new AppDomain to run service AppDomainSetup setup = new AppDomainSetup(); setup.ApplicationBase = Environment.CurrentDirectory; setup.ConfigurationFile = Path.Combine(Environment.CurrentDirectory, assembly + ".config"); setup.ApplicationName = logname; AppDomain newDomain = AppDomain.CreateDomain(logname, AppDomain.CurrentDomain.Evidence, setup); MonoServiceRunner rnr = newDomain.CreateInstanceAndUnwrap( typeof(MonoServiceRunner).Assembly.FullName, typeof(MonoServiceRunner).FullName, true, BindingFlags.Default, null, new object [] { assembly, name, logname }, null, null, null) as MonoServiceRunner; if (rnr == null) { error(logname, "Internal Mono Error: Could not create MonoServiceRunner."); return(1); } return(rnr.StartService()); } finally { // Remove lock file when done if (File.Exists(lockfile)) { File.Delete(lockfile); } } }
protected override void OnStop() { Syscall.kill(proc.Id, Signum.SIGINT); }
/// <summary> /// Check whether the current user has write permission to the specified path. /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool HasWritePermissionOnDir(string path) { var writeAllow = false; var writeDeny = false; try { var accessControlList = Directory.GetAccessControl(path); if (accessControlList == null) { return(false); } var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); if (accessRules == null) { return(false); } foreach (System.Security.AccessControl.FileSystemAccessRule rule in accessRules) { if ((System.Security.AccessControl.FileSystemRights.Write & rule.FileSystemRights) != System.Security.AccessControl.FileSystemRights.Write) { continue; } if (rule.AccessControlType == System.Security.AccessControl.AccessControlType.Allow) { writeAllow = true; } else if (rule.AccessControlType == System.Security.AccessControl.AccessControlType.Deny) { writeDeny = true; } } } catch (System.PlatformNotSupportedException) { #if __MonoCS__ && !__COCOA__ writeAllow = (0 == Syscall.access(path, AccessModes.W_OK)); //writeAllow = true; #endif #if __COCOA__ // TODO check directory permissions writeAllow = true; #endif } catch (System.UnauthorizedAccessException) { var permission = new FileIOPermission(FileIOPermissionAccess.Write, path); var permissionSet = new PermissionSet(PermissionState.None); permissionSet.AddPermission(permission); if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet)) { return(true); } else { return(false); } } return(writeAllow && !writeDeny); }
private static int Poppassd() { const int badPassDelay = 3000; const int maxLenPass = 128; const string version = "0.1shim"; Console.WriteLine("200 poppassd v{0} hello, who are you?", version); string line = (Console.ReadLine() ?? "").ToLowerInvariant(); string user; if (!line.StartsWith("user ") || (user = line.Substring(5)).Length == 0) { Console.WriteLine("500 Username required."); return(1); } if (!CheckUserName(user)) { Console.WriteLine("500 Invalid username."); return(1); } Console.WriteLine("200 Your password please."); line = (Console.ReadLine() ?? "").ToLowerInvariant(); if (line.Length > maxLenPass) { Console.WriteLine("500 Password length exceeded (max {0}).", maxLenPass); return(1); } string oldpass; if (!line.StartsWith("pass ") || (oldpass = line.Substring(5)).Length == 0) { Console.WriteLine("500 Password required."); return(1); } if (!CheckPassword(user, oldpass)) { Console.WriteLine("500 Old password is incorrect."); Syscall.syslog(SyslogFacility.LOG_DAEMON, SyslogLevel.LOG_ERR, string.Format("old password is incorrect for user {0}", user)); Thread.Sleep(badPassDelay); return(1); } Console.WriteLine("200 Your new password please."); line = (Console.ReadLine() ?? "").ToLowerInvariant(); string newpass; if (!line.StartsWith("newpass ") || (newpass = line.Substring(8)).Length == 0) { Console.WriteLine("500 New password required."); return(1); } if (!ChangePassword(user, newpass)) { Console.WriteLine("500 Server error, password not changed"); return(1); } Syscall.syslog(SyslogFacility.LOG_DAEMON, SyslogLevel.LOG_ERR, string.Format("changed POP3 password for {0}", user)); Console.WriteLine("200 Password changed, thank-you."); line = (Console.ReadLine() ?? "").ToLowerInvariant(); if (line != "quit") { Console.WriteLine("500 Quit required."); return(1); } Console.WriteLine("200 bye."); return(0); }
public static void Main(string[] args) { Bus conn; if (args.Length == 0) { conn = Bus.Session; } else { if (args[0] == "--session") { conn = Bus.Session; } else if (args[0] == "--system") { conn = Bus.System; } else { conn = Bus.Open(args[0]); } } IBus bus = conn.GetObject <IBus> ("org.freedesktop.DBus", new ObjectPath("/org/freedesktop/DBus")); Console.WriteLine(bus.ListNames().Length); var obj = conn.GetObject <Interface> (Constants.BusName, Constants.ObjectPath); var obj2 = conn.GetObject <Interface2> (Constants.BusName, Constants.ObjectPath); var objIntr = conn.GetObject <Introspectable> (Constants.BusName, Constants.ObjectPath); obj.Ping(); Console.WriteLine(obj.GetBytes(3).Length); Console.WriteLine("conn.UnixFDSupported = " + conn.UnixFDSupported); if (!conn.UnixFDSupported) { return; } using (var disposableList = new DisposableList()) { var res = obj.GetFD(disposableList, false); Console.WriteLine("Got FD:"); Mono.Unix.Native.Stdlib.system("ls -l /proc/$PPID/fd/" + res.Handle); } using (var disposableList = new DisposableList()) { var res = obj.GetFDList(disposableList, false); Console.WriteLine("Got FDs:"); foreach (var fd in res) { Mono.Unix.Native.Stdlib.system("ls -l /proc/$PPID/fd/" + fd.Handle); } } using (var disposableList = new DisposableList()) { var res = (UnixFD[])obj.GetFDListVariant(disposableList, false); Console.WriteLine("Got FDs as variant:"); foreach (var fd in res) { Mono.Unix.Native.Stdlib.system("ls -l /proc/$PPID/fd/" + fd.Handle); } } using (var disposableList = new DisposableList()) { try { obj.GetFD(disposableList, true); throw new Exception("Expected an exception"); } catch (Exception e) { if (!e.Message.Contains("Throwing an exception after creating a UnixFD object")) { throw; } } } using (var disposableList = new DisposableList()) { try { obj.GetFDList(disposableList, true); throw new Exception("Expected an exception"); } catch (Exception e) { if (!e.Message.Contains("Throwing an exception after creating a UnixFD object")) { throw; } } } using (var disposableList = new DisposableList()) { try { obj.GetFDListVariant(disposableList, true); throw new Exception("Expected an exception"); } catch (Exception e) { if (!e.Message.Contains("Throwing an exception after creating a UnixFD object")) { throw; } } } // Check whether this leaks an FD obj.GetFD(null, false); obj.GetFDList(null, false); obj.GetFDListVariant(null, false); try { obj.GetFD(null, true); } catch {} try { obj.GetFDList(null, true); } catch {} try { obj.GetFDListVariant(null, true); } catch {} obj2.GetFD(false); obj2.GetFDList(false); obj2.GetFDListVariant(false); try { obj2.GetFD(true); } catch {} try { obj2.GetFDList(true); } catch {} try { obj2.GetFDListVariant(true); } catch {} var fd_ = Syscall.open("/dev/zero", OpenFlags.O_RDWR, 0); if (fd_ < 0) { UnixMarshal.ThrowExceptionForLastError(); } using (var fd = new UnixFD(fd_)) { obj.SendFD(fd); obj.SendFD(fd); obj.SendFDList(new UnixFD[] { fd, fd }); obj.SendFDListVariant(new UnixFD[] { fd, fd }); var impl = new SignalsImpl(); var spath = new ObjectPath("/mono_dbus_sharp_test/Signals"); conn.Register(spath, impl); obj.RegisterSignalInterface(conn.UniqueName, spath); impl.CallGotFD(fd); } Console.WriteLine(objIntr.Introspect().Length); obj.ListOpenFDs(); Console.WriteLine("Open FDs:"); Mono.Unix.Native.Stdlib.system("ls -l /proc/$PPID/fd/"); }
public bool Resume() { return(Syscall.ResumeThread(ThreadHandle)); }
/// <inheritdoc/> public virtual async Task ReadFromMemory(Func <IntPtr, long, Task> readProc) { VxArgs.NotNull(readProc, nameof(readProc)); #if NETSTANDARD2_0 if (MmapAvailable && AllowUseMmap) { var fileId = Syscall.open(_fullFilePath, OpenFlags.O_RDONLY); VerifyUnixSuccess(fileId); VerifyUnixSuccess(Syscall.fstat(fileId, out var sb)); long size = sb.st_size; try { // ReSharper disable once IdentifierTypo IntPtr mmapPtr = Syscall.mmap( IntPtr.Zero, (ulong)size, MmapProts.PROT_READ, MmapFlags.MAP_PRIVATE | MmapFlags.MAP_POPULATE, fileId, 0); if (mmapPtr == (IntPtr)(-1)) { throw new UnixIOException(); } try { await readProc(mmapPtr, size); } finally { Syscall.munmap(mmapPtr, (ulong)size); } } finally { Syscall.close(fileId); } } else #endif { using (var mmf = MemoryMappedFile.CreateFromFile(_fullFilePath, FileMode.Open)) { using (var mmv = mmf.CreateViewAccessor(0L, 0L, MemoryMappedFileAccess.Read)) { using (var handle = mmv.SafeMemoryMappedViewHandle) { IntPtr mmfDataPtr; unsafe { byte *mmfData = (byte *)IntPtr.Zero; handle.AcquirePointer(ref mmfData); mmfDataPtr = (IntPtr)mmfData; } try { await readProc(mmfDataPtr, mmv.Capacity); } finally { handle.ReleasePointer(); } } } } } }
public RemoteThread(RemoteProcess process, uint threadId) { Process = process; ThreadId = threadId; ThreadHandle = Syscall.OpenThread(Enumerations.ThreadAccessFlags.AllAccess, ThreadId); }
public bool Suspend() { return(Syscall.SuspendThread(ThreadHandle)); }
public override string GetUserId() { return(Syscall.getuid().ToString(CultureInfo.InvariantCulture)); }
// Atomic rename of a file. It does not fire events. public static void SystemRename(string sourceFile, string destFile) { if (string.IsNullOrEmpty(sourceFile)) { throw new ArgumentException("sourceFile"); } if (string.IsNullOrEmpty(destFile)) { throw new ArgumentException("destFile"); } //FIXME: use the atomic System.IO.File.Replace on NTFS if (Platform.IsWindows) { string wtmp = null; if (File.Exists(destFile)) { do { wtmp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); } while (File.Exists(wtmp)); File.Move(destFile, wtmp); } try { File.Move(sourceFile, destFile); } catch { try { if (wtmp != null) { File.Move(wtmp, destFile); } } catch { wtmp = null; } throw; } finally { if (wtmp != null) { try { File.Delete(wtmp); } catch { } } } } else { if (Syscall.rename(sourceFile, destFile) != 0) { switch (Stdlib.GetLastError()) { case Errno.EACCES: case Errno.EPERM: throw new UnauthorizedAccessException(); case Errno.EINVAL: throw new InvalidOperationException(); case Errno.ENOTDIR: throw new DirectoryNotFoundException(); case Errno.ENOENT: throw new FileNotFoundException(); case Errno.ENAMETOOLONG: throw new PathTooLongException(); default: throw new IOException(); } } } }
static int Main() { var depDir = Path.Combine("..", "external", "benchmarker"); var benchDir = Path.Combine(depDir, "benchmarks"); var testDir = Path.Combine(depDir, "tests"); var benchmarks = Directory.EnumerateFiles(benchDir, "*.benchmark") .Select(Benchmark.Load) .Where(b => !b.OnlyExplicit && b.ClientCommandLine == null && IsSupported(b)) .OrderBy(b => b.Name) .ToArray(); var monoPath = Path.GetFullPath(Path.Combine("..", "..", "runtime", "mono-wrapper")); var classDir = Path.GetFullPath(Path.Combine("..", "..", "mcs", "class", "lib", "net_4_x")); var rand = new Random(); var cpus = Environment.ProcessorCount; var results = new List <TestResult> (benchmarks.Length); var sw = Stopwatch.StartNew(); for (var i = 0; i < benchmarks.Length; i++) { var bench = benchmarks [i]; var sampleFreq = rand.Next(-1000, 1001); var sampleMode = rand.Next(0, 2) == 1 ? "-real" : string.Empty; var maxSamples = rand.Next(0, cpus * 2000 + 1); var heapShotFreq = rand.Next(-10, 11); var maxFrames = rand.Next(0, 33); var options = _options.ToDictionary(x => x, _ => rand.Next(0, 2) == 1) .Select(x => (x.Value ? string.Empty : "no") + x.Key) .ToArray(); var profOptions = $"maxframes={maxFrames},{string.Join (",", options)},output=/dev/null"; if (sampleFreq > 0) { profOptions += $",sample{sampleMode}={sampleFreq},maxsamples={maxSamples}"; } if (heapShotFreq > 0) { profOptions += $",heapshot={heapShotFreq}gc"; } var info = new ProcessStartInfo { UseShellExecute = false, WorkingDirectory = Path.Combine(testDir, bench.TestDirectory), FileName = monoPath, Arguments = $"--debug --profile=log:{profOptions} " + string.Join(" ", bench.CommandLine), RedirectStandardOutput = true, RedirectStandardError = true, }; info.EnvironmentVariables.Clear(); info.EnvironmentVariables.Add("MONO_PATH", classDir); var progress = $"({i + 1}/{benchmarks.Length})"; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine($"[{sw.Elapsed.ToString ("G")}] {progress} Running {bench.Name} with profiler options: {profOptions}"); Console.ResetColor(); var result = new TestResult { Benchmark = bench, StartInfo = info, }; using (var proc = new Process()) { proc.StartInfo = info; var stdout = new StringBuilder(); var stderr = new StringBuilder(); proc.OutputDataReceived += (sender, args) => { if (args.Data != null) { lock (result) stdout.AppendLine(args.Data); } }; proc.ErrorDataReceived += (sender, args) => { if (args.Data != null) { lock (result) stderr.AppendLine(args.Data); } }; result.Stopwatch.Start(); proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); if (!proc.WaitForExit((int)_timeout.TotalMilliseconds)) { // Force a thread dump. Syscall.kill(proc.Id, Signum.SIGQUIT); Thread.Sleep(1000); try { proc.Kill(); } catch (Exception) { } } else { result.ExitCode = proc.ExitCode; } result.Stopwatch.Stop(); lock (result) { result.StandardOutput = stdout.ToString(); result.StandardError = stderr.ToString(); } } var resultStr = result.ExitCode == null ? "timed out" : $"exited with code: {result.ExitCode}"; Console.ForegroundColor = result.ExitCode != 0 ? ConsoleColor.Red : ConsoleColor.Green; Console.WriteLine($"[{sw.Elapsed.ToString ("G")}] {progress} {bench.Name} took {result.Stopwatch.Elapsed.ToString ("G")} and {resultStr}"); Console.ResetColor(); if (result.ExitCode != 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("===== stdout ====="); Console.ResetColor(); Console.WriteLine(result.StandardOutput); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("===== stderr ====="); Console.ResetColor(); Console.WriteLine(result.StandardError); } if (_processors.TryGetValue(bench.Name, out var processor)) { processor(result); } results.Add(result); } sw.Stop(); var successes = results.Count(r => r.ExitCode == 0); var failures = results.Count(r => r.ExitCode != null && r.ExitCode != 0); var timeouts = results.Count(r => r.ExitCode == null); var settings = new XmlWriterSettings { NewLineOnAttributes = true, Indent = true, }; using (var writer = XmlWriter.Create("TestResult-profiler-stress.xml", settings)) { writer.WriteStartDocument(); writer.WriteComment("This file represents the results of running a test suite"); writer.WriteStartElement("test-results"); writer.WriteAttributeString("name", "profiler-stress-tests.dummy"); writer.WriteAttributeString("total", results.Count.ToString()); writer.WriteAttributeString("failures", failures.ToString()); writer.WriteAttributeString("not-run", "0"); writer.WriteAttributeString("date", DateTime.Now.ToString("yyyy-MM-dd")); writer.WriteAttributeString("time", DateTime.Now.ToString("HH:mm:ss")); writer.WriteStartElement("environment"); writer.WriteAttributeString("nunit-version", "2.4.8.0"); writer.WriteAttributeString("clr-version", Environment.Version.ToString()); writer.WriteAttributeString("os-version", Environment.OSVersion.ToString()); writer.WriteAttributeString("platform", Environment.OSVersion.Platform.ToString()); writer.WriteAttributeString("cwd", Environment.CurrentDirectory); writer.WriteAttributeString("machine-name", Environment.MachineName); writer.WriteAttributeString("user", Environment.UserName); writer.WriteAttributeString("user-domain", Environment.UserDomainName); writer.WriteEndElement(); writer.WriteStartElement("culture-info"); writer.WriteAttributeString("current-culture", CultureInfo.CurrentCulture.Name); writer.WriteAttributeString("current-uiculture", CultureInfo.CurrentUICulture.Name); writer.WriteEndElement(); writer.WriteStartElement("test-suite"); writer.WriteAttributeString("name", "profiler-stress-tests.dummy"); writer.WriteAttributeString("success", (failures + timeouts == 0).ToString()); writer.WriteAttributeString("time", ((int)sw.Elapsed.TotalSeconds).ToString()); writer.WriteAttributeString("asserts", (failures + timeouts).ToString()); writer.WriteStartElement("results"); writer.WriteStartElement("test-suite"); writer.WriteAttributeString("name", "MonoTests"); writer.WriteAttributeString("success", (failures + timeouts == 0).ToString()); writer.WriteAttributeString("time", ((int)sw.Elapsed.TotalSeconds).ToString()); writer.WriteAttributeString("asserts", (failures + timeouts).ToString()); writer.WriteStartElement("results"); writer.WriteStartElement("test-suite"); writer.WriteAttributeString("name", "profiler-stress"); writer.WriteAttributeString("success", (failures + timeouts == 0).ToString()); writer.WriteAttributeString("time", ((int)sw.Elapsed.TotalSeconds).ToString()); writer.WriteAttributeString("asserts", (failures + timeouts).ToString()); writer.WriteStartElement("results"); foreach (var result in results) { var timeoutStr = result.ExitCode == null ? "_timeout" : string.Empty; writer.WriteStartElement("test-case"); writer.WriteAttributeString("name", $"MonoTests.profiler-stress.{result.Benchmark.Name}{timeoutStr}"); writer.WriteAttributeString("executed", "True"); writer.WriteAttributeString("success", (result.ExitCode == 0).ToString()); writer.WriteAttributeString("time", ((int)result.Stopwatch.Elapsed.TotalSeconds).ToString()); writer.WriteAttributeString("asserts", result.ExitCode == 0 ? "0" : "1"); if (result.ExitCode != 0) { writer.WriteStartElement("failure"); writer.WriteStartElement("message"); writer.WriteCData(FilterInvalidXmlChars(result.StandardOutput)); writer.WriteEndElement(); writer.WriteStartElement("stack-trace"); writer.WriteCData(FilterInvalidXmlChars(result.StandardError)); writer.WriteEndElement(); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); } var failureStr = failures + timeouts != 0 ? $" ({failures} failures, {timeouts} timeouts)" : string.Empty; Console.ForegroundColor = failures + timeouts != 0 ? ConsoleColor.Red : ConsoleColor.Green; Console.WriteLine($"[{sw.Elapsed.ToString ("G")}] Finished with {successes}/{results.Count} passing tests{failureStr}"); Console.ResetColor(); return(failures + timeouts); }
internal static void CloseFD(int fd) { Syscall.close(fd); }
/* * Loads the specific agent assembly into this vm. */ public void Attach(string agent, string args) { string user = UnixUserInfo.GetRealUser().UserName; // Check whenever the attach socket exists string socket_file = "/tmp/mono-" + user + "/.mono-" + pid; if (!File.Exists(socket_file)) { string trigger_file = "/tmp/.mono_attach_pid" + pid; FileStream trigger = null; try { trigger = File.Create(trigger_file); trigger.Close(); // Ask the vm to start the attach mechanism Syscall.kill((int)pid, Signum.SIGQUIT); // Wait for the socket file to materialize int i; for (i = 0; i < 10; ++i) { if (File.Exists(socket_file)) { break; } Thread.Sleep(100); } if (i == 10) { throw new Exception(String.Format("Runtime failed to create attach socket '{0}'.", socket_file)); } } finally { File.Delete(trigger_file); } } /* * We communicate with the agent inside the runtime using a simlified * version of the .net remoting protocol. */ string path = "/tmp/mono-" + user + "/.mono-" + pid; UnixClient client = new UnixClient(path); NetworkStream stream = client.GetStream(); // Compose payload MemoryStream ms = new MemoryStream(); BinaryWriter writer = new BinaryWriter(ms); write_string(writer, "attach"); write_string(writer, agent); write_string(writer, args); // Write header byte[] magic = new byte [] { (byte)'M', (byte)'O', (byte)'N', (byte)'O', 1, 0 }; stream.Write(magic, 0, magic.Length); // Write payload length new BinaryWriter(stream).Write((int)ms.Length); // Write payload stream.Write(ms.GetBuffer(), 0, (int)ms.Length); }
public static DiskSpaceResult GetFreeDiskSpace(string pathToCheck, DriveInfo[] drivesInfo) { if (string.IsNullOrEmpty(pathToCheck)) { return(null); } if (PlatformDetails.RunningOnPosix) { var statvfs = default(Statvfs); if (Syscall.statvfs(pathToCheck, ref statvfs) != 0) { return(null); } return(new DiskSpaceResult { DriveName = Syscall.GetRootMountString(drivesInfo, pathToCheck), TotalFreeSpace = new Size((long)(statvfs.f_bsize * statvfs.f_bfree), SizeUnit.Bytes), TotalSize = new Size((long)(statvfs.f_bsize * statvfs.f_blocks), SizeUnit.Bytes) }); } if (Path.IsPathRooted(pathToCheck) && pathToCheck.StartsWith("\\\\") == false) { var root = Path.GetPathRoot(pathToCheck); foreach (var drive in drivesInfo) { if (root.IndexOf(drive.Name, StringComparison.OrdinalIgnoreCase) < 0) { continue; } return(new DiskSpaceResult { DriveName = root, VolumeLabel = drive.VolumeLabel, TotalFreeSpace = new Size(drive.TotalFreeSpace, SizeUnit.Bytes), TotalSize = new Size(drive.TotalSize, SizeUnit.Bytes) }); } return(null); } if (pathToCheck.StartsWith("\\\\")) { var uncRoot = Path.GetPathRoot(pathToCheck); var success = GetDiskFreeSpaceEx(uncRoot, out ulong freeBytesAvailable, out ulong totalNumberOfBytes, out _); if (success == false) { return(null); } return(new DiskSpaceResult { DriveName = uncRoot, TotalFreeSpace = new Size((long)freeBytesAvailable, SizeUnit.Bytes), TotalSize = new Size((long)totalNumberOfBytes, SizeUnit.Bytes) }); } return(null); }
protected override bool ReleaseHandle() { return(Syscall.close(handle.ToInt32()) != -1); }
private byte[] LoadMasterKey() { var debug = "<unknown>"; try { if (_config.MasterKeyExec != null) { debug = _config.MasterKeyExec + " " + _config.MasterKeyExecArguments; return(LoadMasterKeyWithExecutable()); } if (_config.MasterKeyPath != null) { debug = _config.MasterKeyPath; return(LoadMasterKeyFromPath()); } if (PlatformDetails.RunningOnPosix == false) { return(null); } var dirpath = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".ravendb"); dirpath = Path.GetFullPath(dirpath); var filepath = Path.Combine(dirpath, "secret.key"); debug = filepath; var buffer = new byte[(int)Sodium.crypto_aead_xchacha20poly1305_ietf_keybytes()]; fixed(byte *pBuf = buffer) { if (Directory.Exists(dirpath) == false) { Directory.CreateDirectory(dirpath); } var fd = Syscall.open(filepath, PerPlatformValues.OpenFlags.O_CREAT | OpenFlags.O_RDWR, // octal 01600 - Sticky and only user can read it FilePermissions.S_ISVTX | FilePermissions.S_IRUSR | FilePermissions.S_IWUSR); if (fd == -1) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"when opening {filepath}"); } try { var ret = Syscall.flock(fd, Syscall.FLockOperations.LOCK_EX); if (ret != 0) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"could not lock {filepath}"); } var size = Syscall.lseek64(fd, 0, WhenceFlags.SEEK_END); if (size == -1) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"could not get size of {filepath}"); } if (size == buffer.Length) { byte *pos = pBuf; long amountRead = 0; while (amountRead < buffer.Length) { var read = Syscall.pread(fd, pos, (ulong)(buffer.Length - amountRead), amountRead); pos += read; if (read < 0) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"failed to read {filepath}"); } if (read == 0) { break; } amountRead += read; } if (amountRead != buffer.Length) { throw new FileLoadException($"Failed to read the full key size from {filepath}, expected to read {buffer.Length} but go only {amountRead}"); } } else // we assume that if the size isn't a key size, then it was never valid and regenerate the key { Sodium.randombytes_buf(pBuf, (UIntPtr)buffer.Length); if (Syscall.ftruncate(fd, IntPtr.Zero) != 0) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Failed to truncate {filepath}"); } if (Syscall.lseek64(fd, 0, WhenceFlags.SEEK_SET) == -1) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Failed to seek to beginning of {filepath}"); } var len = buffer.Length; while (len > 0) { var writeAmount = Syscall.write(fd, pBuf, (ulong)buffer.Length); if (writeAmount <= 0) // 0 will be considered as error here { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Failed to write {buffer.Length} bytes into {filepath}, only wrote {len}"); } len -= (int)writeAmount; } if (Syscall.FSync(fd) != 0) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Failed to FSync {filepath}"); } Syscall.FsyncDirectoryFor(filepath); } } finally { if (Syscall.close(fd) != 0) { var err = Marshal.GetLastWin32Error(); Syscall.ThrowLastError(err, $"Failed to close the secret key file : {filepath}"); } } return(buffer); } } catch (Exception e) { throw new CryptographicException( $"Unable to open the master secret key ({debug}), won't proceed because losing this key will lose access to all user encrypted information. Admin assistance required.", e); } }
private UnixStream OpenSocket(uint?mtu) { sockaddr_ll sa = new sockaddr_ll(); sa.sll_family = AF_PACKET; sa.sll_protocol = (ushort)IPAddress.HostToNetworkOrder((short)Protocol); sa.sll_ifindex = if_nametoindex(Interface); if (sa.sll_ifindex == 0) { throw new ArgumentException("The interface \"" + Interface + "\" is not valid."); } int fd = -1, ret = -1; try { fd = socket(AF_PACKET, SOCK_RAW, sa.sll_protocol); UnixMarshal.ThrowExceptionForLastErrorIf(fd); ret = bind(fd, ref sa, Marshal.SizeOf(sa)); UnixMarshal.ThrowExceptionForLastErrorIf(ret); ifreq ifr = new ifreq(Interface); if (orig_mtu == 0) { ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); orig_mtu = ifr.ifru_mtu; } if (mtu != null) { ifr.ifru_mtu = mtu.Value; ret = ioctl(fd, SIOCSIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); } ret = ioctl(fd, SIOCGIFMTU, ref ifr); UnixMarshal.ThrowExceptionForLastErrorIf(ret); if (mtu != null && ifr.ifru_mtu != mtu.Value) { throw new PeachException("MTU change did not take effect."); } _mtu = ifr.ifru_mtu; if (ifr.ifru_mtu > (MaxMTU - EthernetHeaderSize)) { _bufferSize = (int)MaxMTU; } else { _bufferSize = (int)(ifr.ifru_mtu + EthernetHeaderSize); } var stream = new UnixStream(fd); fd = -1; return(stream); } catch (InvalidOperationException ex) { if (ex.InnerException != null) { var inner = ex.InnerException as UnixIOException; if (inner != null && inner.ErrorCode == Errno.EPERM) { throw new PeachException("Access denied when opening the raw ethernet publisher. Ensure the user has the appropriate permissions.", ex); } } throw; } finally { if (fd != -1) { Syscall.close(fd); } } }
public Documents RasterizeOne(string fullyQualifiedFileName, string clientFileName, string guid, Person uploader, Organization organization = null, ProgressRange progressRange = null) { int pdfPageCount = GetPageCount(fullyQualifiedFileName); string relativeFileName = fullyQualifiedFileName.Substring(Document.StorageRoot.Length); Process process = Process.Start("bash", "-c \"convert -density 75 -background white -alpha remove " + fullyQualifiedFileName + " " + fullyQualifiedFileName + "-%04d.png\""); Documents documents = new Documents(); if (progressRange == null) { progressRange = new ProgressRange(); } int pageCounter = 0; // the first produced page will be zero string testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); string lastPageFileName = testPageFileName; // Convert works by first calling imagemagick that creates /tmp/magick-* files int lastProgress = progressRange.Minimum; int progress = progressRange.Minimum; while (pageCounter < pdfPageCount) { while (!File.Exists(Document.StorageRoot + testPageFileName)) { // Wait for file to appear if (!process.HasExited) { process.WaitForExit(250); } if (pageCounter == 0) { // If first page hasn't appeared yet, check for the Magick temp files int currentMagickCount = Directory.GetFiles("/tmp", "magick-*").Count(); int currentFilePercentage = currentMagickCount * 50 / pdfPageCount; if (currentFilePercentage > 50) { currentFilePercentage = 50; // we may be not the only one converting right now } progress = progressRange.Minimum + currentFilePercentage * 100 / progressRange.Range; if (progress > lastProgress) // can't use Not-Equal; temp files slowly deleted before next step { BroadcastProgress(organization, guid, progress); lastProgress = progress; } } } progress = progressRange.Minimum + progressRange.Range / 2 + ((pageCounter + 1) * progressRange.Range / pdfPageCount) / 2; if (progress > lastProgress) { BroadcastProgress(organization, guid, progress); lastProgress = progress; } // If the page# file that has appeared is 1+, then the preceding file is ready if (pageCounter > 0) { long fileLength = new FileInfo(Document.StorageRoot + lastPageFileName).Length; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + (pageCounter).ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLength, guid, null, uploader)); // Set to readonly, lock out changes, permit all read Syscall.chmod(Document.StorageRoot + lastPageFileName, FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); // Prepare to save the next file lastPageFileName = testPageFileName; } // Increase the page counter and the file we're looking for pageCounter++; testPageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); } // We've seen the last page being written -- wait for process to exit to assure it's complete if (!process.HasExited) { process.WaitForExit(); } // Save the last page long fileLengthLastPage = new FileInfo(Document.StorageRoot + lastPageFileName).Length; documents.Add(Document.Create(lastPageFileName, clientFileName + " {{LOCPAGE-" + (pageCounter).ToString(CultureInfo.InvariantCulture) + "-" + pdfPageCount.ToString(CultureInfo.InvariantCulture) + "}}", fileLengthLastPage, guid, null, uploader)); // Set to readonly, lock out changes, permit all read Syscall.chmod(Document.StorageRoot + lastPageFileName, FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); /* -- OLD CODE BELOW -- * process.WaitForExit(); * * int pageCounter = 0; // the first produced page will be zero * * // Create all document records * * * while (pageCounter < pdfPageCount) * { * string pageFileName = String.Format("{0}-{1:D4}.png", relativeFileName, pageCounter); * * if (File.Exists(Document.StorageRoot + pageFileName)) * { * long fileLength = new FileInfo(Document.StorageRoot + pageFileName).Length; * * documents.Add(Document.Create(pageFileName, * clientFileName + " " + (pageCounter + 1).ToString(CultureInfo.InvariantCulture) + "/" + pdfPageCount.ToString(CultureInfo.InvariantCulture), * fileLength, guid, null, uploader)); * * Syscall.chmod(Document.StorageRoot + pageFileName, * FilePermissions.S_IRUSR | FilePermissions.S_IRGRP | FilePermissions.S_IROTH); * * } * * pageCounter++; * } */ return(documents); }
protected override void OnInput() { System.Diagnostics.Debug.Assert(_socket != null); if (_recvBuffer == null || _recvBuffer.Capacity < _bufferSize) { _recvBuffer = new MemoryStream(_bufferSize); } _recvBuffer.Seek(0, SeekOrigin.Begin); _recvBuffer.SetLength(_recvBuffer.Capacity); byte[] buf = _recvBuffer.GetBuffer(); int offset = (int)_recvBuffer.Position; int size = (int)_recvBuffer.Length; Pollfd[] fds = new Pollfd[1]; fds[0].fd = _socket.Handle; fds[0].events = PollEvents.POLLIN; int expires = Environment.TickCount + Timeout; int wait = 0; for (;;) { try { wait = Math.Max(0, expires - Environment.TickCount); fds[0].revents = 0; int ret = Syscall.poll(fds, wait); if (UnixMarshal.ShouldRetrySyscall(ret)) { continue; } UnixMarshal.ThrowExceptionForLastErrorIf(ret); if (ret == 0) { throw new TimeoutException(); } if (ret != 1 || (fds[0].revents & PollEvents.POLLIN) == 0) { continue; } var rxLen = _socket.Read(buf, offset, size); _recvBuffer.SetLength(rxLen); if (Logger.IsDebugEnabled) { Logger.Debug("\n\n" + Utilities.HexDump(_recvBuffer)); } // Got a valid packet return; } catch (Exception ex) { if (ex is TimeoutException) { Logger.Debug("Ethernet packet not received on {0} in {1}ms, timing out.", Interface, Timeout); } else { Logger.Error("Unable to receive ethernet packet on {0}. {1}", Interface, ex.Message); } throw new SoftException(ex); } } }
public void TestOFDLock() { int fd1 = Syscall.open(TempFolder + "/testfile", OpenFlags.O_RDWR | OpenFlags.O_CREAT | OpenFlags.O_EXCL, FilePermissions.DEFFILEMODE); if (fd1 < 0) { UnixMarshal.ThrowExceptionForLastError(); } int fd2 = Syscall.open(TempFolder + "/testfile", OpenFlags.O_RDWR); if (fd2 < 0) { UnixMarshal.ThrowExceptionForLastError(); } int fd3 = Syscall.open(TempFolder + "/testfile", OpenFlags.O_RDWR); if (fd3 < 0) { UnixMarshal.ThrowExceptionForLastError(); } // Get read lock for first 100 bytes on fd1 var flock1 = new Flock { l_type = LockType.F_RDLCK, l_whence = SeekFlags.SEEK_SET, l_start = 0, l_len = 100, }; if (Syscall.fcntl(fd1, FcntlCommand.F_OFD_SETLKW, ref flock1) < 0) { // Old kernels and non-linux systems should return EINVAL if (Stdlib.GetLastError() == Errno.EINVAL) { Assert.Ignore("F_OFD_SETLKW does not seem to be supported."); } UnixMarshal.ThrowExceptionForLastError(); } // Get read lock for first 100 bytes on fd2 var flock2 = new Flock { l_type = LockType.F_RDLCK, l_whence = SeekFlags.SEEK_SET, l_start = 0, l_len = 100, }; if (Syscall.fcntl(fd2, FcntlCommand.F_OFD_SETLK, ref flock2) < 0) { UnixMarshal.ThrowExceptionForLastError(); } // Get write lock for remaining bytes on fd1 var flock3 = new Flock { l_type = LockType.F_WRLCK, l_whence = SeekFlags.SEEK_SET, l_start = 100, l_len = 0, }; if (Syscall.fcntl(fd1, FcntlCommand.F_OFD_SETLK, ref flock3) < 0) { UnixMarshal.ThrowExceptionForLastError(); } // Close fd3, should not release lock if (Syscall.close(fd3) < 0) { UnixMarshal.ThrowExceptionForLastError(); } // Get lock status for byte 150 from fd2 var flock4 = new Flock { l_type = LockType.F_RDLCK, l_whence = SeekFlags.SEEK_SET, l_start = 150, l_len = 1, }; if (Syscall.fcntl(fd2, FcntlCommand.F_OFD_GETLK, ref flock4) < 0) { UnixMarshal.ThrowExceptionForLastError(); } // There should be a conflicting write lock Assert.AreEqual(LockType.F_WRLCK, flock4.l_type); // Get write byte 0 on fd1, should fail with EAGAIN var flock5 = new Flock { l_type = LockType.F_WRLCK, l_whence = SeekFlags.SEEK_SET, l_start = 0, l_len = 1, }; var res = Syscall.fcntl(fd1, FcntlCommand.F_OFD_SETLK, ref flock5); Assert.AreEqual(-1, res); Assert.AreEqual(Errno.EAGAIN, Stdlib.GetLastError()); if (Syscall.close(fd1) < 0) { UnixMarshal.ThrowExceptionForLastError(); } if (Syscall.close(fd2) < 0) { UnixMarshal.ThrowExceptionForLastError(); } }
protected override void OnOutput(byte[] buf, int offset, int count) { int size = count; Pollfd[] fds = new Pollfd[1]; fds[0].fd = _socket.Handle; fds[0].events = PollEvents.POLLOUT; if (Logger.IsDebugEnabled) { Logger.Debug("\n\n" + Utilities.HexDump(buf, offset, count)); } int expires = Environment.TickCount + Timeout; int wait = 0; for (;;) { try { wait = Math.Max(0, expires - Environment.TickCount); fds[0].revents = 0; int ret = Syscall.poll(fds, wait); if (UnixMarshal.ShouldRetrySyscall(ret)) { continue; } UnixMarshal.ThrowExceptionForLastErrorIf(ret); if (ret == 0) { throw new TimeoutException(); } if (ret != 1 || (fds[0].revents & PollEvents.POLLOUT) == 0) { continue; } _socket.Write(buf, offset, size); return; } catch (Exception ex) { if (ex is TimeoutException) { Logger.Debug("Ethernet packet not sent to {0} in {1}ms, timing out.", Interface, Timeout); } else { Logger.Error("Unable to send ethernet packet to {0}. {1}", Interface, ex.Message); } throw new SoftException(ex); } } }
public override bool Verify() { ZipFile file = null; try { file = new ZipFile(File.OpenRead(this.file)); int num = 0; string str = null; IEnumerator enumerator = file.GetEnumerator(); try { while (enumerator.MoveNext()) { ZipEntry current = (ZipEntry)enumerator.Current; if (!current.IsDirectory) { string name = current.Name; if (name.EndsWith("ivy.xml")) { name = name.Replace("ivy.xml", "ivy-waiting-for-unzip-to-end"); str = Path.Combine(this.localPath, Path.GetDirectoryName(name)); } string str3 = Path.Combine(this.localPath, name); if (!File.Exists(str3)) { Console.WriteLine("ZipVerifier: file doesn't exist: {0}", str3); return(false); } if (((Environment.OSVersion.Platform == PlatformID.Unix) || (Environment.OSVersion.Platform == PlatformID.MacOSX)) && (current.ExternalFileAttributes != 0)) { Stat stat; if (Syscall.stat(str3, out stat) != 0) { Console.WriteLine("ZipVerifier: couldn't stat {0}", str3); return(false); } if (((FilePermissions)current.ExternalFileAttributes) != (((FilePermissions)current.ExternalFileAttributes) & stat.st_mode)) { Console.WriteLine("ZipVerifier: permissions don't match: {0} vs {1}", current.ExternalFileAttributes, stat.st_mode); return(false); } } this.UpdateProgress(((float)(++num)) / ((float)file.Size)); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } string path = Path.Combine(str, "ivy-waiting-for-unzip-to-end"); if (!File.Exists(path)) { Console.WriteLine("ZipVerifier: Missing package description for {0}", str); return(false); } File.Move(path, Path.Combine(str, "ivy.xml")); } catch { throw; } finally { if (file != null) { file.Close(); } } return(true); }
public Syscall64(Syscall syscall) { _syscall = syscall; }
private void TransferFilePatched(string source, string destination, bool overwrite, bool move) { // Mono 6.x throws errors if permissions or timestamps cannot be set // - In 6.0 it'll leave a full length file // - In 6.6 it'll leave a zero length file // Catch the exception and attempt to handle these edgecases // Mono 6.x till 6.10 doesn't properly try use rename first. if (move && ((PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() < new Version(6, 10)) || (PlatformInfo.Platform == PlatformType.NetCore))) { if (Syscall.lstat(source, out var sourcestat) == 0 && Syscall.lstat(destination, out var deststat) != 0 && Syscall.rename(source, destination) == 0) { _logger.Trace("Moved '{0}' -> '{1}' using Syscall.rename", source, destination); return; } } try { if (move) { base.MoveFileInternal(source, destination); } else { base.CopyFileInternal(source, destination); } } catch (UnauthorizedAccessException) { var srcInfo = new FileInfo(source); var dstInfo = new FileInfo(destination); var exists = dstInfo.Exists && srcInfo.Exists; if (PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() >= new Version(6, 6) && exists && dstInfo.Length == 0 && srcInfo.Length != 0) { // mono >=6.6 bug: zero length file since chmod happens at the start _logger.Debug("{3} failed to {2} file likely due to known {3} bug, attempting to {2} directly. '{0}' -> '{1}'", source, destination, move ? "move" : "copy", PlatformInfo.PlatformName); try { _logger.Trace("Copying content from {0} to {1} ({2} bytes)", source, destination, srcInfo.Length); using (var srcStream = new FileStream(source, FileMode.Open, FileAccess.Read)) using (var dstStream = new FileStream(destination, FileMode.Create, FileAccess.Write)) { srcStream.CopyTo(dstStream); } } catch { // If it fails again then bail throw; } } else if (((PlatformInfo.Platform == PlatformType.Mono && PlatformInfo.GetVersion() >= new Version(6, 0) && PlatformInfo.GetVersion() < new Version(6, 6)) || PlatformInfo.Platform == PlatformType.NetCore) && exists && dstInfo.Length == srcInfo.Length) { // mono 6.0, mono 6.4 and netcore 3.1 bug: full length file since utime and chmod happens at the end _logger.Debug("{3} failed to {2} file likely due to known {3} bug, attempting to {2} directly. '{0}' -> '{1}'", source, destination, move ? "move" : "copy", PlatformInfo.PlatformName); // Check at least part of the file since UnauthorizedAccess can happen due to legitimate reasons too var checkLength = (int)Math.Min(64 * 1024, dstInfo.Length); if (checkLength > 0) { var srcData = new byte[checkLength]; var dstData = new byte[checkLength]; _logger.Trace("Check last {0} bytes from {1}", checkLength, destination); using (var srcStream = new FileStream(source, FileMode.Open, FileAccess.Read)) using (var dstStream = new FileStream(destination, FileMode.Open, FileAccess.Read)) { srcStream.Position = srcInfo.Length - checkLength; dstStream.Position = dstInfo.Length - checkLength; srcStream.Read(srcData, 0, checkLength); dstStream.Read(dstData, 0, checkLength); } for (var i = 0; i < checkLength; i++) { if (srcData[i] != dstData[i]) { // Files aren't the same, the UnauthorizedAccess was unrelated _logger.Trace("Copy was incomplete, rethrowing original error"); throw; } } _logger.Trace("Copy was complete, finishing {0} operation", move ? "move" : "copy"); } } else { // Unrecognized situation, the UnauthorizedAccess was unrelated throw; } if (exists) { try { dstInfo.LastWriteTimeUtc = srcInfo.LastWriteTimeUtc; } catch { _logger.Debug("Unable to change last modified date for {0}, skipping.", destination); } if (move) { _logger.Trace("Removing source file {0}", source); File.Delete(source); } } } }
public static IServiceCollection AddFtpServices( this IServiceCollection services, FtpOptions options) { services .Configure <AuthTlsOptions>( opt => { opt.ServerCertificate = options.GetCertificate(); opt.ImplicitFtps = options.Ftps.Implicit; }) .Configure <FtpConnectionOptions>( opt => opt.DefaultEncoding = Encoding.ASCII) .Configure <FubarDev.FtpServer.FtpServerOptions>( opt => { opt.ServerAddress = options.Server.Address; opt.Port = options.GetServerPort(); opt.MaxActiveConnections = options.Server.MaxActiveConnections ?? 0; opt.ConnectionInactivityCheckInterval = ToTimeSpan(options.Server.ConnectionInactivityCheckInterval); }) .Configure <PortCommandOptions>( opt => { if (options.Server.UseFtpDataPort) { opt.DataPort = options.GetServerPort() - 1; } }) .Configure <SimplePasvOptions>( opt => { var portRange = options.GetPasvPortRange(); if (portRange != null) { (opt.PasvMinPort, opt.PasvMaxPort) = portRange.Value; } }) .Configure <PasvCommandOptions>(opt => opt.PromiscuousPasv = options.Server.Pasv.Promiscuous) .Configure <GoogleDriveOptions>(opt => opt.UseBackgroundUpload = options.GoogleDrive.BackgroundUpload) .Configure <FileSystemAmazonS3Options>( opt => { opt.BucketName = options.AmazonS3.BucketName; opt.BucketRegion = options.AmazonS3.BucketRegion; opt.AwsAccessKeyId = options.AmazonS3.AwsAccessKeyId; opt.AwsSecretAccessKey = options.AmazonS3.AwsSecretAccessKey; }); #if NETCOREAPP services .Configure <PamMembershipProviderOptions>( opt => opt.IgnoreAccountManagement = options.Pam.NoAccountManagement); #endif // Add "Hello" service - unique per FTP connection services.AddScoped <Hello>(); // Add custom command handlers services.AddSingleton <IFtpCommandHandlerScanner>( _ => new AssemblyFtpCommandHandlerScanner(typeof(HelloFtpCommandHandler).Assembly)); // Add custom command handler extensions services.AddSingleton <IFtpCommandHandlerExtensionScanner>( sp => new AssemblyFtpCommandHandlerExtensionScanner( sp.GetRequiredService <IFtpCommandHandlerProvider>(), sp.GetService <ILogger <AssemblyFtpCommandHandlerExtensionScanner> >(), typeof(SiteHelloFtpCommandHandlerExtension).Assembly)); #if NETCOREAPP if (options.SetFileSystemId && RuntimeEnvironment.OperatingSystemPlatform != Microsoft.DotNet.PlatformAbstractions.Platform.Windows) { services.AddScoped <IFtpCommandMiddleware, FsIdChanger>(); } #endif switch (options.BackendType) { case FileSystemType.InMemory: services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseInMemoryFileSystem().ConfigureServer(options)) .Configure <InMemoryFileSystemOptions>( opt => opt.KeepAnonymousFileSystem = options.InMemory.KeepAnonymous); break; case FileSystemType.Unix: #if NETCOREAPP services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseUnixFileSystem().ConfigureServer(options)) .Configure <UnixFileSystemOptions>( opt => { opt.Root = options.Unix.Root; opt.FlushAfterWrite = options.Unix.FlushAfterWrite; }); #else services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseDotNetFileSystem().ConfigureServer(options)) .Configure <DotNetFileSystemOptions>( opt => { opt.RootPath = options.Unix.Root; opt.FlushAfterWrite = options.Unix.FlushAfterWrite; }); #endif break; case FileSystemType.SystemIO: services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseDotNetFileSystem().ConfigureServer(options)) .Configure <DotNetFileSystemOptions>( opt => { opt.RootPath = options.SystemIo.Root; opt.FlushAfterWrite = options.SystemIo.FlushAfterWrite; }); break; case FileSystemType.GoogleDriveUser: var userCredential = GetUserCredential( options.GoogleDrive.User.ClientSecrets ?? throw new ArgumentNullException( nameof(options.GoogleDrive.User.ClientSecrets), "Client secrets file not specified."), options.GoogleDrive.User.UserName ?? throw new ArgumentNullException( nameof(options.GoogleDrive.User.ClientSecrets), "User name not specified."), options.GoogleDrive.User.RefreshToken); services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseGoogleDrive(userCredential).ConfigureServer(options)); break; case FileSystemType.GoogleDriveService: var serviceCredential = GoogleCredential .FromFile(options.GoogleDrive.Service.CredentialFile) .CreateScoped(DriveService.Scope.Drive, DriveService.Scope.DriveFile); services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseGoogleDrive(serviceCredential).ConfigureServer(options)); break; case FileSystemType.AmazonS3: services = services .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseS3FileSystem().ConfigureServer(options)); break; default: throw new NotSupportedException( $"Backend of type {options.Backend} cannot be run from configuration file options."); } switch (options.LayoutType) { case FileSystemLayoutType.SingleRoot: services.AddSingleton <IAccountDirectoryQuery, SingleRootWithoutHomeAccountDirectoryQuery>(); break; case FileSystemLayoutType.PamHome: #if NETCOREAPP services .AddSingleton <IAccountDirectoryQuery, PamAccountDirectoryQuery>() .Configure <PamAccountDirectoryQueryOptions>( opt => opt.AnonymousRootDirectory = Path.GetTempPath()); break; #endif case FileSystemLayoutType.PamHomeChroot: #if NETCOREAPP services .AddSingleton <IAccountDirectoryQuery, PamAccountDirectoryQuery>() .Configure <PamAccountDirectoryQueryOptions>( opt => { opt.AnonymousRootDirectory = Path.GetTempPath(); opt.UserHomeIsRoot = true; }); break; #endif case FileSystemLayoutType.RootPerUser: services .AddSingleton <IAccountDirectoryQuery, RootPerUserAccountDirectoryQuery>() .Configure <RootPerUserAccountDirectoryQueryOptions>(opt => opt.AnonymousRootPerEmail = true); break; } if (options.Ftps.Implicit) { var implicitFtpsCertificate = options.GetCertificate(); if (implicitFtpsCertificate != null) { services .AddSingleton(new ImplicitFtpsControlConnectionStreamAdapterOptions(implicitFtpsCertificate)) .AddSingleton <IFtpControlStreamAdapter, ImplicitFtpsControlConnectionStreamAdapter>(); // Ensure that PROT and PBSZ commands are working. services.Decorate <IFtpServer>( (ftpServer, _) => { ftpServer.ConfigureConnection += (s, e) => { var serviceProvider = e.Connection.ConnectionServices; var stateMachine = serviceProvider.GetRequiredService <IFtpLoginStateMachine>(); var authTlsMechanism = serviceProvider.GetRequiredService <IEnumerable <IAuthenticationMechanism> >() .Single(x => x.CanHandle("TLS")); stateMachine.Activate(authTlsMechanism); }; return(ftpServer); }); } } #if NETCOREAPP services.Decorate <IFtpServer>( (ftpServer, serviceProvider) => { /* Setting the umask is only valid for non-Windows platforms. */ if (!string.IsNullOrEmpty(options.Umask) && RuntimeEnvironment.OperatingSystemPlatform != Microsoft.DotNet.PlatformAbstractions.Platform.Windows) { var umask = options.Umask !.StartsWith("0") ? Convert.ToInt32(options.Umask, 8) : Convert.ToInt32(options.Umask, 10); Syscall.umask((FilePermissions)umask); } return(ftpServer); }); #endif services.Scan( ts => ts .FromAssemblyOf <HostedFtpService>() .AddClasses(itf => itf.AssignableTo <IModuleInfo>(), true).As <IModuleInfo>() .WithSingletonLifetime()); return(services); }
static void error(string prefix, string format, params object [] args) { Syscall.syslog(SyslogLevel.LOG_ERR, String.Format("{0}: {1}", prefix, String.Format(format, args))); }
public UnixMultiProcessFileAppender(string fileName, ICreateFileParameters parameters) : base(fileName, parameters) { UnixFileInfo fileInfo = null; bool fileExists = false; try { fileInfo = new UnixFileInfo(fileName); fileExists = fileInfo.Exists; } catch { } int fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6))); if (fd == -1) { if (Stdlib.GetLastError() == Errno.ENOENT && parameters.CreateDirs) { string dirName = Path.GetDirectoryName(fileName); if (!Directory.Exists(dirName) && parameters.CreateDirs) { Directory.CreateDirectory(dirName); } fd = Syscall.open(fileName, OpenFlags.O_CREAT | OpenFlags.O_WRONLY | OpenFlags.O_APPEND, (FilePermissions)(6 | (6 << 3) | (6 << 6))); } } if (fd == -1) { UnixMarshal.ThrowExceptionForLastError(); } try { _file = new UnixStream(fd, true); long filePosition = _file.Position; if (fileExists || filePosition > 0) { if (fileInfo != null) { CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileInfo, (f) => File.GetCreationTimeUtc(f.FullName), (f) => { f.Refresh(); return(f.LastStatusChangeTimeUtc); }, (f) => DateTime.UtcNow).Value; } else { CreationTimeUtc = FileCharacteristicsHelper.ValidateFileCreationTime(fileName, (f) => File.GetCreationTimeUtc(f), (f) => DateTime.UtcNow).Value; } } else { // We actually created the file and eventually concurrent processes CreationTimeUtc = DateTime.UtcNow; File.SetCreationTimeUtc(FileName, CreationTimeUtc); } } catch { Syscall.close(fd); throw; } }
public static void OutputResultToStream(TextWriter sw, HashSet <string> threadIds = null, bool includeStackObjects = false) { var ravenDebugExec = Path.Combine(AppContext.BaseDirectory, PlatformDetails.RunningOnPosix ? "Raven.Debug" : "Raven.Debug.exe" ); if (File.Exists(ravenDebugExec) == false) { throw new FileNotFoundException($"Could not find debugger tool at '{ravenDebugExec}'"); } using (var currentProcess = Process.GetCurrentProcess()) { var sb = new StringBuilder($"stack-traces --pid {currentProcess.Id}"); if (PlatformDetails.RunningOnPosix && PlatformDetails.RunningOnMacOsx == false) { sb.Append(" --wait"); } if (threadIds != null && threadIds.Count > 0) { foreach (var threadId in threadIds) { if (int.TryParse(threadId, out _) == false) { throw new ArgumentException($"Could not parse thread id with value '{threadId}' to number."); } sb.Append($" --tid {threadId}"); } } if (includeStackObjects) { sb.Append(" --includeStackObjects"); } var startup = new ProcessStartInfo { Arguments = sb.ToString(), FileName = ravenDebugExec, WindowStyle = ProcessWindowStyle.Normal, RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, UseShellExecute = false }; if (PlatformDetails.RunningOnPosix == false) { startup.LoadUserProfile = false; } var process = new Process { StartInfo = startup, EnableRaisingEvents = true }; sb.Clear(); process.OutputDataReceived += (sender, args) => sw.Write(args.Data); process.ErrorDataReceived += (sender, args) => sb.Append(args.Data); process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); if (PlatformDetails.RunningOnPosix && PlatformDetails.RunningOnMacOsx == false) { // enable this process to attach to us Syscall.prctl(Syscall.PR_SET_PTRACER, new UIntPtr((uint)process.Id), UIntPtr.Zero, UIntPtr.Zero, UIntPtr.Zero); process.StandardInput.WriteLine("go");// value is meaningless, just need a new line process.StandardInput.Flush(); } try { process.WaitForExit(); } finally { if (PlatformDetails.RunningOnPosix && PlatformDetails.RunningOnMacOsx == false) { // disable attachments Syscall.prctl(Syscall.PR_SET_PTRACER, UIntPtr.Zero, UIntPtr.Zero, UIntPtr.Zero, UIntPtr.Zero); } } if (process.ExitCode != 0) { throw new InvalidOperationException("Could not read stack traces, " + $"exit code: {process.ExitCode}, error: {sb}"); } } }
bool check_log() { bool retval; Syscall[] expected; int i; int num_syscalls; retval = false; expected = new Syscall[6]; expected[0].index = 0; expected[0].pid = 3630; expected[0].tid = 3630; expected[0].execname = "gnome-panel"; expected[0].timestamp = 1180976736974992; expected[0].name = "open"; expected[0].arguments = "\"/proc/partitions\", O_RDONLY"; expected[0].extra_info = null; expected[0].have_result = false; expected[0].result = -1; expected[0].is_syscall_start = true; expected[0].end_index = 1; expected[0].is_syscall_end = false; expected[0].start_index = -1; expected[1].index = 1; expected[1].pid = 3630; expected[1].tid = 3630; expected[1].execname = "gnome-panel"; expected[1].timestamp = 1180976736975010; expected[1].name = "open"; expected[1].arguments = null; expected[1].extra_info = null; expected[1].have_result = true; expected[1].result = 27; expected[1].is_syscall_start = false; expected[1].end_index = -1; expected[1].is_syscall_end = true; expected[1].start_index = 0; expected[2].index = 2; expected[2].pid = 2882; expected[2].tid = 2883; expected[2].execname = "hald-addon-stor"; expected[2].timestamp = 1181064007999786; expected[2].name = "open"; expected[2].arguments = "\"/dev/hdc\", O_RDONLY|O_EXCL|O_LARGEFILE|O_NONBLOCK"; expected[2].extra_info = null; expected[2].have_result = false; expected[2].result = -1; expected[2].is_syscall_start = true; expected[2].end_index = -1; expected[2].is_syscall_end = false; expected[2].start_index = -1; expected[3].index = 3; expected[3].pid = 27920; expected[3].tid = 27920; expected[3].execname = "gimp-2.2"; expected[3].timestamp = 1181064008000173; expected[3].name = "open"; expected[3].arguments = "\"/home/federico/.gimp-2.2/tool-options/gimp-free-select-tool.presetsysBTNg\", O_RDWR|O_CREAT|O_EXCL|O_LARGEFILE, 0600"; expected[3].extra_info = null; expected[3].have_result = false; expected[3].result = -1; expected[3].is_syscall_start = true; expected[3].end_index = 5; expected[3].is_syscall_end = false; expected[3].start_index = -1; expected[4].index = 4; expected[4].pid = 2539; expected[4].tid = 26181; expected[4].execname = "NetworkManager"; expected[4].timestamp = 1181064008031945; expected[4].name = "open"; expected[4].arguments = null; expected[4].extra_info = null; expected[4].have_result = true; expected[4].result = 19; expected[4].is_syscall_start = false; expected[4].end_index = -1; expected[4].is_syscall_end = true; expected[4].start_index = -1; expected[5].index = 5; expected[5].pid = 27920; expected[5].tid = 27920; expected[5].execname = "gimp-2.2"; expected[5].timestamp = 1181064008000205; expected[5].name = "open"; expected[5].arguments = null; expected[5].extra_info = null; expected[5].have_result = true; expected[5].result = 7; expected[5].is_syscall_start = false; expected[5].end_index = -1; expected[5].is_syscall_end = true; expected[5].start_index = 3; for (i = 0; i < expected.Length; i++) { Syscall syscall = log.GetSyscall (i); if (!(syscall.Equals (expected[i]))) goto bail; } num_syscalls = log.GetNumSyscalls (); if (num_syscalls != expected.Length) goto bail; retval = true; bail: return retval; }