Esempio n. 1
0
        public ChildProcessStartInfoInternal(ChildProcessStartInfo startInfo)
        {
            var flags = startInfo.Flags;

            FileName             = startInfo.FileName;
            Arguments            = startInfo.Arguments;
            WorkingDirectory     = startInfo.WorkingDirectory;
            Flags                = flags;
            CodePage             = startInfo.CodePage;
            SearchPath           = startInfo.SearchPath;
            StdInputRedirection  = startInfo.StdInputRedirection;
            StdOutputRedirection = startInfo.StdOutputRedirection;
            StdErrorRedirection  = startInfo.StdErrorRedirection;
            StdInputFile         = startInfo.StdInputFile;
            StdOutputFile        = startInfo.StdOutputFile;
            StdErrorFile         = startInfo.StdErrorFile;
            StdInputHandle       = startInfo.StdInputHandle;
            StdOutputHandle      = startInfo.StdOutputHandle;
            StdErrorHandle       = startInfo.StdErrorHandle;

            if (!flags.HasDisableEnvironmentVariableInheritance() &&
                startInfo.CreationContext is null &&
                startInfo.ExtraEnvironmentVariables.Count == 0)
            {
                UseCustomEnvironmentVariables = false;
                EnvironmentVariables          = default;
            }
Esempio n. 2
0
        private Socket SetPipeSocketInterlocked(Socket socket, bool ownsHandle)
        {
            Debug.Assert(socket != null);

            // Multiple threads may try to create the PipeSocket.
            Socket?current = Interlocked.CompareExchange(ref _pipeSocket, socket, null);

            if (current != null)
            {
                socket.Dispose();
                return(current);
            }

            // If we own the handle, defer ownership to the SocketHandle.
            SafeSocketHandle socketHandle = _pipeSocket.SafeHandle;

            if (ownsHandle)
            {
                _pipeSocketHandle = socketHandle;

                bool ignored = false;
                socketHandle.DangerousAddRef(ref ignored);
            }

            return(socket);
        }
Esempio n. 3
0
 public SecurityDescriptor(SafeHandle objectHandle, ObjectType objectType, SECURITY_DESCRIPTOR *descriptor, bool ownsHandle = true)
     : base((IntPtr)descriptor, ownsHandle)
 {
     _control      = descriptor->Control;
     _objectHandle = objectHandle;
     _objectType   = objectType;
 }
Esempio n. 4
0
        protected SafeNCryptHandle(IntPtr handle, SafeHandle parentHandle)
            : base(true)
        {
            ArgumentNullException.ThrowIfNull(parentHandle);

            if (parentHandle.IsClosed || parentHandle.IsInvalid)
            {
                throw new ArgumentException(SR.Argument_Invalid_SafeHandleInvalidOrClosed, nameof(parentHandle));
            }

            bool success = false;

            parentHandle.DangerousAddRef(ref success);
            _parentHandle = parentHandle;

            // Don't set the handle value until after parentHandle has been validated and persisted to a field,
            // otherwise Dispose will try to call the underlying Free function.
            SetHandle(handle);

            // But if this handle value IsInvalid then we'll never call ReleaseHandle, which leaves the parent open
            // forever.  Instead, release such a parent now.
            if (IsInvalid)
            {
                _parentHandle.DangerousRelease();
                _parentHandle = null;
            }
        }
 public SecurityBuffer(byte[]?data, SecurityBufferType tokentype)
 {
     this.offset         = 0;
     this.size           = data == null ? 0 : data.Length;
     this.type           = tokentype;
     this.token          = size == 0 ? null : data;
     this.unmanagedToken = null;
 }
 public SecurityBuffer(ChannelBinding binding)
 {
     this.offset         = 0;
     this.size           = (binding == null ? 0 : binding.Size);
     this.type           = SecurityBufferType.SECBUFFER_CHANNEL_BINDINGS;
     this.token          = null;
     this.unmanagedToken = binding;
 }
 internal static extern unsafe bool TransmitFile(
     SafeHandle socket,
     SafeHandle?fileHandle,
     int numberOfBytesToWrite,
     int numberOfBytesPerSend,
     NativeOverlapped *overlapped,
     TransmitFileBuffers *buffers,
     TransmitFileOptions flags);
        public SafeHandle AddUnmanagedMemory(int cb)
        {
            if (!(unmanagedMemory is null))
            {
                throw new InvalidOperationException("Memory already allocated");
            }

            return(unmanagedMemory = new SafeHGlobalHandle(Marshal.AllocHGlobal(cb), cb));
        }
Esempio n. 9
0
        internal void SetParent(SafeHandle parent)
        {
            bool addedRef = false;

            parent.DangerousAddRef(ref addedRef);
            Debug.Assert(addedRef);

            _parent = parent;
        }
        public SecurityBuffer(int size, SecurityBufferType tokentype)
        {
            Debug.Assert(size >= 0, $"'size' out of range.  [{size}]");

            this.offset         = 0;
            this.size           = size;
            this.type           = tokentype;
            this.token          = size == 0 ? null : new byte[size];
            this.unmanagedToken = null;
        }
Esempio n. 11
0
        internal void TransferOwnershipToParent(SafeHandle parent)
        {
            Debug.Assert(_parent == null, "Expected no existing parent");
            Debug.Assert(parent != null && !parent.IsInvalid, "Expected new parent to be non-null and valid");

            bool addedRef = false;

            parent.DangerousAddRef(ref addedRef);

            _parent = parent;
        }
            public IOUringAsyncContext(IOUringThread thread, SafeHandle handle)
            {
                _iouring    = thread;
                _writeQueue = new Queue(thread, this);
                _readQueue  = new Queue(thread, this);
                bool success = false;

                handle.DangerousAddRef(ref success);
                _fd     = handle.DangerousGetHandle().ToInt32();
                _handle = handle;
            }
Esempio n. 13
0
        public WicImageContainer(IWICBitmapDecoder *dec, SafeHandle?src, FileFormat fmt)
        {
            WicDecoder = dec;
            handle     = src;

            uint fcount;

            HRESULT.Check(dec->GetFrameCount(&fcount));
            FrameCount      = (int)fcount;
            ContainerFormat = fmt;
        }
Esempio n. 14
0
        internal static unsafe IntPtr ConvertSafeHandleToNative(SafeHandle?handle, ref CleanupWorkListElement?cleanupWorkList)
        {
            if (Unsafe.IsNullRef(ref cleanupWorkList))
            {
                throw new InvalidOperationException(SR.Interop_Marshal_SafeHandle_InvalidOperation);
            }

            ArgumentNullException.ThrowIfNull(handle);

            return(StubHelpers.AddToCleanupList(ref cleanupWorkList, handle));
        }
        public SecurityBuffer(byte[]?data, int offset, int size, SecurityBufferType tokentype)
        {
            Debug.Assert(offset >= 0 && offset <= (data == null ? 0 : data.Length), $"'offset' out of range.  [{offset}]");
            Debug.Assert(size >= 0 && size <= (data == null ? 0 : data.Length - offset), $"'size' out of range.  [{size}]");

            this.offset         = data == null || offset < 0 ? 0 : Math.Min(offset, data.Length);
            this.size           = data == null || size < 0 ? 0 : Math.Min(size, data.Length - this.offset);
            this.type           = tokentype;
            this.token          = size == 0 ? null : data;
            this.unmanagedToken = null;
        }
Esempio n. 16
0
        internal SafePipeHandle(Socket namedPipeSocket) : base(ownsHandle: true)
        {
            Debug.Assert(namedPipeSocket != null);
            _namedPipeSocket = namedPipeSocket;

            _namedPipeSocketHandle = namedPipeSocket.SafeHandle;

            bool ignored = false;

            _namedPipeSocketHandle.DangerousAddRef(ref ignored);
            SetHandle(_namedPipeSocketHandle.DangerousGetHandle());
        }
Esempio n. 17
0
            public IOUringAsyncContext(IOUringThread thread, SafeHandle handle)
            {
                _iouring    = thread;
                _writeQueue = new Queue(thread, this, readNotWrite: false);
                _readQueue  = new Queue(thread, this, readNotWrite: true);
                bool success = false;

                handle.DangerousAddRef(ref success);
                _fd     = handle.DangerousGetHandle().ToInt32();
                _handle = handle;
                SocketPal.SetNonBlocking(handle);
            }
            public EPollAsyncContext(EPollThread thread, SafeHandle handle)
            {
                _epoll      = thread;
                _writeQueue = new Queue(thread);
                _readQueue  = new Queue(thread);
                bool success = false;

                handle.DangerousAddRef(ref success);
                _fd     = handle.DangerousGetHandle().ToInt32();
                _handle = handle;

                _epoll.Control(EPOLL_CTL_ADD, _fd, EPOLLIN | EPOLLOUT | EPOLLET, Key);
            }
Esempio n. 19
0
        protected override bool ReleaseHandle()
        {
            SafeHandle?parent = _parent;

            if (parent != null)
            {
                parent.DangerousRelease();
            }

            _parent = null;
            SetHandle(IntPtr.Zero);
            return(true);
        }
Esempio n. 20
0
        public SecurityBuffer(int size, SecurityBufferType tokentype)
        {
            if (size < 0)
            {
                NetEventSource.Fail(typeof(SecurityBuffer), $"'size' out of range.  [{size}]");
            }

            this.offset         = 0;
            this.size           = size;
            this.type           = tokentype;
            this.token          = size == 0 ? null : new byte[size];
            this.unmanagedToken = null;
        }
Esempio n. 21
0
        public static WicImageContainer Create(IWICBitmapDecoder *dec, SafeHandle?src = null)
        {
            var guid = default(Guid);

            HRESULT.Check(dec->GetContainerFormat(&guid));

            var fmt = WicImageDecoder.FormatMap.GetValueOrDefault(guid, FileFormat.Unknown);

            if (fmt == FileFormat.Gif)
            {
                return(new WicGifContainer(dec, src));
            }

            return(new WicImageContainer(dec, src, fmt));
        }
            private void EnsureNonBlocking()
            {
                SafeHandle?handle = _handle;

                if (handle == null)
                {
                    // We've been disposed.
                    return;
                }

                if (!_setToNonBlocking)
                {
                    SocketPal.SetNonBlocking(handle);
                    _setToNonBlocking = true;
                }
            }
Esempio n. 23
0
 private SafeHandle ChooseInput(
     InputRedirection redirection,
     string?fileName,
     SafeHandle?handle,
     SafeHandle?inputPipe,
     bool createNewConsole)
 {
     return(redirection switch
     {
         InputRedirection.ParentInput => ConsolePal.GetStdInputHandleForChild(createNewConsole),
         InputRedirection.InputPipe => inputPipe !,
         InputRedirection.File => OpenFile(fileName !, FileMode.Open, FileAccess.Read, FileShare.Read),
         InputRedirection.Handle => handle !,
         InputRedirection.NullDevice => OpenNullDevice(FileAccess.Read),
         _ => throw new ArgumentOutOfRangeException(nameof(redirection), "Not a valid value for " + nameof(InputRedirection) + "."),
     });
Esempio n. 24
0
        private static Exception?_HandleErrorCode(int errorCode, string?name, SafeHandle?handle, object?context)
        {
            Exception?exception = null;

            switch (errorCode)
            {
            case Interop.Errors.ERROR_INVALID_NAME:
                exception = new ArgumentException(SR.Argument_InvalidName, nameof(name));
                break;

            case Interop.Errors.ERROR_INVALID_HANDLE:
                exception = new ArgumentException(SR.AccessControl_InvalidHandle);
                break;

            case Interop.Errors.ERROR_FILE_NOT_FOUND:
                if ((context != null) && (context is bool) && ((bool)context))
                {
                    // DirectorySecurity
                    if ((name != null) && (name.Length != 0))
                    {
                        exception = new DirectoryNotFoundException(name);
                    }
                    else
                    {
                        exception = new DirectoryNotFoundException();
                    }
                }
                else
                {
                    if ((name != null) && (name.Length != 0))
                    {
                        exception = new FileNotFoundException(name);
                    }
                    else
                    {
                        exception = new FileNotFoundException();
                    }
                }
                break;

            default:
                break;
            }

            return(exception);
        }
Esempio n. 25
0
        internal static unsafe int ReadFile(Stream stream, byte *buffer, int size)
        {
            if (!IsReadFileAvailable)
            {
                return(0);
            }

            SafeHandle?handle = GetSafeFileHandle(stream);

            if (handle == null)
            {
                return(0);
            }

            int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);

            return(result == 0 ? 0 : bytesRead);
        }
Esempio n. 26
0
        protected override bool ReleaseHandle()
        {
            Debug.Assert(!IsInvalid);

            // Clean up resources for named handles
            if (_namedPipeSocketHandle != null)
            {
                SetHandle(DefaultInvalidHandle);
                _namedPipeSocketHandle.DangerousRelease();
                _namedPipeSocketHandle = null;
                return(true);
            }

            // Clean up resources for anonymous handles
            return((long)handle >= 0 ?
                   Interop.Sys.Close(handle) == 0 :
                   true);
        }
Esempio n. 27
0
        internal static unsafe int Read(this Stream stream, byte *buffer, int size)
        {
            if (!IsWindows || stream is not FileStream fs)
            {
                return(0);
            }

            SafeHandle?handle = GetSafeFileHandle(fs);

            if (handle == null)
            {
                return(0);
            }

            int result = Interop.Kernel32.ReadFile(handle, buffer, size, out int bytesRead, IntPtr.Zero);

            return(result == 0 ? 0 : bytesRead);
        }
Esempio n. 28
0
        protected override bool ReleaseHandle()
        {
            Debug.Assert(!IsInvalid);

            if (_pipeSocketHandle != null)
            {
                base.SetHandle((IntPtr)DefaultInvalidHandle);
                _pipeSocketHandle.DangerousRelease();
                _pipeSocketHandle = null;
                return(true);
            }
            else
            {
                return((long)handle >= 0 ?
                       Interop.Sys.Close(handle) == 0 :
                       true);
            }
        }
Esempio n. 29
0
        public SecurityBuffer(byte[]?data, int offset, int size, SecurityBufferType tokentype)
        {
            if (offset < 0 || offset > (data == null ? 0 : data.Length))
            {
                NetEventSource.Fail(typeof(SecurityBuffer), $"'offset' out of range.  [{offset}]");
            }

            if (size < 0 || size > (data == null ? 0 : data.Length - offset))
            {
                NetEventSource.Fail(typeof(SecurityBuffer), $"'size' out of range.  [{size}]");
            }

            this.offset         = data == null || offset < 0 ? 0 : Math.Min(offset, data.Length);
            this.size           = data == null || size < 0 ? 0 : Math.Min(size, data.Length - this.offset);
            this.type           = tokentype;
            this.token          = size == 0 ? null : data;
            this.unmanagedToken = null;
        }
Esempio n. 30
0
        protected override bool ReleaseHandle()
        {
            IntPtr h = handle;

            SetHandle(IntPtr.Zero);

            if (_parent != null)
            {
                SafeHandle parent = _parent;
                _parent = null;
                parent.DangerousRelease();
                return(true);
            }
            else
            {
                return(Interop.Crypto.BioDestroy(h));
            }
        }