-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckprog.cc
200 lines (177 loc) · 5.37 KB
/
checkprog.cc
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
* Portions of this software are Copyright (c) 2023-2024 HPC-Gridware GmbH
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include "basis_types.h"
#include "msg_utilbin.h"
#include "uti/sge_os.h"
#include "uti/sge_smf.h"
void sge_exit_wrapper(int);
void usage();
int main(int, char *[]);
/*----------------------------------------------------------------------
* sge_exit_wrapper
* wrapper for standalone program
*----------------------------------------------------------------------*/
void sge_exit_wrapper(
int i
) {
exit(i);
}
/*----------------------------------------------------------------------*/
void usage_checkprog()
{
fprintf(stderr, "\n%s\n", MSG_COMMAND_USAGECHECKPROG);
/*fprintf(stderr, "check the first 8 letters of process basename\n\n");
fprintf(stderr, "exit status: 0 if process was found\n");
fprintf(stderr, " 1 if process was not found\n");
fprintf(stderr, " 2 if ps program couldn't be spawned\n"); */
exit(2);
}
/*----------------------------------------------------------------------*/
void usage_getprogs()
{
fprintf(stderr, "\n%s\n", MSG_COMMAND_USAGEGETPROGS );
/*
fprintf(stderr, "check and list pids of \"processname\"\n\n");
fprintf(stderr, "exit status: 0 if process(es) were found\n");
fprintf(stderr, " 1 if process(es) was not found\n");
fprintf(stderr, " 2 if ps program couldn't be spawned\n");
*/
exit(2);
}
/*----------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
int res;
pid_t pid = 0;
pid_t pids[10000];
char *ptr;
int checkit, i;
ptr = strrchr(argv[0], '/');
if (ptr)
ptr++;
else
ptr = argv[0];
if (!strcmp(ptr, "checkprog"))
checkit = 1;
else if (!strcmp(ptr, "getprogs"))
checkit = 0;
else {
fprintf(stderr, "%s\n", MSG_COMMAND_CALLCHECKPROGORGETPROGS);
exit(1);
}
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-help"))) {
if (checkit)
usage_checkprog();
else
usage_getprogs();
}
if (checkit ) {
if( argc == 3 )
if( atoi(argv[1]) == 0) {
usage_checkprog();
}
}
else if (!checkit && argc != 2)
usage_getprogs();
if (checkit) {
int printpid = 0;
char* process_name = nullptr;
switch( argc ) {
case 2:
if(strcmp(argv[1], "-ppid" ) == 0) {
printpid = 1;
} else {
usage_checkprog();
}
break;
case 3:
pid = atoi(argv[1]);
if(pid == 0) {
usage_checkprog();
}
process_name = argv[2];
break;
default:
usage_checkprog();
}
if( printpid ) {
pid = getppid();
printf("%ld\n", (long)pid );
res = 0;
} else {
#if defined(SOLARIS)
/* Init shared SMF libs if necessary */
if (sge_smf_used() == 1 && sge_smf_init_libs() != 0) {
fprintf(stderr, MSG_COMMAND_SMF_INIT_FAILED);
fprintf(stderr, "\n");
exit(1);
}
#endif
res = sge_checkprog(pid, process_name, PSCMD);
if (res == 1)
printf(MSG_PROC_PIDNOTRUNNINGORWRONGNAME_IS, (int) pid, argv[2]);
else if (res == 0)
printf(MSG_PROC_PIDISRUNNINGWITHNAME_IS , (int) pid, argv[2]);
else if (res == -1)
printf("%s", MSG_COMMAND_SPANPSFAILED);
if (res == -1)
res = 2;
printf("\n");
}
}
else {
res = sge_get_pids(pids, 10000, argv[1], PSCMD);
if (res == -1)
printf(MSG_COMMAND_RUNPSCMDFAILED_S , PSCMD);
else if (res == 0)
printf(MSG_PROC_FOUNDNOPROCESS_S , argv[1]);
else {
printf(MSG_PROC_FOUNDPIDSWITHNAME_S , argv[1]);
for (i = 0; i < res; i++)
printf(pid_t_fmt "\n", pids[i]);
}
if (res == -1)
res = 2;
else if (res == 0)
res = 1;
else
res = 0;
printf("\n");
}
return res;
}