Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2bd74e

Browse files
committed
#P3 complete
1 parent fea5f0f commit a2bd74e

File tree

13 files changed

+2052
-0
lines changed

13 files changed

+2052
-0
lines changed
2.14 MB
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
.SUFFIXES: .c .o
2+
.PHONY: dep all new clean
3+
4+
# Compiler
5+
CC = gcc
6+
# Compile option
7+
# -c: Generate object file
8+
# -W, -Wall: Print warning about all ambigous grammer
9+
# -Wextra: Print warning out of -W, -Wall
10+
# -O2: Optimization
11+
# -g: Debugging, PLEASE DELETE AFTER PROJECT COMPLETE!
12+
CFLAGS = -c -W -Wall -Wextra -g $(INC)
13+
14+
# Execute program file
15+
CRONTAB = ssu_crontab
16+
CROND = ssu_crond
17+
RSYNC = ssu_rsync
18+
# Source file
19+
COMMON_SRCS = common.c
20+
CRONTAB_SRCS = ssu_crontab.c
21+
CROND_SRCS = ssu_crond.c
22+
RSYNC_SRCS = ssu_rsync.c
23+
CRON_SUPPORT_SRCS = cron_support.c
24+
SRCS = $(COMMON_SRCS) $(CRONTAB_SRCS) $(CROND_SRCS) $(RSYNC_SRCS) $(CRON_SUPPORT_SRCS)
25+
# Object file
26+
COMMON_OBJS = $(COMMON_SRCS:.c=.o)
27+
CRONTAB_OBJS = $(CRONTAB_SRCS:.c=.o)
28+
CROND_OBJS = $(CROND_SRCS:.c=.o)
29+
RSYNC_OBJS = $(RSYNC_SRCS:.c=.o)
30+
CRON_SUPPORT_OBJS = $(CRON_SUPPORT_SRCS:.c=.o)
31+
OBJS = $(COMMON_OBJS) $(CRONTAB_OBJS) $(CROND_OBJS) $(RSYNC_OBJS) $(CRON_SUPPORT_OBJS)
32+
# Library file
33+
LIBS =
34+
# Include path
35+
INC =
36+
37+
# Execute file grneration
38+
# $@ = TARGET
39+
# $^ = DEPENDENCY
40+
# make all: Make all execute file
41+
all : $(OBJS)
42+
$(CC) -o $(CRONTAB) $(COMMON_OBJS) $(CRONTAB_OBJS) $(CRON_SUPPORT_OBJS) $(LIBS)
43+
$(CC) -o $(CROND) $(CROND_OBJS) $(CRON_SUPPORT_OBJS) $(LIBS) -lpthread
44+
$(CC) -o $(RSYNC) $(COMMON_OBJS) $(RSYNC_OBJS) $(LIBS)
45+
$(CRONTAB) : $(COMMON_OBJS) $(CRONTAB_OBJS) $(CRON_SUPPORT_OBJS)
46+
$(CC) -o $@ $^ $(LIBS)
47+
$(CROND) : $(CROND_OBJS) $(CRON_SUPPORT_OBJS)
48+
$(CC) -o $@ $^ $(LIBS) -lpthread
49+
$(RSYNC) : $(COMMON_OBJS) $(RSYNC_OBJS)
50+
$(CC) -o $@ $^ $(LIBS)
51+
52+
# Object file generation
53+
$(OBJS):
54+
$(CC) $(CFLAGS) $(SRCS)
55+
56+
# make dep: Make dependency information file
57+
dep:
58+
$(CC) -M $(INC) $(SRCS) > .dependency
59+
60+
# make new: Re-generation
61+
new:
62+
$(MAKE) clean
63+
$(MAKE) all
64+
65+
# make clean: Remove all generated file
66+
clean:
67+
rm -rf $(OBJS) $(CRONTAB) $(CROND) $(RSYNC)
68+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
C_SSU_Crontab&Rsync
2+
3+
#Requirement
4+
1. Makefile 통합 생성
5+
- https://jhb.kr/56?category=735240
6+
- https://mug896.github.io/make-script/rules.html
7+
2. Doxyzen 주석 규칙 적용
8+
- https://github.com/xpressengine/xe-core/wiki/%EC%BD%94%EB%93%9C-%EB%AC%B8%EC%84%9C%ED%99%94%EB%A5%BC-%EC%9C%84%ED%95%9C-%EC%A3%BC%EC%84%9D-%EA%B7%9C%EC%B9%99(Draft-v0.1)
9+
3. gdb 디버깅
10+
- https://blankspace-dev.tistory.com/434?category=705047
11+
4. MacOS systemctl
12+
- https://devlog.jwgo.kr/2019/07/03/how-do-i-check-if-a-service-is-running-in-mac/
13+
5. crontab 등록
14+
- https://zetawiki.com/wiki/스크립트_crontab_등록
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @file common.c
3+
* @brief 프로젝트에서 공통적으로 사용되는 함수 정의
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#include "common.h"
7+
8+
/**
9+
* @brief 실행 시간 출력
10+
* @param begin_t 시작 시간
11+
* @param end_t 종료 시간
12+
*/
13+
void ssu_runtime(struct timeval *begin_t, struct timeval *end_t) // 실행 시간 출력
14+
{
15+
end_t->tv_sec -= begin_t->tv_sec;
16+
17+
if(end_t->tv_usec < begin_t->tv_usec){
18+
end_t->tv_sec--;
19+
end_t->tv_usec += SECOND_TO_MICRO;
20+
}
21+
22+
end_t->tv_usec -= begin_t->tv_usec;
23+
printf("Runtime: %ld:%ld(sec:usec)\n", end_t->tv_sec, end_t->tv_usec);
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @file common.h
3+
* @brief 프로젝트에서 공통적으로 사용되는 라이브러리, 매크로 정의
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#ifndef COMMON_H
7+
#define COMMON_H
8+
9+
//#define DEBUG
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <unistd.h>
14+
#include <stdbool.h>
15+
#include <string.h>
16+
#include <time.h>
17+
#include <sys/time.h>
18+
19+
// 버퍼 크기
20+
#define BUFFER_SIZE 256
21+
#define MAX_BUFFER_SIZE 1024
22+
23+
// 시간 크기
24+
#define SECOND_TO_MICRO 100000
25+
26+
void ssu_runtime(struct timeval *begin_t, struct timeval *end_t); // 실행시간 출력
27+
28+
#endif // COMMON_H
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @file cron_support.c
3+
* @brief ssu_crontab, ssu_crond에서 공통적으로 사용되는 함수
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#include "cron_support.h"
7+
8+
FILE *fp;
9+
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 뮤텍스 객체 선언
10+
extern char reservation_command[BUFFER_SIZE][MAX_BUFFER_SIZE]; // 예약 명령 목록
11+
12+
/**
13+
* @brief 입력한 명령행을 토큰 구조체로 변환
14+
* @param command 명령행 토큰 구조체
15+
* @param command_buffer 명령 문자열
16+
*/
17+
void make_command_token(CommandToken *command, char *command_buffer) // 입력한 명령행을 토큰 구조체로 변환
18+
{
19+
char *tmp;
20+
char *last;
21+
#ifdef DEBUG
22+
printf("make_command_token(): command_buffer = %s\n", command_buffer);
23+
#endif
24+
command->argv = (char**)calloc(BUFFER_SIZE, sizeof(char*));
25+
command->argc = 0;
26+
27+
if ((tmp = strtok_r(command_buffer, " ", &last)) == NULL)
28+
return;
29+
#ifdef DEBUG
30+
printf("make_command_token(): command->argv[%d] = %s\n", command->argc, tmp);
31+
#endif
32+
33+
command->argv[command->argc] = (char *)calloc(BUFFER_SIZE, sizeof(char));
34+
strcpy(command->argv[command->argc++], tmp);
35+
36+
while ((tmp = strtok_r(NULL, " ", &last)) != NULL) {
37+
#ifdef DEBUG
38+
printf("make_command_token(): command->argv[%d] = %s\n", command->argc, tmp);
39+
#endif
40+
command->argv[command->argc] = (char *)calloc(BUFFER_SIZE, sizeof(char));
41+
strcpy(command->argv[command->argc++], tmp);
42+
}
43+
}
44+
45+
/**
46+
* @brief 명령행 구조체 초기화
47+
* @param command 명령행 구조체
48+
*/
49+
void free_command_token(CommandToken *command) // 명령행 구조체 초기화
50+
{
51+
int i;
52+
53+
for (i = 0; i < command->argc; i++)
54+
free(command->argv[i]);
55+
free(command->argv);
56+
}
57+
58+
/**
59+
* @brief 예약 명령 목록 가져오기
60+
*/
61+
int get_reservation_command(void) // 예약 명령 목록 가져오
62+
{
63+
FILE *fp;
64+
65+
int count = 0;
66+
67+
if ((fp = fopen(CRONTAB_FILE, "r+")) == NULL) {
68+
fprintf(stderr, "get_reservation_command: fopen error for %s\n", CRONTAB_FILE);
69+
return count;
70+
}
71+
72+
while(fscanf(fp, "%[^\n]\n", reservation_command[count]) > 0)
73+
count++;
74+
#ifdef DEBUG
75+
printf("get_reservation_command: reservation_count = %d\n", count);
76+
#endif
77+
78+
fclose(fp);
79+
return count;
80+
}
81+
82+
/**
83+
* @brief 로그 파일에 이력 기록
84+
* @param command_type 명령 타입 번호
85+
* @param command 명령 문자열
86+
*/
87+
void write_log(int command_type, char *command) // 로그 파일에 이력 기록
88+
{
89+
time_t now_t;
90+
struct tm *now_tm;
91+
92+
pthread_mutex_lock(&mutex);
93+
94+
if ((fp = fopen(CRONTAB_LOG, "r+")) == NULL)
95+
fp = fopen(CRONTAB_LOG, "w");
96+
97+
fseek(fp, 0, SEEK_END);
98+
99+
time(&now_t);
100+
now_tm = localtime(&now_t);
101+
102+
switch (command_type) {
103+
case ADD:
104+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "add", command, CRONTAB_LOG);
105+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "add", command);
106+
break;
107+
case REMOVE:
108+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "remove", command, CRONTAB_LOG);
109+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "remove", command);
110+
break;
111+
case RUN:
112+
//sprintf(temp, "echo \"[%.24s] %s %s\" >> %s", asctime(now_tm), "run", command, CRONTAB_LOG);
113+
fprintf(fp, "[%.24s] %s %s\n", asctime(now_tm), "run", command);
114+
break;
115+
}
116+
fclose(fp);
117+
pthread_mutex_unlock(&mutex);
118+
}
119+
120+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @file cron_support.h
3+
* @brief ssu_crontab과 ssu_crond에서 공통적으로 사용되는 라이브러리, 매크로 정의
4+
* @author 김병준 (kbj9704@gmail.com)
5+
*/
6+
#ifndef CRON_SUPPORT_H
7+
#define CRON_SUPPORT_H
8+
9+
#include <pthread.h>
10+
#include "common.h"
11+
12+
/**
13+
* @brief 명령어 타입 번호
14+
*/
15+
#define ADD 1
16+
#define REMOVE 2
17+
#define RUN 3
18+
#define EXIT 4
19+
#define UNKNOWN 5
20+
21+
/**
22+
* @brief 주기 타입 번호
23+
*/
24+
#define MINUTE 1
25+
#define HOUR 2
26+
#define DAY 3
27+
#define MONTH 4
28+
#define DAY_OF_WEEK 5
29+
30+
/**
31+
* @brief 파일 이름
32+
*/
33+
#define CRONTAB_FILE "ssu_crontab_file"
34+
#define CRONTAB_LOG "ssu_crontab_log"
35+
36+
/**
37+
* @brief 프롬프트 명령행 토큰 구조체
38+
* @param argc 인자 개수
39+
* @param argv 인자 토큰
40+
*/
41+
typedef struct CommandTokenStruct
42+
{
43+
int argc; // 인자 개수
44+
char **argv; // 인자 토큰
45+
} CommandToken;
46+
47+
void make_command_token(CommandToken *command, char *command_buffer); // 입력한 명령행을 토큰 구조체로 변환
48+
void free_command_token(CommandToken *command); // 토큰 구조체 메모리 해제
49+
int get_reservation_command(void); // 예약 명령 목록 가져오기
50+
void write_log(int command_type, char *command); // 로그 파일에 이력 기록
51+
52+
#endif // CRON_SUPPORT_H

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /