Skip to content

Commit 695dbc8

Browse files
committed
fix ssu_crontab remove command bug, ssu_crond multi-write log file bug
1 parent 6785985 commit 695dbc8

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

cron_support.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66
#include "cron_support.h"
77

8-
extern char reservation_command[BUFFER_SIZE][MAX_BUFFER_SIZE];
8+
FILE *fp;
9+
pthread_attr_t mutex; // 뮤텍스 객체 선언
10+
extern char reservation_command[BUFFER_SIZE][MAX_BUFFER_SIZE]; // 예약 명령 목록
911

1012
/**
1113
* @brief 입력한 명령행을 토큰 구조체로 변환
@@ -84,39 +86,39 @@ int get_reservation_command(void) // 예약 명령 목록 가져오
8486
*/
8587
void write_log(int command_type, char *command) // 로그 파일에 이력 기록
8688
{
87-
//FILE *fp;
8889
time_t now_t;
8990
struct tm *now_tm;
9091
char temp[MAX_BUFFER_SIZE];
9192

92-
/*
93+
pthread_mutex_lock(&mutex);
9394
if ((fp = fopen(CRONTAB_LOG, "r+")) == NULL)
9495
{
9596
fprintf(stderr, "write_log: fopen error for %s\n", CRONTAB_LOG);
9697
return;
9798
}
9899
fseek(fp, 0, SEEK_END);
99-
*/
100+
100101
time(&now_t);
101102
now_tm = localtime(&now_t);
102103

103104
switch (command_type) {
104105
case ADD:
105-
sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "add", command, CRONTAB_LOG);
106-
//fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "add", command);
106+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "add", command, CRONTAB_LOG);
107+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "add", command);
107108
break;
108109
case REMOVE:
109-
sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "remove", command, CRONTAB_LOG);
110-
//fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "remove", command);
110+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "remove", command, CRONTAB_LOG);
111+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "remove", command);
111112
break;
112113
case RUN:
113-
sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "run", command, CRONTAB_LOG);
114-
//fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "run", command);
114+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "run", command, CRONTAB_LOG);
115+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "run", command);
115116
break;
116117
}
117-
system(temp);
118+
//system(temp);
118119
//fflush(fp);
119-
//fclose(fp);
120+
fclose(fp);
121+
pthread_mutex_unlock(&mutex);
120122
}
121123

122124

ssu_crond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ char reservation_command[BUFFER_SIZE][MAX_BUFFER_SIZE];
1313
* @param argc 최초 실행시에 제공되는 인자들의 개수
1414
* @param argv 최초 실행시에 제공되는 파일들의 경로
1515
*/
16-
int main(int argc, char *argv[])
16+
int main(void)
1717
{
1818
pthread_t tid[BUFFER_SIZE]; // 스레드 ID 배열
1919
int reservation_count; // 예약 명령 개수

ssu_crontab.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,15 @@ void prompt(void) // 프롬프트 메인
105105
}
106106

107107
command_number = atoi(command.argv[1]);
108+
109+
if (reservation_count == 0) {
110+
fprintf(stderr, "prompt: no reservation command in ssu_crontab_file\n");
111+
break;
112+
}
113+
108114
if (command_number < 0 || command_number > reservation_count) {
109-
fprintf(stderr, "prompt: invalid COMMAND_NUMBER");
110-
return;
115+
fprintf(stderr, "prompt: invalid COMMAND_NUMBER\n");
116+
break;
111117
}
112118

113119
sprintf(command_buffer, "%s", reservation_command[command_number]);

0 commit comments

Comments
 (0)