more things broken for now
- shell now uses the path env variable - commands run based on permission of the bin file
This commit is contained in:
@@ -18,7 +18,6 @@ export interface UserInfo {
|
|||||||
|
|
||||||
export interface SessionState {
|
export interface SessionState {
|
||||||
readonly user: UserInfo;
|
readonly user: UserInfo;
|
||||||
readonly envVars: ReadonlyMap<string, string>;
|
|
||||||
readonly history: HistoryState;
|
readonly history: HistoryState;
|
||||||
readonly cwd: number;
|
readonly cwd: number;
|
||||||
readonly pwd: number;
|
readonly pwd: number;
|
||||||
@@ -28,7 +27,7 @@ export interface SessionState {
|
|||||||
export function createInitialSessionState(user: UserInfo): SessionState {
|
export function createInitialSessionState(user: UserInfo): SessionState {
|
||||||
return Object.freeze({
|
return Object.freeze({
|
||||||
user,
|
user,
|
||||||
envVars: Object.freeze(new Map()),
|
//envVars: Object.freeze(new Map()),
|
||||||
history: Object.freeze([]),
|
history: Object.freeze([]),
|
||||||
cwd: user.home,
|
cwd: user.home,
|
||||||
pwd: user.home,
|
pwd: user.home,
|
||||||
@@ -65,7 +64,7 @@ export function login(
|
|||||||
|
|
||||||
const stack = [...state.sessionStack];
|
const stack = [...state.sessionStack];
|
||||||
stack.push(createInitialSessionState(user));
|
stack.push(createInitialSessionState(user));
|
||||||
return createShellState(state.filesystem, state.users, state.groups, stack);
|
return createShellState(state.filesystem, state.users, state.groups, stack, state.env);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function logout(state: ShellState): ShellState {
|
export function logout(state: ShellState): ShellState {
|
||||||
@@ -73,7 +72,7 @@ export function logout(state: ShellState): ShellState {
|
|||||||
const stack = [...state.sessionStack];
|
const stack = [...state.sessionStack];
|
||||||
stack.pop();
|
stack.pop();
|
||||||
|
|
||||||
return createShellState(state.filesystem, state.users, state.groups, stack);
|
return createShellState(state.filesystem, state.users, state.groups, stack, state.env);
|
||||||
}
|
}
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-6
@@ -8,6 +8,7 @@ export interface ShellState {
|
|||||||
readonly users: ReadonlyMap<number, UserInfo>;
|
readonly users: ReadonlyMap<number, UserInfo>;
|
||||||
readonly groups: ReadonlyMap<number, GroupInfo>;
|
readonly groups: ReadonlyMap<number, GroupInfo>;
|
||||||
readonly sessionStack: ReadonlyArray<SessionState>;
|
readonly sessionStack: ReadonlyArray<SessionState>;
|
||||||
|
readonly env: ReadonlyMap<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createInitialShellState(
|
export function createInitialShellState(
|
||||||
@@ -15,12 +16,14 @@ export function createInitialShellState(
|
|||||||
users: ReadonlyMap<number, UserInfo>,
|
users: ReadonlyMap<number, UserInfo>,
|
||||||
groups: ReadonlyMap<number, GroupInfo>,
|
groups: ReadonlyMap<number, GroupInfo>,
|
||||||
user: UserInfo,
|
user: UserInfo,
|
||||||
|
env: ReadonlyMap<string, string>,
|
||||||
): ShellState {
|
): ShellState {
|
||||||
return Object.freeze({
|
return Object.freeze({
|
||||||
filesystem,
|
filesystem,
|
||||||
users,
|
users,
|
||||||
groups,
|
groups,
|
||||||
sessionStack: Object.freeze([createInitialSessionState(user)]),
|
sessionStack: Object.freeze([createInitialSessionState(user)]),
|
||||||
|
env,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,12 +32,14 @@ export function createShellState(
|
|||||||
users: ReadonlyMap<number, UserInfo>,
|
users: ReadonlyMap<number, UserInfo>,
|
||||||
groups: ReadonlyMap<number, GroupInfo>,
|
groups: ReadonlyMap<number, GroupInfo>,
|
||||||
sessionStack: ReadonlyArray<SessionState>,
|
sessionStack: ReadonlyArray<SessionState>,
|
||||||
|
env: ReadonlyMap<string, string>,
|
||||||
): ShellState {
|
): ShellState {
|
||||||
return Object.freeze({
|
return Object.freeze({
|
||||||
filesystem,
|
filesystem,
|
||||||
users,
|
users,
|
||||||
groups,
|
groups,
|
||||||
sessionStack: Object.freeze(sessionStack),
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
env,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +48,7 @@ export function withCwd(state: ShellState, inode: number): ShellState {
|
|||||||
|
|
||||||
const newSession = Object.freeze({
|
const newSession = Object.freeze({
|
||||||
user: session.user,
|
user: session.user,
|
||||||
envVars: Object.freeze(session.envVars),
|
//envVars: Object.freeze(session.envVars),
|
||||||
history: Object.freeze(session.history),
|
history: Object.freeze(session.history),
|
||||||
cwd: inode,
|
cwd: inode,
|
||||||
pwd: session.cwd,
|
pwd: session.cwd,
|
||||||
@@ -59,6 +64,7 @@ export function withCwd(state: ShellState, inode: number): ShellState {
|
|||||||
users: state.users,
|
users: state.users,
|
||||||
groups: state.groups,
|
groups: state.groups,
|
||||||
sessionStack: Object.freeze(sessionStack),
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
env: state.env,
|
||||||
} satisfies ShellState);
|
} satisfies ShellState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +73,7 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
|||||||
|
|
||||||
const newSession = Object.freeze({
|
const newSession = Object.freeze({
|
||||||
user: session.user,
|
user: session.user,
|
||||||
envVars: Object.freeze(session.envVars),
|
//envVars: Object.freeze(session.envVars),
|
||||||
history: Object.freeze(addHistoryEntry(session.history, input)),
|
history: Object.freeze(addHistoryEntry(session.history, input)),
|
||||||
cwd: session.cwd,
|
cwd: session.cwd,
|
||||||
pwd: session.pwd,
|
pwd: session.pwd,
|
||||||
@@ -83,10 +89,11 @@ export function withHistory(state: ShellState, input: string): ShellState {
|
|||||||
users: state.users,
|
users: state.users,
|
||||||
groups: state.groups,
|
groups: state.groups,
|
||||||
sessionStack: Object.freeze(sessionStack),
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
env: state.env,
|
||||||
} satisfies ShellState);
|
} satisfies ShellState);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function withEnv(state: ShellState, key: string, value: string): ShellState {
|
/* export function withEnv(state: ShellState, key: string, value: string): ShellState {
|
||||||
const session = getCurrentSession(state);
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
const envVars = new Map(session.envVars);
|
const envVars = new Map(session.envVars);
|
||||||
@@ -94,7 +101,7 @@ export function withEnv(state: ShellState, key: string, value: string): ShellSta
|
|||||||
|
|
||||||
const newSession = Object.freeze({
|
const newSession = Object.freeze({
|
||||||
user: session.user,
|
user: session.user,
|
||||||
envVars: Object.freeze(envVars),
|
//envVars: Object.freeze(envVars),
|
||||||
history: Object.freeze(session.history),
|
history: Object.freeze(session.history),
|
||||||
cwd: session.cwd,
|
cwd: session.cwd,
|
||||||
pwd: session.pwd,
|
pwd: session.pwd,
|
||||||
@@ -111,14 +118,14 @@ export function withEnv(state: ShellState, key: string, value: string): ShellSta
|
|||||||
groups: state.groups,
|
groups: state.groups,
|
||||||
sessionStack: Object.freeze(sessionStack),
|
sessionStack: Object.freeze(sessionStack),
|
||||||
} satisfies ShellState);
|
} satisfies ShellState);
|
||||||
}
|
} */
|
||||||
|
|
||||||
export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
||||||
const session = getCurrentSession(state);
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
const newSession = Object.freeze({
|
const newSession = Object.freeze({
|
||||||
user: session.user,
|
user: session.user,
|
||||||
envVars: Object.freeze(session.envVars),
|
//envVars: Object.freeze(session.envVars),
|
||||||
history: Object.freeze(session.history),
|
history: Object.freeze(session.history),
|
||||||
cwd: session.cwd,
|
cwd: session.cwd,
|
||||||
pwd: session.pwd,
|
pwd: session.pwd,
|
||||||
@@ -134,5 +141,6 @@ export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
|||||||
users: state.users,
|
users: state.users,
|
||||||
groups: state.groups,
|
groups: state.groups,
|
||||||
sessionStack: Object.freeze(sessionStack),
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
env: state.env,
|
||||||
} satisfies ShellState);
|
} satisfies ShellState);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -45,7 +45,10 @@ const group: GroupInfo = {
|
|||||||
users.set(1000, user);
|
users.set(1000, user);
|
||||||
groups.set(1000, group);
|
groups.set(1000, group);
|
||||||
|
|
||||||
const state: ShellState = createInitialShellState(fs, users, groups, user);
|
const env: Map<string, string> = new Map();
|
||||||
|
env.set("PATH", "/folder1");
|
||||||
|
|
||||||
|
const state: ShellState = createInitialShellState(fs, users, groups, user, env);
|
||||||
Shell.init(state);
|
Shell.init(state);
|
||||||
|
|
||||||
//console.log(resolveInode(Shell.Instance?.State!, 6));
|
//console.log(resolveInode(Shell.Instance?.State!, 6));
|
||||||
|
|||||||
+14
-3
@@ -1,4 +1,4 @@
|
|||||||
import { COMMAND_REGISTRY } from "@core/command_registry";
|
import { type DirectoryNode } from "@core/filesystem/treenode";
|
||||||
import { createErrorResult, type Result } from "@core/result";
|
import { createErrorResult, type Result } from "@core/result";
|
||||||
import { getCurrentSession } from "@core/session_manager";
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
||||||
@@ -6,6 +6,7 @@ import { SIGNAL_MAP } from "@core/signal";
|
|||||||
import type { ShellError } from "@core/util/errors";
|
import type { ShellError } from "@core/util/errors";
|
||||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
import { tokenize } from "@core/util/parser";
|
import { tokenize } from "@core/util/parser";
|
||||||
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
import { EventEmitter } from "./event_emitter";
|
import { EventEmitter } from "./event_emitter";
|
||||||
import { ProcessManager } from "./process_manager";
|
import { ProcessManager } from "./process_manager";
|
||||||
|
|
||||||
@@ -63,7 +64,12 @@ export class Shell {
|
|||||||
if (!parsed[0]) return;
|
if (!parsed[0]) return;
|
||||||
|
|
||||||
this._State = withHistory(this._State, input);
|
this._State = withHistory(this._State, input);
|
||||||
const command = COMMAND_REGISTRY.get(parsed[0]);
|
|
||||||
|
const inode = resolvePath(this._State, this._State.env.get("PATH")!);
|
||||||
|
const node = this._State.filesystem.getNode(inode) as DirectoryNode;
|
||||||
|
const command = node.children[parsed[0]];
|
||||||
|
|
||||||
|
//const command = COMMAND_REGISTRY.get(parsed[0]);
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
||||||
@@ -82,7 +88,7 @@ export class Shell {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (command.requiresRoot && getCurrentSession(this._State).user.uid != 0) {
|
if (this._State.filesystem.canExecute(command, getCurrentSession(this._State).user)) {
|
||||||
const result = createErrorResult(
|
const result = createErrorResult(
|
||||||
"PermissionDenied",
|
"PermissionDenied",
|
||||||
`Permission Denied`,
|
`Permission Denied`,
|
||||||
@@ -95,6 +101,11 @@ export class Shell {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const bin = this._State.filesystem.getNode(command);
|
||||||
|
// read file content
|
||||||
|
// try to serialize it to a function
|
||||||
|
// call that function with a new process
|
||||||
|
|
||||||
parsed.shift();
|
parsed.shift();
|
||||||
|
|
||||||
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
||||||
|
|||||||
Reference in New Issue
Block a user