-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_services.cpp
269 lines (235 loc) · 9.26 KB
/
os_services.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//
// Copyright 2015-2021 by Kevin L. Goodwin [[email protected]]; All rights reserved
//
// This file is part of K.
//
// K is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// K is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with K. If not, see <http://www.gnu.org/licenses/>.
//
#include "ed_main.h"
void ConOut::Bell() {
if( g_fAllowBeep ) { fputc( '\a', stdout ); } // write RTL's stdout, which hasn't been touched since startup
}
// takes counted-string param so *pStart doesn't have to be forcibly
// NUL-terminated (it may be a const string)
//
PCChar Getenv( stref varnm ) {
ALLOCA_STRDUP( buf, slen, varnm.data(), varnm.length() ) 0 && DBG("Getenv '%s'", buf );
return getenv( buf );
}
//------------------------------------------------
bool PutEnvOk( PCChar varName, PCChar varValue ) { // params canNOT be stref since non-_WIN32 API which takes ASCIZ strings is called directly
// nullptr == varValue || !*varValue result in unsetenv (variable undefine) operation being performed
if( nullptr == varValue ) { varValue = ""; } // promote nullptr to empty string
0 && DBG( "*** %s(%s=%s)", FUNC, varName, varValue );
#if defined(_WIN32)
// MSVC-specific _putenv COPIES params (therefore SAFE to use with auto buffer params); if empty string follows "=", unsetenv is performed
const auto ln( Strlen(varName) );
const auto lv( Strlen(varValue) );
const auto bs( ln + lv + (1+1) ); // (1+1) = ('=' + '\0')
auto bp( PChar( alloca( bs ) ) );
scat( bp, bs, stref(varValue,lv), scat( bp, bs, "=", scpy( bp, bs, stref(varName,ln) ) ) );
const auto ok( 0 == _putenv( bp ) );
#else
// posix putenv NOT USED because parameter becomes part of process-duration environment of process (therefore should never pass auto buffers to it)
// posix setenv USED because it COPIES params into process-duration environment (therefore SAFE to use with auto buffer params)
const auto ok( 0 == (*varValue ? setenv( varName, varValue, 1 ) : unsetenv( varName )) );
#endif
if( !ok ) {
ErrorDialogBeepf( "%s(%s=%s) FAILED: %s", FUNC, varName, varValue, strerror( errno ) );
}
return ok;
}
STATIC_FXN bool PutEnvOk( PCChar szNameEqualsVal ) {
0 && DBG( "*** %s(%s)", FUNC, szNameEqualsVal );
const auto pEQ( strchr( szNameEqualsVal, '=' ) );
if( pEQ == szNameEqualsVal ) { // no name?
return false; // not OK
}
if( pEQ == nullptr ) {
return PutEnvOk( szNameEqualsVal, "" );
}
ALLOCA_STRDUP( nm, nmLen, szNameEqualsVal, pEQ - szNameEqualsVal - 1 );
return PutEnvOk( nm, pEQ + 1 );
}
bool PutEnvChkOk( PCChar szNameEqualsVal ) {
if( strchr( szNameEqualsVal, '=' ) ) {
return PutEnvOk( szNameEqualsVal );
}
else {
DBG( "%s '%s' missing '='", FUNC, szNameEqualsVal );
return false;
}
}
#ifdef fn_setenv
bool ARG::setenv() {
switch( d_argType ) {
default: return BadArg();
case TEXTARG: return PutEnvChkOk( d_textarg.pText );
case LINEARG: ATTR_FALLTHRU;
case BOXARG: for( ArgLineWalker aw( this ) ; !aw.Beyond() ; aw.NextLine() ) {
if( aw.GetLine() && !PutEnvChkOk( aw.c_str() ) ) {
return false;
}
}
return true;
}
}
#endif// fn_setenv
//------------------------------------------------
class OsEnv {
Path::str_t d_exe_path; // "C:\dir1\dir2\" (includes trailing '\')
Path::str_t d_exe_name; // "k"
std::string d_euname;
std::string d_hostname;
int d_euid;
public: //===================================================================
OsEnv();
PCChar ExePath() const { return d_exe_path.c_str(); } // includes trailing '\'
PCChar ExeName() const { return d_exe_name.c_str(); }
#if !defined(_WIN32)
int euid() const { return d_euid; }
PCChar euname() const { return d_euname.c_str(); }
PCChar hostname() const { return d_hostname.c_str(); }
#endif
};
#if !defined(_WIN32)
# include <pwd.h>
#endif
OsEnv::OsEnv() {
#if defined(_WIN32)
pathbuf pb;
const auto len( Win32::GetModuleFileName( nullptr, BSOB(pb) ) );
char exe_all [ _MAX_PATH+1 ]; // "C:\dir1\dir2\k.exe"
if( len >= sizeof(exe_all) ) {
DBG( "GetModuleFileName rv (%ld) >= sizeof(pb) (%" PR_SIZET ")\n", len, sizeof(pb) );
Win32::ExitProcess( 1 );
}
d_exe_path.assign( Path::RefDirnm( pb ) ); 0 && DBG( "d_exe_path=%s\n", d_exe_path.c_str() );
d_exe_name.assign( Path::RefFnm ( pb ) ); 0 && DBG( "d_exe_name=%s\n", d_exe_name.c_str() );
#else
const auto pw( getpwuid( d_euid=geteuid() ) );
if( pw ) { d_euname.assign( pw->pw_name ); }
{ char hnbuf[257]; hnbuf[0] = '\0';
if( 0 == gethostname( BSOB( hnbuf ) ) && hnbuf[0] ) {
d_hostname.assign( hnbuf );
}
}
STATIC_CONST char s_link_nm[] = "/proc/self/exe";
if( 0 ) {
// this is pointless since there is a race condition: between (this)
// lstat and the readlink call, the link content could be changed
struct stat sb;
if( lstat( s_link_nm, &sb ) == -1 ) {
perror( "lstat(/proc/self/exe) failed" );
return;
}
}
size_t bufbytes = PATH_MAX;
for(;;) {
char *linkname = static_cast<PChar>( malloc( bufbytes ) );
if( !linkname ) {
perror( "readlink(/proc/self/exe) memory exhausted" );
free( linkname );
return;
}
ssize_t r = readlink( s_link_nm, linkname, bufbytes );
if( r < 0 ) {
perror( "readlink(/proc/self/exe) < 0" );
free( linkname );
return;
}
if( r < bufbytes ) {
linkname[r] = '\0';
d_exe_path.assign( Path::RefDirnm( linkname ) ); 0 && DBG( "d_exe_path=%s\n", d_exe_path.c_str() );
d_exe_name.assign( Path::RefFnm ( linkname ) ); 0 && DBG( "d_exe_name=%s\n", d_exe_name.c_str() );
free( linkname );
return;
}
free( linkname );
bufbytes *= 2;
}
#endif
}
COMPLEX_STATIC_VAR OsEnv *g_Process;
void ThisProcessInfo::Init() {
g_Process = new OsEnv();
}
PCChar ThisProcessInfo::ExePath() { return g_Process->ExePath(); } // includes trailing '\'
PCChar ThisProcessInfo::ExeName() { return g_Process->ExeName(); }
#if !defined(_WIN32)
int ThisProcessInfo::euid() { return g_Process->euid(); }
PCChar ThisProcessInfo::euname() { return g_Process->euname(); }
PCChar ThisProcessInfo::hostname() { return g_Process->hostname(); }
#endif
InterlockedExchangeOperand s_fHaltExecution;
PCChar ExecutionHaltReason() {
switch( s_fHaltExecution ) {
default : Assert( 0 ) ; return "???";
case NEVER_REQUESTED : return "NEVER REQUESTED";
case USER_INTERRUPT : return "USER INTERRUPT";
case CMD_ABEND : return "unrecoverable CMD error";
case USER_CHOSE_EARLY_CMD_TERMINATE : return "User chose to quit CMD";
}
}
static_assert( NEVER_REQUESTED == 0, "NEVER_REQUESTED == 0" );
HaltReason ExecutionHaltRequested_( PCChar ident ) {
if( s_fHaltExecution ) {
if( ident ) {
DBG( "ExecutionHaltRequest '%s' seen by %s", ExecutionHaltReason(), ident );
}
return HaltReason(s_fHaltExecution);
}
return NEVER_REQUESTED;
}
#if defined(_WIN32)
PerfCounter::TCapture PerfCounter::FxnQueryPerformanceFrequency() {
TCapture rv;
/* Win32::*/QueryPerformanceFrequency( &rv );
return rv;
}
const PerfCounter::TCapture PerfCounter::s_PcFreq( FxnQueryPerformanceFrequency() );
#endif
DLinkHead <MainThreadPerfCounter> MainThreadPerfCounter::dhead;
void MainThreadPerfCounter::PauseAll() {
TCapture now;
GetTOD( &now );
DLINKC_FIRST_TO_LASTA(MainThreadPerfCounter::dhead,dlink,pCur) {
pCur->Capture_( now, paused );
}
}
void MainThreadPerfCounter::ResumeAll() {
TCapture now;
GetTOD( &now );
DLINKC_FIRST_TO_LASTA(MainThreadPerfCounter::dhead,dlink,pCur) {
if( pCur->state() == paused ) {
pCur->Start_( now );
}
}
}
ptrdiff_t xlat_cmdline_flag_chars( PCChar pCmdln, int *flags ) {
0 && DBG( "%s+ '%s'", __func__, pCmdln );
const auto p0( pCmdln );
do {
switch( *pCmdln ) {
break;case ' ': case HTAB:
break;case CH_NO_ECHO_CMDLN: *flags |= NO_ECHO_CMDLN;
break;case CH_IGNORE_ERROR : *flags |= IGNORE_ERROR ;
break;case CH_NOSHELL : *flags |= NOSHELL ;
break;default: goto PAST_OPTS;
}
} while( *(++pCmdln) );
PAST_OPTS: ;
0 && DBG( "%s- fl=0x%X '%s'", __func__, *flags, pCmdln );
return pCmdln - p0;
}