From 5b6b192e34311924388715f1981e9ffca7c4b53c Mon Sep 17 00:00:00 2001 From: Dmitry Torokhov Date: Wed, 28 Feb 2018 14:25:40 -0800 Subject: plugin: use positive error values for errno-based errors Errno-based error codes are positive and turning them into negatives is Linxu kernel internal convention. Let's not do this so we do not confuse the signedness of errors coming from crosvm code and system libraries. TEST=cargo test --features plugin BUG=None Change-Id: Ia89f0b78ad1d2bb30a2f427593f13ebfb548b2b0 Signed-off-by: Dmitry Torokhov Reviewed-on: https://chromium-review.googlesource.com/947562 Reviewed-by: Zach Reizner --- src/plugin/mod.rs | 12 ++++++------ src/plugin/process.rs | 26 +++++++++++++------------- src/plugin/vcpu.rs | 28 ++++++++++++++-------------- 3 files changed, 33 insertions(+), 33 deletions(-) (limited to 'src/plugin') diff --git a/src/plugin/mod.rs b/src/plugin/mod.rs index 4d164a4..853b7e5 100644 --- a/src/plugin/mod.rs +++ b/src/plugin/mod.rs @@ -175,19 +175,19 @@ fn new_seqpacket_pair() -> SysResult<(UnixDatagram, UnixDatagram)> { fn proto_to_sys_err(e: ProtobufError) -> SysError { match e { - ProtobufError::IoError(e) => SysError::new(-e.raw_os_error().unwrap_or(EINVAL)), - _ => SysError::new(-EINVAL), + ProtobufError::IoError(e) => SysError::new(e.raw_os_error().unwrap_or(EINVAL)), + _ => SysError::new(EINVAL), } } fn io_to_sys_err(e: io::Error) -> SysError { - SysError::new(-e.raw_os_error().unwrap_or(EINVAL)) + SysError::new(e.raw_os_error().unwrap_or(EINVAL)) } fn mmap_to_sys_err(e: MmapError) -> SysError { match e { MmapError::SystemCallFailed(e) => e, - _ => SysError::new(-EINVAL), + _ => SysError::new(EINVAL), } } @@ -246,7 +246,7 @@ fn create_plugin_jail(root: &Path, seccomp_policy: &Path) -> Result { /// ``` /// match objects.get(&request_id) { /// Some(&PluginObject::Memory { slot, length }) => vm.get_dirty_log(slot, &mut dirty_log[..]) -/// _ => return Err(SysError::new(-ENOENT)), +/// _ => return Err(SysError::new(ENOENT)), /// } /// ``` enum PluginObject { @@ -275,7 +275,7 @@ impl PluginObject { 2 => vm.unregister_ioevent(&evt, addr, datamatch as u16), 4 => vm.unregister_ioevent(&evt, addr, datamatch as u32), 8 => vm.unregister_ioevent(&evt, addr, datamatch as u64), - _ => Err(SysError::new(-EINVAL)), + _ => Err(SysError::new(EINVAL)), } } PluginObject::Memory { slot, .. } => vm.remove_device_memory(slot).and(Ok(())), diff --git a/src/plugin/process.rs b/src/plugin/process.rs index 89c201c..d40e5f2 100644 --- a/src/plugin/process.rs +++ b/src/plugin/process.rs @@ -275,7 +275,7 @@ impl Process { 2 => vm.register_ioevent(&evt, addr, io_event.datamatch as u16)?, 4 => vm.register_ioevent(&evt, addr, io_event.datamatch as u32)?, 8 => vm.register_ioevent(&evt, addr, io_event.datamatch as u64)?, - _ => return Err(SysError::new(-EINVAL)), + _ => return Err(SysError::new(EINVAL)), }; let fd = evt.as_raw_fd(); @@ -303,12 +303,12 @@ impl Process { // to SIGBUS in the future. let seals = shm.get_seals()?; if !seals.shrink_seal() { - return Err(SysError::new(-EPERM)); + return Err(SysError::new(EPERM)); } // Check to make sure we don't mmap areas beyond the end of the memfd. match length.checked_add(offset) { - Some(end) if end > shm.size() => return Err(SysError::new(-EINVAL)), - None => return Err(SysError::new(-EOVERFLOW)), + Some(end) if end > shm.size() => return Err(SysError::new(EINVAL)), + None => return Err(SysError::new(EOVERFLOW)), _ => {} } let mem = MemoryMapping::from_fd_offset(&shm, length as usize, offset as usize) @@ -333,7 +333,7 @@ impl Process { _ => lock.reserve_range(space, reserve_range.start, reserve_range.length), } } - Err(_) => Err(SysError::new(-EDEADLK)), + Err(_) => Err(SysError::new(EDEADLK)), } } @@ -360,7 +360,7 @@ impl Process { // Because route is a oneof field in the proto definition, this should // only happen if a new variant gets added without updating this chained // if block. - return Err(SysError::new(-EINVAL)); + return Err(SysError::new(EINVAL)); }, }); } @@ -435,7 +435,7 @@ impl Process { memory.read_only, memory.dirty_log) } - None => Err(SysError::new(-EBADF)), + None => Err(SysError::new(EBADF)), } } else if create.has_irq_event() { let irq_event = create.get_irq_event(); @@ -460,16 +460,16 @@ impl Process { (Err(e), _) | (_, Err(e)) => Err(e), } } else { - Err(SysError::new(-ENOTTY)) + Err(SysError::new(ENOTTY)) } } - Entry::Occupied(_) => Err(SysError::new(-EEXIST)), + Entry::Occupied(_) => Err(SysError::new(EEXIST)), } } else if request.has_destroy() { response.mut_destroy(); match self.objects.entry(request.get_destroy().id) { Entry::Occupied(entry) => entry.remove().destroy(vm), - Entry::Vacant(_) => Err(SysError::new(-ENOENT)), + Entry::Vacant(_) => Err(SysError::new(ENOENT)), } } else if request.has_new_connection() { response.mut_new_connection(); @@ -520,7 +520,7 @@ impl Process { } else if request.has_start() { response.mut_start(); if self.started { - Err(SysError::new(-EINVAL)) + Err(SysError::new(EINVAL)) } else { self.started = true; Ok(()) @@ -533,7 +533,7 @@ impl Process { dirty_log.resize(dirty_log_bitmap_size(length), 0); vm.get_dirty_log(slot, &mut dirty_log[..]) } - _ => Err(SysError::new(-ENOENT)), + _ => Err(SysError::new(ENOENT)), } } else if request.has_get_supported_cpuid() { let cpuid_response = &mut response.mut_get_supported_cpuid().entries; @@ -558,7 +558,7 @@ impl Process { Err(e) => Err(e) } } else { - Err(SysError::new(-ENOTTY)) + Err(SysError::new(ENOTTY)) }; if let Err(e) = res { diff --git a/src/plugin/vcpu.rs b/src/plugin/vcpu.rs index e175d8a..fe59dd6 100644 --- a/src/plugin/vcpu.rs +++ b/src/plugin/vcpu.rs @@ -83,22 +83,22 @@ fn set_vcpu_state(vcpu: &Vcpu, state_set: VcpuRequest_StateSet, state: &[u8]) -> match state_set { VcpuRequest_StateSet::REGS => { vcpu.set_regs(&VcpuRegs::from_slice(state) - .ok_or(SysError::new(-EINVAL))? + .ok_or(SysError::new(EINVAL))? .0) } VcpuRequest_StateSet::SREGS => { vcpu.set_sregs(&VcpuSregs::from_slice(state) - .ok_or(SysError::new(-EINVAL))? + .ok_or(SysError::new(EINVAL))? .0) } VcpuRequest_StateSet::FPU => { vcpu.set_fpu(&VcpuFpu::from_slice(state) - .ok_or(SysError::new(-EINVAL))? + .ok_or(SysError::new(EINVAL))? .0) } VcpuRequest_StateSet::DEBUGREGS => { vcpu.set_debugregs(&VcpuDebugregs::from_slice(state) - .ok_or(SysError::new(-EINVAL))? + .ok_or(SysError::new(EINVAL))? .0) } } @@ -118,17 +118,17 @@ impl SharedVcpuState { /// This will reject any reservation that overlaps with an existing reservation. pub fn reserve_range(&mut self, space: IoSpace, start: u64, length: u64) -> SysResult<()> { if length == 0 { - return Err(SysError::new(-EINVAL)); + return Err(SysError::new(EINVAL)); } // Reject all cases where this reservation is part of another reservation. if self.is_reserved(space, start) { - return Err(SysError::new(-EPERM)); + return Err(SysError::new(EPERM)); } let last_address = match start.checked_add(length) { Some(end) => end - 1, - None => return Err(SysError::new(-EINVAL)), + None => return Err(SysError::new(EINVAL)), }; let space = match space { @@ -140,7 +140,7 @@ impl SharedVcpuState { .range(..Range(last_address, 0)) .next_back() .cloned() { - Some(Range(existing_start, _)) if existing_start >= start => Err(SysError::new(-EPERM)), + Some(Range(existing_start, _)) if existing_start >= start => Err(SysError::new(EPERM)), _ => { space.insert(Range(start, length)); Ok(()) @@ -158,7 +158,7 @@ impl SharedVcpuState { if space.remove(&range) { Ok(()) } else { - Err(SysError::new(-ENOENT)) + Err(SysError::new(ENOENT)) } } @@ -285,7 +285,7 @@ impl PluginVcpu { /// to this VCPU. pub fn pre_run(&self, vcpu: &Vcpu) -> SysResult<()> { let request = { - let mut lock = self.per_vcpu_state.lock().map_err(|_| SysError::new(-EDEADLK))?; + let mut lock = self.per_vcpu_state.lock().map_err(|_| SysError::new(EDEADLK))?; lock.pause_request.take() }; @@ -332,7 +332,7 @@ impl PluginVcpu { self.wait_reason.set(Some(wait_reason)); match self.handle_until_resume(vcpu) { Ok(resume_data) => data.copy_from_slice(&resume_data), - Err(e) if e.errno() == -EPIPE => {} + Err(e) if e.errno() == EPIPE => {} Err(e) => error!("failed to process vcpu requests: {:?}", e), } true @@ -383,12 +383,12 @@ impl PluginVcpu { response.set_wait(wait_reason); Ok(()) } - None => Err(SysError::new(-EPROTO)), + None => Err(SysError::new(EPROTO)), } } else if wait_reason.is_some() { // Any request other than getting the wait_reason while there is one pending is invalid. self.wait_reason.set(wait_reason); - Err(SysError::new(-EPROTO)) + Err(SysError::new(EPROTO)) } else if request.has_resume() { response.mut_resume(); resume_data = Some(request.take_resume().take_data()); @@ -470,7 +470,7 @@ impl PluginVcpu { } vcpu.set_cpuid2(&cpuid) } else { - Err(SysError::new(-ENOTTY)) + Err(SysError::new(ENOTTY)) }; if let Err(e) = res { -- cgit 1.4.1