|
| 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 | + |
0 commit comments