-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract TTY writing so multiple writers can be synchronized (#1108)
* Abstract TTY writing so multiple writers can be synchronized * Address PR feedback
- Loading branch information
Showing
7 changed files
with
144 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/// Copyright 2023 Google LLC | ||
/// | ||
/// Licensed under the Apache License, Version 2.0 (the "License"); | ||
/// you may not use this file except in compliance with the License. | ||
/// You may obtain a copy of the License at | ||
/// | ||
/// https://www.apache.org/licenses/LICENSE-2.0 | ||
/// | ||
/// Unless required by applicable law or agreed to in writing, software | ||
/// distributed under the License is distributed on an "AS IS" BASIS, | ||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
/// See the License for the specific language governing permissions and | ||
/// limitations under the License. | ||
|
||
#ifndef SANTA__SANTAD__TTYWRITER_H | ||
#define SANTA__SANTAD__TTYWRITER_H | ||
|
||
#import <Foundation/Foundation.h> | ||
#include <dispatch/dispatch.h> | ||
|
||
#include <memory> | ||
|
||
namespace santa::santad { | ||
|
||
// Small helper class to synchronize writing to TTYs | ||
class TTYWriter { | ||
public: | ||
static std::unique_ptr<TTYWriter> Create(); | ||
|
||
TTYWriter(dispatch_queue_t q); | ||
|
||
// Moves can be safe, but not currently needed/implemented | ||
TTYWriter(TTYWriter &&other) = delete; | ||
TTYWriter &operator=(TTYWriter &&rhs) = delete; | ||
|
||
// No copies | ||
TTYWriter(const TTYWriter &other) = delete; | ||
TTYWriter &operator=(const TTYWriter &other) = delete; | ||
|
||
void Write(const char *tty, NSString *msg); | ||
|
||
private: | ||
dispatch_queue_t q_; | ||
}; | ||
|
||
} // namespace santa::santad | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/// Copyright 2023 Google LLC | ||
/// | ||
/// Licensed under the Apache License, Version 2.0 (the "License"); | ||
/// you may not use this file except in compliance with the License. | ||
/// You may obtain a copy of the License at | ||
/// | ||
/// https://www.apache.org/licenses/LICENSE-2.0 | ||
/// | ||
/// Unless required by applicable law or agreed to in writing, software | ||
/// distributed under the License is distributed on an "AS IS" BASIS, | ||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
/// See the License for the specific language governing permissions and | ||
/// limitations under the License. | ||
|
||
#include "Source/santad/TTYWriter.h" | ||
|
||
#include <string.h> | ||
#include <sys/errno.h> | ||
|
||
#import "Source/common/SNTLogging.h" | ||
#include "Source/common/String.h" | ||
|
||
namespace santa::santad { | ||
|
||
std::unique_ptr<TTYWriter> TTYWriter::Create() { | ||
dispatch_queue_t q = dispatch_queue_create_with_target( | ||
"com.google.santa.ttywriter", DISPATCH_QUEUE_SERIAL, | ||
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); | ||
|
||
if (!q) { | ||
return nullptr; | ||
} | ||
|
||
return std::make_unique<TTYWriter>(q); | ||
} | ||
|
||
TTYWriter::TTYWriter(dispatch_queue_t q) : q_(q) {} | ||
|
||
void TTYWriter::Write(const char *tty, NSString *msg) { | ||
dispatch_async(q_, ^{ | ||
@autoreleasepool { | ||
int fd = open(tty, O_WRONLY | O_NOCTTY); | ||
if (fd == -1) { | ||
LOGW(@"Failed to open TTY for writing: %s", strerror(errno)); | ||
return; | ||
} | ||
|
||
std::string_view str = santa::common::NSStringToUTF8StringView(msg); | ||
write(fd, str.data(), str.length()); | ||
|
||
close(fd); | ||
} | ||
}); | ||
} | ||
|
||
} // namespace santa::santad |