Skip to content

Commit

Permalink
Fix build errors on Apple platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
xtremekforever committed Jan 18, 2025
1 parent 54f2e40 commit 8f25033
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Swift
.build*/
.swiftpm/

# VSCode
launch.json
Expand Down
24 changes: 13 additions & 11 deletions Sources/Example/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ if SystemdHelpers.isSystemdService {
logger.info("Not running as a systemd service...")
}

logger.info("Adding SystemdService to run as part of a ServiceGroup...")
let serviceGroup = ServiceGroup(
configuration: .init(
services: [
.init(service: SystemdService())
],
gracefulShutdownSignals: [.sigterm],
logger: logger
if #available(iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
logger.info("Adding SystemdService to run as part of a ServiceGroup...")
let serviceGroup = ServiceGroup(
configuration: .init(
services: [
.init(service: SystemdService())
],
gracefulShutdownSignals: [.sigterm],
logger: logger
)
)
)

logger.info("Send SIGTERM signal to exit the service")
try await serviceGroup.run()
logger.info("Send SIGTERM signal to exit the service")
try await serviceGroup.run()
}

logger.info("Exiting application...")
60 changes: 30 additions & 30 deletions Sources/Systemd/Utility.swift
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
#if canImport(Glibc)
import Glibc
#endif

/// Compute the prefix sum of `seq`.
private func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] {
var result: [U] = []
result.reserveCapacity(seq.underestimatedCount)
var runningResult = initial
for element in seq {
runningResult = combine(runningResult, element)
result.append(runningResult)
/// Compute the prefix sum of `seq`.
private func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] {
var result: [U] = []
result.reserveCapacity(seq.underestimatedCount)
var runningResult = initial
for element in seq {
runningResult = combine(runningResult, element)
result.append(runningResult)
}
return result
}
return result
}

func withArrayOfIovecs<R>(_ args: [String], _ body: ([iovec]) -> R) -> R {
let argsCounts = Array(args.map { $0.utf8.count + 1 })
let argsOffsets = [0] + scan(argsCounts, 0, +)
let argsBufferSize = argsOffsets.last!
var argsBuffer: [UInt8] = []
argsBuffer.reserveCapacity(argsBufferSize)
for arg in args {
argsBuffer.append(contentsOf: arg.utf8)
argsBuffer.append(0)
}
return argsBuffer.withUnsafeMutableBufferPointer {
argsBuffer in
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory(
to: CChar.self, capacity: argsBuffer.count
)
let iovecs: [iovec] = zip(argsCounts, argsOffsets).map { count, offset in
iovec(iov_base: ptr + offset, iov_len: count - 1)
func withArrayOfIovecs<R>(_ args: [String], _ body: ([iovec]) -> R) -> R {
let argsCounts = Array(args.map { $0.utf8.count + 1 })
let argsOffsets = [0] + scan(argsCounts, 0, +)
let argsBufferSize = argsOffsets.last!
var argsBuffer: [UInt8] = []
argsBuffer.reserveCapacity(argsBufferSize)
for arg in args {
argsBuffer.append(contentsOf: arg.utf8)
argsBuffer.append(0)
}
return argsBuffer.withUnsafeMutableBufferPointer {
argsBuffer in
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory(
to: CChar.self, capacity: argsBuffer.count
)
let iovecs: [iovec] = zip(argsCounts, argsOffsets).map { count, offset in
iovec(iov_base: ptr + offset, iov_len: count - 1)
}
return body(iovecs)
}
return body(iovecs)
}
}
#endif

0 comments on commit 8f25033

Please sign in to comment.