File: examples.cpp

package info (click to toggle)
mender-client 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,124 kB
  • sloc: cpp: 56,631; sh: 1,118; python: 633; makefile: 94; xml: 44
file content (234 lines) | stat: -rw-r--r-- 8,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "process.hpp"
#include <iostream>

using namespace std;
using namespace TinyProcessLib;

int main() {
#if !defined(_WIN32) || defined(MSYS_PROCESS_USE_SH)
  // The following examples are for Unix-like systems and Windows through MSYS2


  cout << "Example 1a - the mandatory Hello World through a command" << endl;
  Process process1a("echo Hello World", "", [](const char *bytes, size_t n) {
    cout << "Output from stdout: " << string(bytes, n);
  });
  auto exit_status = process1a.get_exit_status();
  cout << "Example 1a process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 1b - Hello World using arguments" << endl;
  Process process1b(vector<string>{"/bin/echo", "Hello", "World"}, "", [](const char *bytes, size_t n) {
    cout << "Output from stdout: " << string(bytes, n);
  });
  exit_status = process1b.get_exit_status();
  cout << "Example 1b process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


#ifndef _WIN32
  cout << endl
       << "Example 1c - Hello World through a function on Unix-like systems" << endl;
  Process process1c(
      [] {
        cout << "Hello World" << endl;
        exit(0);
      },
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      });
  exit_status = process1c.get_exit_status();
  cout << "Example 1c process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));
#endif


  cout << endl
       << "Example 2 - cd into a nonexistent directory" << endl;
  Process process2(
      "cd a_nonexistent_directory", "",
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      },
      [](const char *bytes, size_t n) {
        cout << "Output from stderr: " << string(bytes, n);
        // Add a newline for prettier output on some platforms:
        if(bytes[n - 1] != '\n')
          cout << endl;
      });
  exit_status = process2.get_exit_status();
  cout << "Example 2 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 3 - async sleep process" << endl;
  thread thread3([]() {
    Process process3("sleep 2");
    auto exit_status = process3.get_exit_status();
    cout << "Example 3 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  });
  thread3.detach();
  this_thread::sleep_for(chrono::seconds(4));


  cout << endl
       << "Example 4 - killing async sleep process after 2 seconds" << endl;
  auto process4 = make_shared<Process>("sleep 4");
  thread thread4([process4]() {
    auto exit_status = process4->get_exit_status();
    cout << "Example 4 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  });
  thread4.detach();
  this_thread::sleep_for(chrono::seconds(2));
  process4->kill();
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 5 - multiple commands, stdout and stderr" << endl;
  Process process5(
      "echo Hello && ls an_incorrect_path", "",
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      },
      [](const char *bytes, size_t n) {
        cout << "Output from stderr: " << string(bytes, n);
        // Add a newline for prettier output on some platforms:
        if(bytes[n - 1] != '\n')
          cout << endl;
      });
  exit_status = process5.get_exit_status();
  cout << "Example 5 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 6 - run bash with input from stdin" << endl;
  Process process6(
      "bash", "",
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      },
      nullptr, true);
  process6.write("echo Hello from bash\n");
  process6.write("exit\n");
  exit_status = process6.get_exit_status();
  cout << "Example 6 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 7 - send data to cat through stdin" << endl;
  Process process7(
      "cat", "",
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      },
      nullptr, true);
  process7.write("Hello cat\n");
  process7.close_stdin();
  exit_status = process7.get_exit_status();
  cout << "Example 7 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 8 - demonstrates Process::try_get_exit_status" << endl;
  Process process8("sleep 3");
  while(!process8.try_get_exit_status(exit_status)) {
    cout << "Example 8 process is running" << endl;
    this_thread::sleep_for(chrono::seconds(1));
  }
  cout << "Example 8 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 9 - launch with different environment" << endl;
  Process process9("printenv", "", {{"VAR1", "value1"}, {"VAR2", "second value"}}, [](const char *bytes, size_t n) {
    std::cout << std::string{bytes, n};
  });
  exit_status = process9.get_exit_status();
  cout << "Example 9 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 10 - launch with normal environment" << endl;
  Process process10("printenv", "", [](const char *bytes, size_t n) {
    std::cout << std::string{bytes, n};
  });
  exit_status = process10.get_exit_status();
  cout << "Example 10 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;


#else
  // Examples for Windows without MSYS2


  cout << "Example 1 - the mandatory Hello World" << endl;
  Process process1("cmd /C echo Hello World", "", [](const char *bytes, size_t n) {
    cout << "Output from stdout: " << string(bytes, n);
  });
  auto exit_status = process1.get_exit_status();
  cout << "Example 1 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 2 - cd into a nonexistent directory" << endl;
  Process process2(
      "cmd /C cd a_nonexistent_directory", "",
      [](const char *bytes, size_t n) {
        cout << "Output from stdout: " << string(bytes, n);
      },
      [](const char *bytes, size_t n) {
        cout << "Output from stderr: " << string(bytes, n);
        // Add a newline for prettier output on some platforms:
        if(bytes[n - 1] != '\n')
          cout << endl;
      });
  exit_status = process2.get_exit_status();
  cout << "Example 2 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 3 - async sleep process" << endl;
  thread thread3([]() {
    Process process3("timeout 2");
    auto exit_status = process3.get_exit_status();
    cout << "Example 3 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  });
  thread3.detach();
  this_thread::sleep_for(chrono::seconds(4));


  cout << endl
       << "Example 4 - killing async sleep process after 2 seconds" << endl;
  auto process4 = make_shared<Process>("timeout 4");
  thread thread4([process4]() {
    auto exit_status = process4->get_exit_status();
    cout << "Example 4 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;
  });
  thread4.detach();
  this_thread::sleep_for(chrono::seconds(2));
  process4->kill();
  this_thread::sleep_for(chrono::seconds(2));


  cout << endl
       << "Example 5 - demonstrates Process::try_get_exit_status" << endl;
  Process process5("timeout 3");
  while(!process5.try_get_exit_status(exit_status)) {
    cout << "Example 5 process is running" << endl;
    this_thread::sleep_for(chrono::seconds(1));
  }
  cout << "Example 5 process returned: " << exit_status << " (" << (exit_status == 0 ? "success" : "failure") << ")" << endl;


#endif
}