xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/examples/static/static_bin.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // This file is an example of a binary which is intended to be sandboxed by the
16 // sandbox2. It's not google3-based, and compiled statically (see BUILD).
17 //
18 // It inverts all bytes coming from stdin and writes them to the stdout.
19 
20 #include <sys/prctl.h>
21 #include <unistd.h>
22 
23 #include <cctype>
24 #include <cstdio>
25 
main(int argc,char * argv[])26 int main(int argc, char* argv[]) {
27   char buf[1024];
28   size_t total_bytes = 0U;
29 
30   prctl(PR_SET_NAME, "static_bin");
31 
32   fprintf(stderr, "=============================\n");
33   fprintf(stderr, "Starting file capitalization\n");
34   fprintf(stderr, "=============================\n");
35   fflush(nullptr);
36 
37   for (;;) {
38     ssize_t sz = read(STDIN_FILENO, buf, sizeof(buf));
39     if (sz < 0) {
40       perror("read");
41       break;
42     }
43     if (sz == 0) {
44       break;
45     }
46     for (int i = 0; i < sz; i++) {
47       buf[i] = toupper(buf[i]);
48     }
49     write(STDOUT_FILENO, buf, sz);
50     total_bytes += sz;
51   }
52 
53   fprintf(stderr, "=============================\n");
54   fprintf(stderr, "Converted: %zu bytes\n", total_bytes);
55   fprintf(stderr, "=============================\n");
56   fflush(nullptr);
57   return 0;
58 }
59