summaryrefslogtreecommitdiff
path: root/ext/node/ops/os/mod.rs
blob: b4c9eaa8cadc59ed2441f330053a6666cddb201c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use crate::NodePermissions;
use deno_core::op2;
use deno_core::OpState;

mod cpus;
pub mod priority;

#[derive(Debug, thiserror::Error)]
pub enum OsError {
  #[error(transparent)]
  Priority(priority::PriorityError),
  #[error(transparent)]
  Permission(deno_core::error::AnyError),
  #[error("Failed to get cpu info")]
  FailedToGetCpuInfo,
}

#[op2(fast)]
pub fn op_node_os_get_priority<P>(
  state: &mut OpState,
  pid: u32,
) -> Result<i32, OsError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions
      .check_sys("getPriority", "node:os.getPriority()")
      .map_err(OsError::Permission)?;
  }

  priority::get_priority(pid).map_err(OsError::Priority)
}

#[op2(fast)]
pub fn op_node_os_set_priority<P>(
  state: &mut OpState,
  pid: u32,
  priority: i32,
) -> Result<(), OsError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions
      .check_sys("setPriority", "node:os.setPriority()")
      .map_err(OsError::Permission)?;
  }

  priority::set_priority(pid, priority).map_err(OsError::Priority)
}

#[op2]
#[string]
pub fn op_node_os_username<P>(
  state: &mut OpState,
) -> Result<String, deno_core::error::AnyError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions.check_sys("username", "node:os.userInfo()")?;
  }

  Ok(deno_whoami::username())
}

#[op2(fast)]
pub fn op_geteuid<P>(
  state: &mut OpState,
) -> Result<u32, deno_core::error::AnyError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions.check_sys("uid", "node:os.geteuid()")?;
  }

  #[cfg(windows)]
  let euid = 0;
  #[cfg(unix)]
  // SAFETY: Call to libc geteuid.
  let euid = unsafe { libc::geteuid() };

  Ok(euid)
}

#[op2(fast)]
pub fn op_getegid<P>(
  state: &mut OpState,
) -> Result<u32, deno_core::error::AnyError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions.check_sys("getegid", "node:os.getegid()")?;
  }

  #[cfg(windows)]
  let egid = 0;
  #[cfg(unix)]
  // SAFETY: Call to libc getegid.
  let egid = unsafe { libc::getegid() };

  Ok(egid)
}

#[op2]
#[serde]
pub fn op_cpus<P>(state: &mut OpState) -> Result<Vec<cpus::CpuInfo>, OsError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions
      .check_sys("cpus", "node:os.cpus()")
      .map_err(OsError::Permission)?;
  }

  cpus::cpu_info().ok_or(OsError::FailedToGetCpuInfo)
}

#[op2]
#[string]
pub fn op_homedir<P>(
  state: &mut OpState,
) -> Result<Option<String>, deno_core::error::AnyError>
where
  P: NodePermissions + 'static,
{
  {
    let permissions = state.borrow_mut::<P>();
    permissions.check_sys("homedir", "node:os.homedir()")?;
  }

  Ok(home::home_dir().map(|path| path.to_string_lossy().to_string()))
}