Skip to content

Commit

Permalink
20190812
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Aug 11, 2019
1 parent bd78a72 commit b13fca7
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "lib/sds"]
path = lib/sds
url = https://github.com/antirez/sds
[submodule "lib/vec"]
path = lib/vec
url = https://github.com/rxi/vec
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SRC = $(wildcard *.c)
SRC += $(wildcard lib/sds/*.c)
SRC += $(wildcard lib/str-starts-with.c/src/*.c)
SRC += $(wildcard lib/vec/src/*.c)
OBJS = $(SRC:.c=.o)
CFLAGS = -Ilib -Wall

Expand Down
30 changes: 22 additions & 8 deletions cmd.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <signal.h>
#include "sds/sds.h"
// Copyright (c) 2019-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

#include "joker.h"

void make_cmd(struct Cmd *r, int argc, char *argv[])
{
(*r).command = sdsempty();
(*r).path = sdsempty();
(*r).name = sdsempty();
(*r).pid = 0;
(*r).argc = argc-1;
vec_init(&((*r).argv));
int i = 1;
for(;i<argc;i++){
vec_push(&((*r).argv), argv[i]);
if(i == 1){
(*r).command = sdscat((*r).command, argv[i]);
(*r).path = sdscat((*r).path, argv[i]);
char *s = strrchr(argv[i], '/');
if (s != NULL){
(*r).name = sdscat((*r).name, s+1);
Expand All @@ -34,4 +45,7 @@ void make_cmd(struct Cmd *r, int argc, char *argv[])
void free_cmd(struct Cmd *r)
{
sdsfree((*r).command);
sdsfree((*r).path);
sdsfree((*r).name);
vec_deinit(&((*r).argv));
}
33 changes: 31 additions & 2 deletions joker.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,49 @@
// Copyright (c) 2019-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

#ifndef __JOKER_H
#define __JOKER_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <signal.h>
#include "sds/sds.h"
#include "vec/src/vec.h"
#include "joker.h"

void help();

struct Cmd {
sds command;
sds path;
sds name;
pid_t pid;
int argc;
vec_str_t argv;
};

void run(struct Cmd *r, sds *e);
void make_cmd(struct Cmd *r, int argc, char *argv[]);
void free_cmd(struct Cmd *r);
void list_add(struct Cmd *r, sds *e);
void list_del(struct Cmd *r, sds *e);
void list_stop(struct Cmd *r, sds *e);
void list(sds *e);
void log(int pid, sds *e);
void list_all(sds *e);
void log_cmd(int pid, sds *e);

#endif
1 change: 1 addition & 0 deletions lib/vec
Submodule vec added at 20e842
77 changes: 53 additions & 24 deletions list.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <signal.h>
#include "str-starts-with.c/src/str-starts-with.h"
#include "sds/sds.h"
// Copyright (c) 2019-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

#include "joker.h"

void list_add(struct Cmd *r, sds *e)
{
FILE *f = fopen("/tmp/joker", "a");
FILE *f = fopen("/tmp/joker.list", "a");
if(!f) {
*e = sdscpy(*e, "can not open /tmp/joker");
*e = sdscpy(*e, "can not open /tmp/joker.list");
return;
}
int i = fprintf(f, "%d\t%s\t%s\n", (*r).pid, (*r).name, (*r).command);
if(i < 0) {
*e = sdscpy(*e, "can not write /tmp/joker");
*e = sdscpy(*e, "can not write /tmp/joker.list");
fclose(f);
return;
}
fclose(f);
}

void list_del(struct Cmd *r, sds *e)
void list_stop(struct Cmd *r, sds *e)
{
FILE *f = fopen("/tmp/joker", "r");
FILE *f = fopen("/tmp/joker.list", "r");
if(!f) {
*e = sdscpy(*e, "can not open /tmp/joker");
*e = sdscpy(*e, "can not open /tmp/joker.list");
return;
}
sds s = sdsempty();
Expand All @@ -42,31 +47,55 @@ void list_del(struct Cmd *r, sds *e)
char s3[6];
sprintf(s3, "%d", (*r).pid);
int i = str_starts_with(s2, s3);
if(i==0){
s = sdscat(s, s1);
if(i!=0){
s = sdscat(s, "[stopped]");
}
s = sdscat(s, s1);
}
fclose(f);

f = fopen("/tmp/joker", "w");
f = fopen("/tmp/joker.list", "w");
if(!f) {
*e = sdscpy(*e, "can not open /tmp/joker");
*e = sdscpy(*e, "can not open /tmp/joker.list");
return;
}
int i = fprintf(f, "%s", s);
if(i < 0) {
*e = sdscpy(*e, "can not write /tmp/joker");
*e = sdscpy(*e, "can not write /tmp/joker.list");
sdsfree(s);
fclose(f);
return;
}
sdsfree(s);
fclose(f);
}

void list(sds *e)
{
FILE *f = fopen("/tmp/joker", "r");
FILE *f = fopen("/tmp/joker.list", "r");
if(!f) {
*e = sdscpy(*e, "can not open /tmp/joker.list");
return;
}
for(;;){
char s1[1024];
char *s2 = fgets(s1, 1024, f);
if(s2 == NULL){
break;
}
int i = str_starts_with(s2, "[stopped]");
if(i==0){
printf("%s", s2);
}
}
fclose(f);
}

void list_all(sds *e)
{
FILE *f = fopen("/tmp/joker.list", "r");
if(!f) {
*e = sdscpy(*e, "can not open /tmp/joker");
*e = sdscpy(*e, "can not open /tmp/joker.list");
return;
}
for(;;){
Expand All @@ -80,10 +109,10 @@ void list(sds *e)
fclose(f);
}

void log(int pid, sds *e)
void log_cmd(int pid, sds *e)
{
char s[16+1];
sprintf(s, "/tmp/joker_%d", pid);
sprintf(s, "/tmp/joker.%d", pid);
FILE *f = fopen(s, "r");
if(!f) {
*e = sdscpy(*e, "can not open log");
Expand Down
43 changes: 30 additions & 13 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <signal.h>
#include "sds/sds.h"
// Copyright (c) 2019-present Cloud <cloud@txthinking.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

#include "joker.h"

void help()
{
printf("\njoker: run command in background\n\n");
printf(" <command> run your command\n");
printf(" list show running command list\n");
printf(" list [-a] show running command list\n");
printf(" stop <pid> stop a command\n");
printf(" log <pid> view log of command\n");
printf(" help show help\n");
Expand All @@ -30,7 +36,7 @@ int main(int argc, char *argv[])
return 0;
}
if(argc == 2 && strcmp(argv[1], "version") == 0){
printf("v20190810\n");
printf("v20190812\n");
return 0;
}
if(argc == 2 && strcmp(argv[1], "list") == 0){
Expand All @@ -44,12 +50,23 @@ int main(int argc, char *argv[])
sdsfree(e);
return 0;
}
if(argc == 3 && strcmp(argv[1], "list") == 0){
sds e = sdsempty();
list_all(&e);
if(strcmp(e, "") != 0){
printf("%s\n", e);
sdsfree(e);
return 0;
}
sdsfree(e);
return 0;
}
if(argc == 3 && strcmp(argv[1], "stop") == 0){
int pid = atoi(argv[2]);
if(pid == 0){
return 0;
}
int i = kill(pid, SIGINT);
int i = kill(pid, SIGTERM);
if(i != 0){
printf("%s\n", "stop failed");
return 0;
Expand All @@ -62,7 +79,7 @@ int main(int argc, char *argv[])
return 0;
}
sds e = sdsempty();
log(pid, &e);
log_cmd(pid, &e);
if(strcmp(e, "") != 0){
printf("%s\n", e);
sdsfree(e);
Expand All @@ -83,8 +100,8 @@ int main(int argc, char *argv[])
free_cmd(&r);
return 0;
}
sdsfree(e);

sdsfree(e);
free_cmd(&r);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Joker can help you run command in background, view running command list and stop

### Install

Download [joker](https://github.com/txthinking/mr2/releases/download/v20190810/joker)
Download [joker](https://github.com/txthinking/joker/releases/download/v20190812/joker)

### Source

Expand Down
Loading

0 comments on commit b13fca7

Please sign in to comment.