Skip to content

Commit 83943ed

Browse files
authored
vminitd: Make everything libc agnostic (#655)
Remove the last (as far as I can see..) of the musl specific imports we had.
1 parent a1038a9 commit 83943ed

8 files changed

Lines changed: 61 additions & 14 deletions

File tree

vminitd/Sources/vmexec/Console.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
import FoundationEssentials
18+
19+
#if canImport(Musl)
1820
import Musl
21+
private let _close = Musl.close
22+
#elseif canImport(Glibc)
23+
import Glibc
24+
private let _close = Glibc.close
25+
#endif
1926

2027
class Console {
2128
let master: Int32
@@ -45,7 +52,7 @@ class Console {
4552
guard slaveFD != -1 else {
4653
throw App.Errno(stage: "open_pts")
4754
}
48-
defer { Musl.close(slaveFD) }
55+
defer { _ = _close(slaveFD) }
4956

5057
for fd: Int32 in 0...2 {
5158
guard dup3(slaveFD, fd, 0) != -1 else {
@@ -55,7 +62,7 @@ class Console {
5562
}
5663

5764
func close() throws {
58-
guard Musl.close(self.master) == 0 else {
65+
guard _close(self.master) == 0 else {
5966
throw App.Errno(stage: "close")
6067
}
6168
}

vminitd/Sources/vmexec/ExecCommand.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import ContainerizationOS
2020
import FoundationEssentials
2121
import LCShim
2222
import Logging
23-
import Musl
2423
import SystemPackage
2524

25+
#if canImport(Musl)
26+
import Musl
27+
#elseif canImport(Glibc)
28+
import Glibc
29+
#endif
30+
2631
struct ExecCommand: ParsableCommand {
2732
static let configuration = CommandConfiguration(
2833
commandName: "exec",

vminitd/Sources/vmexec/Mount.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import ContainerizationOCI
1818
import ContainerizationOS
1919
import FoundationEssentials
20+
21+
#if canImport(Musl)
2022
import Musl
23+
#elseif canImport(Glibc)
24+
import Glibc
25+
#endif
2126

2227
struct ContainerMount {
2328
private let mounts: [ContainerizationOCI.Mount]

vminitd/Sources/vmexec/RunCommand.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ import ContainerizationOCI
2020
import ContainerizationOS
2121
import FoundationEssentials
2222
import LCShim
23-
import Musl
2423
import SystemPackage
2524

25+
#if canImport(Musl)
26+
import Musl
27+
#elseif canImport(Glibc)
28+
import Glibc
29+
#endif
30+
2631
struct RunCommand: ParsableCommand {
2732
static let configuration = CommandConfiguration(
2833
commandName: "run",
@@ -152,7 +157,7 @@ struct RunCommand: ParsableCommand {
152157

153158
if !spec.hostname.isEmpty {
154159
let errCode = spec.hostname.withCString { ptr in
155-
Musl.sethostname(ptr, spec.hostname.count)
160+
sethostname(ptr, spec.hostname.count)
156161
}
157162
guard errCode == 0 else {
158163
throw App.Errno(stage: "sethostname()")

vminitd/Sources/vmexec/vmexec.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ import ContainerizationOS
2626
import FoundationEssentials
2727
import LCShim
2828
import Logging
29-
import Musl
3029
import SystemPackage
3130

31+
#if canImport(Musl)
32+
import Musl
33+
#elseif canImport(Glibc)
34+
import Glibc
35+
#endif
36+
3237
@main
3338
struct App: ParsableCommand {
3439
static let ackPid = "AckPid"

vminitd/Sources/vminitd/AgentCommand.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import NIOCore
2626
import NIOPosix
2727

2828
#if os(Linux)
29+
#if canImport(Musl)
2930
import Musl
31+
#elseif canImport(Glibc)
32+
import Glibc
33+
#endif
3034
import LCShim
3135
#endif
3236

@@ -157,13 +161,13 @@ struct AgentCommand: AsyncParsableCommand {
157161
log.info("vminitd API returned, syncing filesystems")
158162

159163
#if os(Linux)
160-
Musl.sync()
164+
sync()
161165
#endif
162166
} catch {
163167
log.error("vminitd boot error \(error)")
164168

165169
#if os(Linux)
166-
Musl.sync()
170+
sync()
167171
#endif
168172

169173
_exit(1)

vminitd/Sources/vminitd/InitCommand.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717
import ArgumentParser
1818
import ContainerizationOS
1919
import LCShim
20+
21+
#if canImport(Musl)
2022
import Musl
23+
private let _exit = Musl.exit
24+
private let _kill = Musl.kill
25+
#elseif canImport(Glibc)
26+
import Glibc
27+
private let _exit = Glibc.exit
28+
private let _kill = Glibc.kill
29+
#endif
2130

2231
/// A minimal init process that:
2332
/// - Spawns and monitors a child process
@@ -78,7 +87,7 @@ struct InitCommand: ParsableCommand {
7887
let sig = sigtimedwait(&allSignals, &siginfo, &timeout)
7988

8089
if sig > 0 && !Self.ignoredSignals.contains(sig) {
81-
_ = Musl.kill(signalTarget, sig)
90+
_ = _kill(signalTarget, sig)
8291
}
8392

8493
while true {
@@ -93,6 +102,6 @@ struct InitCommand: ParsableCommand {
93102
}
94103
}
95104

96-
Musl.exit(childExitStatus ?? 1)
105+
_exit(childExitStatus ?? 1)
97106
}
98107
}

vminitd/Sources/vminitd/PauseCommand.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
import ArgumentParser
1818
import Dispatch
1919
import Logging
20+
21+
#if canImport(Musl)
2022
import Musl
23+
private let _exit = Musl.exit
24+
#elseif canImport(Glibc)
25+
import Glibc
26+
private let _exit = Glibc.exit
27+
#endif
2128

2229
struct PauseCommand: ParsableCommand {
2330
static let configuration = CommandConfiguration(
@@ -39,14 +46,14 @@ struct PauseCommand: ParsableCommand {
3946
let sigintSource = DispatchSource.makeSignalSource(signal: SIGINT)
4047
sigintSource.setEventHandler {
4148
log.info("Shutting down, got SIGINT")
42-
Musl.exit(0)
49+
_exit(0)
4350
}
4451
sigintSource.resume()
4552

4653
let sigtermSource = DispatchSource.makeSignalSource(signal: SIGTERM)
4754
sigtermSource.setEventHandler {
4855
log.info("Shutting down, got SIGTERM")
49-
Musl.exit(0)
56+
_exit(0)
5057
}
5158
sigtermSource.resume()
5259

@@ -60,10 +67,10 @@ struct PauseCommand: ParsableCommand {
6067
log.info("pause container running, waiting for signals...")
6168

6269
while true {
63-
Musl.pause()
70+
_ = pause()
6471
}
6572

6673
log.error("Error: infinite loop terminated")
67-
Musl.exit(42)
74+
_exit(42)
6875
}
6976
}

0 commit comments

Comments
 (0)