Skip to content

Commit bfbe519

Browse files
committed
version detect change
1 parent 472e1a4 commit bfbe519

2 files changed

Lines changed: 37 additions & 42 deletions

File tree

cdocker.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
#include "cdocker.h"
22

3-
#include <iostream>
43
#include <cstdlib>
54
#include <cstring>
65
#include <cctype>
76
#include <stdexcept>
8-
#include <cerrno>
97
#include <cstdio>
108

119
extern "C" {
12-
#include <limits.h>
1310
#include <sys/dir.h>
1411
#include <unistd.h>
15-
#include <signal.h>
1612
#include <sched.h>
1713
#include <fcntl.h>
1814
#include <sys/types.h>
@@ -70,14 +66,14 @@ CDocker::CDocker() : dockerPath("/var/lib/docker/containers"), dockerConfig("con
7066
}
7167

7268
void CDocker::loadConfigDirectory() {
73-
DIR *dir = NULL;
69+
DIR *dir = nullptr;
7470
const char *dpath = this->dockerPath.c_str();
75-
struct dirent *ent = NULL;
71+
struct dirent *ent = nullptr;
7672
char fbuf[PATH_MAX];
7773
memset(fbuf, 0, PATH_MAX);
7874

79-
if ((dir = opendir (dpath)) != NULL) {
80-
while ((ent = readdir (dir)) != NULL) {
75+
if ((dir = opendir (dpath)) != nullptr) {
76+
while ((ent = readdir (dir)) != nullptr) {
8177
if (strcmp(ent->d_name, ".") == 0) {
8278
continue;
8379
} else if (strcmp(ent->d_name, "..") == 0) {
@@ -87,7 +83,7 @@ void CDocker::loadConfigDirectory() {
8783
this->loadConfigV2((const char*)fbuf);
8884
}
8985
closedir(dir);
90-
dir = NULL;
86+
dir = nullptr;
9187
} else {
9288
perror("opendir()");
9389
throw std::runtime_error("opendir failed.");
@@ -97,7 +93,7 @@ void CDocker::loadConfigDirectory() {
9793
void CDocker::loadConfigV2(const char *configFile) {
9894
FILE *f;
9995
size_t lSize = 0;
100-
char *fcontent = NULL;
96+
char *fcontent = nullptr;
10197
std::string hostname;
10298
size_t lRead = 0;
10399
int pid;
@@ -115,7 +111,7 @@ void CDocker::loadConfigV2(const char *configFile) {
115111
}
116112

117113
fcontent = (char*)calloc(lSize, sizeof(char));
118-
if (fcontent == NULL) {
114+
if (fcontent == nullptr) {
119115
fclose(f);
120116
throw std::runtime_error("Calloc failed. Out of memory");
121117
}
@@ -152,19 +148,19 @@ int CDocker::findKeyString(char *fcontent, const char *k, std::string *ret)
152148
{
153149
char fdata[DBUF_SIZE_SMALL];
154150
memset(fdata, 0, DBUF_SIZE_SMALL);
155-
char *tmp = NULL;
156-
char *tmp_e = NULL;
151+
char *tmp = nullptr;
152+
char *tmp_e = nullptr;
157153
std::string lookupString;
158154
lookupString += "\"";
159155
lookupString += k;
160156
lookupString += "\":";
161157
tmp = strstr(fcontent, lookupString.c_str());
162-
if (tmp == NULL)
158+
if (tmp == nullptr)
163159
return -1;
164160
if (strlen(tmp) > strlen(k)+4)
165161
tmp+=strlen(k)+4;
166162
tmp_e = strchr(tmp, '"');
167-
if (tmp_e == NULL)
163+
if (tmp_e == nullptr)
168164
return -2;
169165
if (tmp_e-tmp <= 1)
170166
return -3;
@@ -177,25 +173,25 @@ int CDocker::findKeyInt(char *fcontent, const char *k, int *ret)
177173
{
178174
char fdata[DBUF_SIZE_SMALL];
179175
memset(fdata, 0, DBUF_SIZE_SMALL);
180-
char *tmp = NULL;
181-
char *tmp_e = NULL;
176+
char *tmp = nullptr;
177+
char *tmp_e = nullptr;
182178
int rint = 0;
183179
std::string lookupString;
184180
lookupString += "\"";
185181
lookupString += k;
186182
lookupString += "\":";
187183
tmp = strstr(fcontent, lookupString.c_str());
188-
if (tmp == NULL)
184+
if (tmp == nullptr)
189185
return -1;
190186
if (strlen(tmp) > strlen(k)+3)
191187
tmp+=strlen(k)+3;
192188

193189
tmp_e = tmp;
194-
while (tmp_e != NULL && isdigit(*tmp_e)) {
190+
while (tmp_e != nullptr && isdigit(*tmp_e)) {
195191
tmp_e++;
196192
}
197193

198-
if (tmp_e == NULL || tmp_e == tmp)
194+
if (tmp_e == nullptr || tmp_e == tmp)
199195
return -2;
200196
if (tmp_e-tmp <= 1)
201197
return -3;
@@ -209,7 +205,7 @@ int CDocker::findKeyInt(char *fcontent, const char *k, int *ret)
209205
void CDocker::setNSByHostname(std::string hostname) {
210206
std::map<std::string, CDockerContainer>::iterator it;
211207
char fbuf[PATH_MAX];
212-
NamespaceFile *nsfile = NULL;
208+
NamespaceFile *nsfile = nullptr;
213209

214210
std::vector<class NamespaceFile*> namespace_files;
215211
namespace_files.push_back(new NamespaceFile(CLONE_NEWIPC, "ipc"));
@@ -242,7 +238,7 @@ void CDocker::setNSByHostname(std::string hostname) {
242238
}
243239
nsfile->closefd();
244240
delete nsfile;
245-
namespace_files[i] = NULL;
241+
namespace_files[i] = nullptr;
246242
}
247243
}
248244

@@ -254,7 +250,7 @@ int CDocker::run(std::string hostname, std::vector<std::string> args) {
254250
char cwd[PATH_MAX];
255251
memset(cwd,0,PATH_MAX);
256252

257-
if (getcwd(cwd, PATH_MAX) == NULL) {
253+
if (getcwd(cwd, PATH_MAX) == nullptr) {
258254
perror("ERROR: getcwd()");
259255
throw std::runtime_error( "getcwd failed" );
260256
}

cphprunner.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include <iostream>
88

99
extern "C" {
10-
#include <limits.h>
11-
#include <unistd.h>
12-
#include <libgen.h>
10+
#include <limits.h>
11+
#include <unistd.h>
12+
#include <libgen.h>
1313
}
1414

1515
CPHPRunner::CPHPRunner() : default_version("php72"), lockFile("/var/lock/nophp"), isPHPBin(true)
@@ -24,6 +24,9 @@ CPHPRunner::CPHPRunner() : default_version("php72"), lockFile("/var/lock/nophp")
2424
this->allowed_version.insert("php74");
2525
this->allowed_version.insert("php80");
2626
this->allowed_version.insert("php81");
27+
this->allowed_version.insert("php82");
28+
this->allowed_version.insert("php83");
29+
this->allowed_version.insert("php84");
2730

2831
if (this->isPHPLocked())
2932
throw std::runtime_error("PHP cli is temporarily disabled");
@@ -38,10 +41,10 @@ bool CPHPRunner::isPHPLocked() {
3841
void CPHPRunner::setDefaultVersion() {
3942
const char *default_php_file = "/etc/docker-runner.default_php";
4043
char buffer[32];
41-
char *pos = NULL;
44+
char *pos = nullptr;
4245
memset(buffer, 0, 32);
4346

44-
FILE *f;
47+
FILE *f = nullptr;
4548

4649
if (access(default_php_file, R_OK) != 0)
4750
return;
@@ -50,7 +53,7 @@ void CPHPRunner::setDefaultVersion() {
5053
if (!f)
5154
return;
5255

53-
if (fgets(buffer, 32, f) == NULL) {
56+
if (fgets(buffer, 32, f) == nullptr) {
5457
fclose(f);
5558
return;
5659
}
@@ -65,7 +68,6 @@ void CPHPRunner::setDefaultVersion() {
6568
this->default_version = buffer;
6669
}
6770
fclose(f);
68-
return;
6971
}
7072

7173
void CPHPRunner::selectPHPVersion(const char *argv0) {
@@ -82,16 +84,13 @@ void CPHPRunner::selectPHPVersion(const char *argv0) {
8284
this->isPHPBin = false;
8385
}
8486

85-
for (size_t i=0; i<bname_length; i++) {
86-
if (isdigit(bname[i])) {
87-
version = atoi(bname+i);
88-
if (version > 50 && version < 90) {
89-
snprintf(selected_version_local, 64, "php%d", version);
90-
this->selected_version.assign(selected_version_local);
91-
free(dupargv_original);
92-
return;
93-
}
94-
break;
87+
if (bname_length > 2 && isdigit(bname[bname_length-1]) && isdigit(bname[bname_length-2])) {
88+
version = (int)strtol(bname+bname_length-2, nullptr, 10);
89+
if (version > 50 && version < 90) {
90+
snprintf(selected_version_local, 64, "php%d", version);
91+
this->selected_version.assign(selected_version_local);
92+
free(dupargv_original);
93+
return;
9594
}
9695
}
9796

@@ -114,7 +113,7 @@ void CPHPRunner::buildPHParguments(int argc, char **argv) {
114113
if (this->isPHPBin) {
115114
this->args.push_back("/usr/bin/php");
116115
for (int i=1; i<argc; i++) {
117-
if (argv[i] == NULL)
116+
if (argv[i] == nullptr)
118117
break;
119118
if (i == 0)
120119
args_merged += argv[i];
@@ -126,7 +125,7 @@ void CPHPRunner::buildPHParguments(int argc, char **argv) {
126125
}
127126
} else {
128127
for (int i=0; i<argc; i++) {
129-
if (argv[i] == NULL)
128+
if (argv[i] == nullptr)
130129
break;
131130
if (i == 0)
132131
args_merged += argv[i];

0 commit comments

Comments
 (0)