Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

--

  • Loading branch information...
commit 54347ee8959271b0e9e5c30a30359b16f79bcc99 1 parent 0f0b584
@yasui0906 authored
Showing with 254 additions and 221 deletions.
  1. +25 −147 libmtn.c
  2. +4 −3 mtn.h
  3. +12 −1 mtnd.c
  4. +213 −69 mtnexec.c
  5. +0 −1  mtnexec.h
View
172 libmtn.c
@@ -101,6 +101,17 @@ int getcount(int id)
return(r);
}
+int is_empty(STR str)
+{
+ if(!str){
+ return(1);
+ }
+ if(*str == 0){
+ return(1);
+ }
+ return(0);
+}
+
int is_numeric(STR str)
{
while(*str){
@@ -3102,10 +3113,12 @@ STR modstr(STR str, char *n)
STR catstr(STR str1, STR str2)
{
int len = 1;
- len += strlen(str1);
- len += strlen(str2);
- str1 = xrealloc(str1, len);
- strcat(str1, str2);
+ len += str1 ? strlen(str1) : 0;
+ len += str2 ? strlen(str2) : 0;
+ str1 = str1 ? xrealloc(str1, len) : xcalloc(len);
+ if(str2){
+ strcat(str1, str2);
+ }
return(str1);
}
@@ -3137,6 +3150,9 @@ STR dotstr(STR str)
STR basestr(STR str)
{
char buff[PATH_MAX];
+ if(is_empty(str)){
+ return(str);
+ }
strcpy(buff, str);
return(modstr(str, basename(buff)));
}
@@ -3250,125 +3266,13 @@ STR findarg(ARG arg, STR str)
return(NULL);
}
-static STR convarg3(STR arg, MTNJOB *job)
-{
- int n;
- int m;
- STR s;
- if(!strcmp(arg, "")){
- s = joinarg(job->argl, " ");
- arg = modstr(arg, s);
- s = clrstr(s);
- }else if(!strcmp(arg, "/")){
- s = joinarg(job->argl, " ");
- s = basestr(s);
- arg = modstr(arg, s);
- s = clrstr(s);
- }else if(!strcmp(arg, ".")){
- s = joinarg(job->argl, " ");
- s = dotstr(s);
- arg = modstr(arg, s);
- s = clrstr(s);
- }else if(!strcmp(arg, "/.")){
- s = joinarg(job->argl, " ");
- s = basestr(s);
- s = dotstr(s);
- arg = modstr(arg, s);
- s = clrstr(s);
- }else if(!strcmp(arg, "H")){
- if(job->svr){
- arg = modstr(arg, job->svr->host);
- }else{
- arg = modstr(arg, "local");
- }
- }else{
- errno = 0;
- n = strtol(arg, NULL, 10);
- for(m=0;job->argl[m];m++);
- if(!errno){
- arg = modstr(arg, (n < m) ? job->argl[n] : "");
- }else{
- s = newstr("{");
- s = catstr(s, arg);
- s = catstr(s, "}");
- arg = modstr(arg, s);
- clrstr(s);
- }
- }
- return(arg);
-}
-
-static STR convarg2(STR arg, MTNJOB *job)
-{
- int i;
- STR p;
- STR q;
- STR r;
- if(!job->argl || !arg){
- return(arg);
- }
- i = 0;
- p = newstr(arg);
- while(*(p + i)){
- if(*(p + i) == '}'){
- *(p + i) = 0;
- i++;
- q = newstr(p);
- r = newstr(p + i);
- q = convarg3(q, job);
- arg = modstr(arg, q);
- arg = catstr(arg, r);
- clrstr(q);
- clrstr(r);
- break;
- }
- i++;
- }
- clrstr(p);
- return(arg);
-}
-
-STR convarg(STR arg, MTNJOB *job)
+int cntarg(ARG arg)
{
- int i;
- STR p;
- STR q;
- if(!job->argl || !arg){
- return(arg);
- }
-
- i = 0;
- p = newstr(arg);
- while(*(p + i)){
- if(*(p + i) == '{'){
- *(p + i) = 0;
- i++;
- q = newstr(p + i);
- q = convarg2(q, job);
- arg = modstr(arg, p);
- arg = catstr(arg, q);
- p = modstr(p, arg);
- q = clrstr(q);
- continue;
- }
- i++;
- }
- clrstr(p);
- return(arg);
-}
-
-ARG cpconvarg(ARG arg, MTNJOB *job)
-{
- int i;
- ARG r = newarg(0);
- if(!arg){
- return(r);
- }
- for(i=0;arg[i];i++){
- r = addarg(r, arg[i]);
- r[i] = convarg(r[i], job);
+ int i = 0;
+ if(arg){
+ for(i=0;arg[i];i++);
}
- return(r);
+ return(i);
}
static int job_flush_stdout(MTNJOB *job)
@@ -3455,29 +3359,3 @@ int job_close(MTNJOB *job)
return(0);
}
-//
-// 引数リストを展開する
-// args: 引数リスト
-// argl: 展開パラメータ
-//
-ARG cmdargs(MTNJOB *job)
-{
- int i;
- ARG cmd = newarg(0);
- if(job->args){
- for(i=0;job->args[i];i++){
- if(job->conv){
- cmd = addarg(cmd, convarg(newstr(job->args[i]), job));
- }else{
- cmd = addarg(cmd, newstr(job->args[i]));
- }
- }
- }
- if(!job->conv && job->argl){
- for(i=0;job->argl[i];i++){
- cmd = addarg(cmd, job->argl[i]);
- }
- }
- return(cmd);
-}
-
View
7 mtn.h
@@ -200,6 +200,7 @@ typedef struct mtn_context
} MTN;
/*=========================================================================*/
+int is_empty(STR str);
int is_numeric(STR str);
int is_export(MTNSVR *svr);
int is_execute(MTNSVR *svr);
@@ -271,17 +272,17 @@ STR modstr(STR str, char *n);
STR clrstr(STR str);
STR catstr(STR str1, STR str2);
ARG splitstr(STR str, STR delim);
+STR dotstr(STR str);
+STR basestr(STR str);
ARG newarg(int c);
ARG addarg(ARG arg, STR str);
ARG clrarg(ARG args);
ARG copyarg(ARG args);
STR poparg(ARG args);
-STR convarg(STR arg, MTNJOB *job);
-ARG cpconvarg(ARG arg, MTNJOB *job);
STR joinarg(ARG args, STR delim);
STR findarg(ARG arg, STR str);
-ARG cmdargs(MTNJOB *job);
+int cntarg(ARG arg);
size_t set_mtnstatus_members(MTN *mtn);
size_t set_mtnstatus_debuginfo(MTN *mtn);
View
13 mtnd.c
@@ -27,6 +27,7 @@
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/wait.h>
+#include <sched.h>
MTN *mtn;
MTND *ctx;
@@ -1167,6 +1168,7 @@ void mtnd_child_exit(MTNTASK *kt)
void mtnd_child_fork(MTNTASK *kt, MTNJOB *job)
{
+ struct sched_param schparam;
int pp[3][2];
pipe(pp[0]);
pipe(pp[1]);
@@ -1196,7 +1198,6 @@ void mtnd_child_fork(MTNTASK *kt, MTNJOB *job)
return;
}
//===== exec process =====
- setpgid(0,0);
close(0);
close(1);
close(2);
@@ -1209,6 +1210,16 @@ void mtnd_child_fork(MTNTASK *kt, MTNJOB *job)
close(pp[1][1]);
close(pp[2][0]);
close(pp[2][1]);
+
+ mtn->logtype = 0;
+ mtn->logmode = MTNLOG_STDERR;
+ if(setpgid(0, 0) == -1){
+ mtnlogger(mtn, 0, "[error] %s: setpgid %s\n", __func__, strerror(errno));
+ }
+ schparam.sched_priority = 0;
+ if(sched_setscheduler(0, SCHED_BATCH, &schparam) == -1){
+ mtnlogger(mtn, 0, "[error] %s: setscheduler %s\n", __func__, strerror(errno));
+ }
execlp("sh", "sh", "-c", job->cmd, NULL);
mtnlogger(mtn, 0, "[error] %s: %s %s\n", __func__, strerror(errno), job->cmd);
_exit(127);
View
282 mtnexec.c
@@ -58,6 +58,7 @@ void usage()
printf(" -p port # TCP/UDP port(default: 6000)\n");
printf(" -h # help\n");
printf(" --version # version\n");
+ printf(" --echo=string # \n");
printf(" --stdin=path # \n");
printf(" --stdout=path # \n");
printf(" --stderr=path # \n");
@@ -66,6 +67,173 @@ void usage()
printf("\n");
}
+STR strnum(STR a, char *n)
+{
+ int i;
+ STR s;
+ for(i=0;a[i];i++){
+ if(a[i]>='0' && a[i]<='9'){
+ n[i] = a[i];
+ }else{
+ break;
+ }
+ }
+ n[i] = 0;
+ s = newstr(a + i);
+ clrstr(a);
+ return(s);
+}
+
+STR convarg4(STR a, STR s)
+{
+ int i;
+ s = newstr(s);
+ if(is_empty(a)){
+ clrstr(a);
+ return(s);
+ }
+ for(i=0;a[i];i++){
+ if(a[i] == '/'){
+ s = basestr(s);
+ continue;
+ }
+ if(a[i] == '.'){
+ s = dotstr(s);
+ continue;
+ }
+ clrstr(s);
+ s = newstr("{");
+ s = catstr(s, a);
+ s = catstr(s, "}");
+ clrstr(a);
+ return(s);
+ }
+ clrstr(a);
+ return(s);
+}
+
+STR convarg3(STR a, MTNJOB *j)
+{
+ int m;
+ int n;
+ STR s;
+ char buff[ARG_MAX];
+
+ if(!strcmp(a, "H")){
+ if(j->svr){
+ a = modstr(a, j->svr->host);
+ }else{
+ a = modstr(a, "local");
+ }
+ return(a);
+ }
+
+ a = strnum(a, buff);
+ if(!strlen(buff)){
+ s = joinarg(j->argl, ctx->delim);
+ }else{
+ n = atoi(buff);
+ m = cntarg(j->argl);
+ s = newstr((n < m) ? j->argl[n] : "");
+ }
+ a = convarg4(a, s);
+ clrstr(s);
+ return(a);
+}
+
+STR convarg2(STR a, MTNJOB *j)
+{
+ int i;
+ STR p;
+ STR q;
+ STR r;
+ i = 0;
+ p = newstr(a);
+ while(*(p + i)){
+ if(*(p + i) == '}'){
+ *(p + i) = 0;
+ i++;
+ q = newstr(p);
+ r = newstr(p + i);
+ q = convarg3(q, j);
+ a = modstr(a, q);
+ a = catstr(a, r);
+ clrstr(q);
+ clrstr(r);
+ break;
+ }
+ i++;
+ }
+ clrstr(p);
+ return(a);
+}
+
+STR convarg(STR a, MTNJOB *j)
+{
+ int i;
+ STR p;
+ STR q;
+ if(!a){
+ return(NULL);
+ }
+ if(!j){
+ return(a);
+ }
+ i = 0;
+ p = newstr(a);
+ while(*(p + i)){
+ if(*(p + i) == '{'){
+ *(p + i) = 0;
+ i++;
+ q = newstr(p + i);
+ q = convarg2(q, j);
+ a = modstr(a, p);
+ a = catstr(a, q);
+ p = modstr(p, a);
+ q = clrstr(q);
+ continue;
+ }
+ i++;
+ }
+ clrstr(p);
+ return(a);
+}
+
+ARG cpconvarg(ARG a, MTNJOB *j)
+{
+ int i;
+ ARG r = newarg(0);
+ if(!a){
+ return(r);
+ }
+ for(i=0;a[i];i++){
+ r = addarg(r, a[i]);
+ r[i] = convarg(r[i], j);
+ }
+ return(r);
+}
+
+ARG cmdargs(MTNJOB *job)
+{
+ int i;
+ ARG cmd = newarg(0);
+ if(job->args){
+ for(i=0;job->args[i];i++){
+ if(job->conv){
+ cmd = addarg(cmd, convarg(newstr(job->args[i]), job));
+ }else{
+ cmd = addarg(cmd, newstr(job->args[i]));
+ }
+ }
+ }
+ if(!job->conv && job->argl){
+ for(i=0;job->argl[i];i++){
+ cmd = addarg(cmd, job->argl[i]);
+ }
+ }
+ return(cmd);
+}
+
int getjobcount(int mode)
{
int i = 0;
@@ -176,17 +344,6 @@ int is_delim(char c)
return(0);
}
-int is_empty(STR str)
-{
- if(!str){
- return(1);
- }
- if(*str == 0){
- return(1);
- }
- return(0);
-}
-
char *cmdline(pid_t pid)
{
int r;
@@ -830,6 +987,22 @@ MTNJOB *mtnexec_wait()
return(NULL);
}
+void mtnexec_initjob(MTNJOB *job, ARG arg)
+{
+ static int id = 0;
+ memset(job, 0, sizeof(MTNJOB));
+ job->id = id++;
+ job->uid = getuid();
+ job->gid = getgid();
+ job->cid = -1;
+ job->lim = ctx->cpu_lim;
+ job->conv = ctx->conv;
+ job->args = copyarg(ctx->cmdargs);
+ job->argl = copyarg(arg);
+ job->exit = -1;
+ job->cid = -1;
+}
+
MTNJOB *copyjob(MTNJOB *dst, MTNJOB *src)
{
if(dst){
@@ -883,16 +1056,16 @@ void mtnexec_dryrun(MTNJOB *job)
mtnlogger(mtn, 0, "\n");
}
-int mtnexec_fork(MTNJOB *job)
+int mtnexec_fork(MTNSVR *svr, ARG arg)
{
int f;
int pp[3][2];
+ MTNJOB *job;
struct epoll_event ev;
- if(!job){
- return(-1);
- }
-
+ job = mtnexec_wait();
+ mtnexec_initjob(job, arg);
+ job->svr = svr;
job->argc = cmdargs(job);
job->cmd = joinarg(job->argc, " ");
job->std = stdname(job);
@@ -1020,9 +1193,8 @@ int mtnexec_fork(MTNJOB *job)
_exit(127);
}
-int mtnexec_all(MTNJOB *job)
+int mtnexec_all(ARG arg)
{
- MTNJOB *j;
MTNSVR *s;
MTNSVR *svr = getinfo(mtn);
if(!svr){
@@ -1030,12 +1202,7 @@ int mtnexec_all(MTNJOB *job)
return(-1);
}
for(s=svr;s;s=s->next){
- if(!(j = mtnexec_wait())){
- break;
- }
- copyjob(j, job);
- j->svr = cpsvr(s);
- if(mtnexec_fork(j) == -1){
+ if(mtnexec_fork(cpsvr(s), arg) == -1){
break;
}
}
@@ -1155,9 +1322,7 @@ MTNSVR *filtersvr_cnt_prc(MTNSVR *svr)
limit = filtersvr_list_limit(list, count, level++);
for(s=svr;s;s=s->next){
if(!limit || (s->cnt.prc <= limit)){
- if((s->cnt.cld + 1) * 100 / s->cnt.cpu < 100){
- r = pushsvr(r, s);
- }else if(!s->cnt.cld && (s->cnt.cpu == 1)){
+ if(s->cnt.cld * 100 / s->cnt.cpu < 50){
r = pushsvr(r, s);
}
}
@@ -1251,42 +1416,41 @@ MTNSVR *filtersvr(MTNSVR *s)
return(s);
}
-void mtnexec_hybrid(MTNJOB *job)
+MTNSVR *mtnexec_hybrid(MTNSVR *svr)
{
int count;
if(ctx->mode != MTNEXECMODE_HYBRID){
- return;
+ return(svr);
}
- if(!job){
- return;
- }
- if(!job->svr){
- return;
+ if(!svr){
+ return(NULL);
}
count = getjobcount(1);
- if((job->svr->cnt.cld > count) && (ctx->cpu_num > count + 1)){
- job->svr = clrsvr(job->svr);
+ if((svr->cnt.cld > count) && (ctx->cpu_num > count + 1)){
+ svr = clrsvr(svr);
}
+ return(svr);
}
-int mtnexec_remote(MTNJOB *job)
+int mtnexec_remote(ARG arg)
{
+ MTNSVR *svr;
while(is_loop){
- if(!(job->svr = getinfo(mtn))){
+ if(!(svr = getinfo(mtn))){
if((ctx->mode != MTNEXECMODE_HYBRID) && is_loop){
mtnlogger(mtn, 0, "[mtnexec] error: node not found\n");
return(-1);
}
if(getjobcount(1) == 0){
- return(mtnexec_fork(job));
+ return(mtnexec_fork(NULL, arg));
}
}else{
- if((job->svr = filtersvr(job->svr))){
- mtnexec_hybrid(job);
- return(mtnexec_fork(job));
+ if((svr = filtersvr(svr))){
+ svr = mtnexec_hybrid(svr);
+ return(mtnexec_fork(svr, arg));
}
}
- while(!mtnexec_poll() && getjobcount(0));
+ while(is_loop && !mtnexec_poll() && getjobcount(0));
}
return(0);
}
@@ -1302,43 +1466,23 @@ int is_localbusy()
return(1);
}
-void mtnexec_initjob(MTNJOB *job, ARG arg)
-{
- static int id = 0;
- memset(job, 0, sizeof(MTNJOB));
- job->id = id++;
- job->uid = getuid();
- job->gid = getgid();
- job->cid = -1;
- job->lim = ctx->cpu_lim;
- job->conv = ctx->conv;
- job->args = copyarg(ctx->cmdargs);
- job->argl = copyarg(arg);
- job->exit = -1;
- job->cid = -1;
-}
-
-int mtnexec(ARG argp)
+int mtnexec(ARG arg)
{
int r = 0;
- MTNJOB *j;
-
- j = mtnexec_wait(); // プロセス数の上限に達していたらここで待機する
- mtnexec_initjob(j, argp);
switch(ctx->mode){
case MTNEXECMODE_LOCAL:
- r = mtnexec_fork(j);
+ r = mtnexec_fork(NULL, arg);
break;
case MTNEXECMODE_REMOTE:
case MTNEXECMODE_HYBRID:
- r = mtnexec_remote(j);
+ r = mtnexec_remote(arg);
break;
case MTNEXECMODE_ALL0:
- r = mtnexec_all(j);
+ r = mtnexec_all(arg);
break;
case MTNEXECMODE_ALL1:
- if(!(r = mtnexec_all(j))){
- r = mtnexec_fork(j);
+ if(!(r = mtnexec_all(arg))){
+ r = mtnexec_fork(NULL, arg);
}
break;
}
View
1  mtnexec.h
@@ -13,7 +13,6 @@
typedef struct mtnexec_context
{
int efd;
- int zero;
int conv;
int mode;
int info;
Please sign in to comment.
Something went wrong with that request. Please try again.