annotate src/pt1_lnbd.c @ 125:e413158cae13

Add ushare project files.
author naoyan@johnstown.minaminoshima.org
date Sun, 03 Oct 2010 11:35:19 +0900
parents 9c7bc6c0327e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
123
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
1 #include <fcntl.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
2 #include <limits.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
3 #include <pwd.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
4 #include <sys/ioctl.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
5 #include <sys/stat.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
6 #include <sys/types.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
7 #include <signal.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
8 #include <stdio.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
9 #include <stdlib.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
10 #include <string.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
11 #include <unistd.h>
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
12 #include "pt1_ioctl.h"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
13
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
14 #ifdef O_NOFOLLOW
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
15 #define OPEN_FLAGS O_NOFOLLOW | O_NONBLOCK
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
16 #else
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
17 #define OPEN_FLAGS O_NONBLOCK
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
18 #endif
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
19
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
20 /* ヘッダに移動すること */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
21 #define C_RET_OK 0
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
22 #define C_RET_NG -1
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
23 #define MYNAME "pt1_lnb_enabler"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
24 #define PID_DIR "/var/run/"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
25 #define PIDFILENAME PID_DIR MYNAME ".pid"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
26 #define NUMBER "0123456789"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
27 #define device "/dev/pt1video0"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
28 #define ROOT "root"
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
29 /* ヘッダに移動すること */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
30
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
31 mode_t umask_val = 0133;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
32
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
33 void fin_action(int);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
34
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
35 int main() {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
36 struct sigaction ign_sigaction;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
37 struct sigaction fin_sigaction;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
38 FILE *fp;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
39 int i;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
40 int i_ret;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
41 int fd;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
42 int pt1_fd;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
43 int string_length;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
44 int number_length;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
45 int i_pid;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
46 struct stat orig_st;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
47 struct stat open_st;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
48 int flags;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
49 char file_name[] = PIDFILENAME;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
50 char buf[1024] = "";
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
51 pid_t mypid;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
52 char *p;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
53 struct passwd *p_st_passwd;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
54 uid_t uid_root;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
55
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
56 /* 初期処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
57 /* root権限で動作していなければ終了 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
58 p_st_passwd = getpwnam(ROOT);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
59 if (p_st_passwd == NULL) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
60 printf("faile to get pwent. id=[%s].\n", ROOT);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
61 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
62 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
63 uid_root = p_st_passwd->pw_uid;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
64 if( uid_root != getuid() ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
65 /* root 意外の実行権限なのでエラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
66 printf("This process must be run by root. uid=[%d].\n", getuid());
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
67 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
68 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
69 /* fork して親は即自殺してターミナルを離す */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
70 mypid = fork();
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
71 if( mypid == -1 ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
72 /* fork エラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
73 printf("fork error.\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
74 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
75 } else if ( mypid != 0 ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
76 /* 親プロセスは即終了(プロンプトに戻す) */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
77 return(C_RET_OK);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
78 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
79 umask(umask_val);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
80 ign_sigaction.sa_handler = SIG_IGN;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
81 fin_sigaction.sa_handler = fin_action;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
82 if (sigaction(SIGHUP, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
83 printf("failed to set signal handler. SIGHUP\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
84 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
85 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
86 if (sigaction(SIGTERM, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
87 printf("failed to set signal handler. SIGTERM\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
88 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
89 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
90 if (sigaction(SIGPIPE, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
91 printf("failed to set signal handler. SIGPIPE\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
92 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
93 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
94 if (sigaction(SIGINT, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
95 printf("failed to set signal handler. SIGINT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
96 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
97 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
98 if (sigaction(SIGQUIT, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
99 printf("failed to set signal handler. SIGQUIT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
100 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
101 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
102 if (sigaction(SIGILL, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
103 printf("failed to set signal handler. SIGILL\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
104 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
105 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
106 if (sigaction(SIGABRT, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
107 printf("failed to set signal handler. SIGABRT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
108 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
109 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
110 if (sigaction(SIGFPE, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
111 printf("failed to set signal handler. SIGFPE\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
112 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
113 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
114 if (sigaction(SIGSEGV, &fin_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
115 printf("failed to set signal handler. SIGSEGV\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
116 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
117 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
118 /* sleep を使うので SIGALRM は放置
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
119 * if (sigaction(SIGALRM, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
120 * printf("failed to set signal handler. SIGALRM\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
121 * exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
122 * }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
123 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
124 if (sigaction(SIGUSR1, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
125 printf("failed to set signal handler. SIGUSR1\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
126 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
127 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
128 if (sigaction(SIGUSR2, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
129 printf("failed to set signal handler. SIGUSR2\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
130 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
131 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
132 if (sigaction(SIGCHLD, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
133 printf("failed to set signal handler. SIGCHLD\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
134 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
135 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
136 if (sigaction(SIGCONT, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
137 printf("failed to set signal handler. SIGCONT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
138 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
139 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
140 if (sigaction(SIGTSTP, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
141 printf("failed to set signal handler. SIGTSTP\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
142 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
143 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
144 if (sigaction(SIGTTIN, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
145 printf("failed to set signal handler. SIGTTIN\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
146 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
147 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
148 if (sigaction(SIGTTOU, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
149 printf("failed to set signal handler. SIGTTOU\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
150 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
151 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
152 if (sigaction(SIGPOLL, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
153 printf("failed to set signal handler. SIGPOLL\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
154 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
155 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
156 if (sigaction(SIGIO, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
157 printf("failed to set signal handler. SIGIO\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
158 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
159 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
160 if (sigaction(SIGPROF, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
161 printf("failed to set signal handler. SIGPROF\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
162 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
163 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
164 if (sigaction(SIGSYS, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
165 printf("failed to set signal handler. SIGSYS\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
166 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
167 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
168 if (sigaction(SIGTRAP, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
169 printf("failed to set signal handler. SIGTRAP\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
170 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
171 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
172 if (sigaction(SIGURG, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
173 printf("failed to set signal handler. SIGURG\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
174 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
175 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
176 if (sigaction(SIGVTALRM, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
177 printf("failed to set signal handler. SIGVTALRM\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
178 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
179 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
180 if (sigaction(SIGXCPU, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
181 printf("failed to set signal handler. SIGXCPU\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
182 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
183 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
184 if (sigaction(SIGXFSZ, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
185 printf("failed to set signal handler. SIGXFSZ\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
186 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
187 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
188 /* ないって
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
189 * if (sigaction(SIGEMT, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
190 * printf("failed to set signal handler. SIGEMT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
191 * exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
192 * }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
193 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
194 if (sigaction(SIGSTKFLT, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
195 printf("failed to set signal handler. SIGSTKFLT\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
196 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
197 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
198 if (sigaction(SIGIO, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
199 printf("failed to set signal handler. SIGIO\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
200 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
201 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
202 if (sigaction(SIGCLD, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
203 printf("failed to set signal handler. SIGCLD\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
204 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
205 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
206 if (sigaction(SIGPWR, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
207 printf("failed to set signal handler. SIGPWR\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
208 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
209 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
210 /* ないって
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
211 * if (sigaction(SIGINFO, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
212 * printf("failed to set signal handler. SIGINFO\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
213 * exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
214 * }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
215 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
216 /* ないって
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
217 * if (sigaction(SIGLOST, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
218 * printf("failed to set signal handler. SIGLOST\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
219 * exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
220 * }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
221 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
222 if (sigaction(SIGWINCH, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
223 printf("failed to set signal handler. SIGWINCH\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
224 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
225 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
226 if (sigaction(SIGUNUSED, &ign_sigaction, NULL) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
227 printf("failed to set signal handler. SIGUNUSED\n");
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
228 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
229 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
230
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
231 /* ファイルチェック処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
232 if ((lstat(file_name, &orig_st) != 0) ||
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
233 (!S_ISREG(orig_st.st_mode)))
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
234 {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
235 /* ファイル無しの場合は前回正常終了 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
236 i_ret = C_RET_OK;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
237 } else {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
238 /* PIDファイルが存在する場合には、現在の状態を確認する。
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
239 * ・二重起動状態(pidファイルのpid値を持つプロセスが存在する)
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
240 * ・前回pidファイルを消さずに死んでしまった */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
241 i_ret = C_RET_NG;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
242 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
243 /* ここに TOCTOU 競合状態の問題があるが、
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
244 * /var/run はセキュアなディレクトリなので問題は無い */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
245 if (i_ret != C_RET_OK) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
246 fd = open(file_name, (OPEN_FLAGS | O_RDWR));
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
247 if (fd == -1) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
248 /* エラー処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
249 printf("open error. file:[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
250 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
251 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
252
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
253 if (fstat(fd, &open_st) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
254 /* エラー処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
255 printf("stat error. file:[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
256 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
257 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
258
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
259 if ((orig_st.st_mode != open_st.st_mode) ||
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
260 (orig_st.st_ino != open_st.st_ino) ||
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
261 (orig_st.st_dev != open_st.st_dev)) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
262 /* ファイルはすでにすり替えられている */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
263 printf("file switch has occurred. file:[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
264 close(fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
265 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
266 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
267
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
268 /* 問題ないファイルであることが確認できたので O_NONBLOCK
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
269 * を無効にする (省略可能) */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
270 if ((flags = fcntl(fd, F_GETFL)) == -1) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
271 /* エラー処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
272 printf("fcntl error. file:[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
273 close(fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
274 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
275 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
276
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
277 if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
278 /* エラー処理 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
279 printf("fcntl error. file:[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
280 close(fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
281 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
282 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
283 /* ファイルサイズが0以外である場合にはPID値のチェック */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
284 if(open_st.st_size != 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
285 /* ファイルを読みPID値を取得する */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
286 if((fp = fdopen(fd, "w+")) == NULL) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
287 /* fdopen()失敗 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
288 printf("fdopen error. fd:[%d].\n", fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
289 close(fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
290 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
291 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
292 if ( fgets(buf, sizeof(buf), fp) == NULL ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
293 /* fgetsエラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
294 printf("fgets error. fd:[%d].\n", fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
295 fclose(fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
296 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
297 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
298 buf[sizeof(buf)-1] = '\0';
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
299 p = strchr(buf, (int)'\n');
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
300 if(p != NULL) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
301 *p = '\0';
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
302 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
303 string_length = strlen(buf);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
304 number_length = strspn(buf, NUMBER);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
305 if ( string_length != number_length ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
306 /* PIDファイル異常 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
307 printf("invalid pid file[%s] buf[%s].\n", file_name, buf);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
308 fclose(fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
309 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
310 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
311 i_pid = atoi(buf);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
312
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
313 /* PIDファイルのPIDと同じPIDを持つプロセスが存在するか? */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
314 i_ret = kill(i_pid, 0);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
315 if ( i_ret == 0 ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
316 /* 存在する場合には二重起動とみなして処理終了 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
317 printf("process already exists. pid[%d]\n", i_pid);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
318 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
319 } else {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
320 /* 存在しない場合は前回異常終了なのでfpを先頭に戻して処理続行 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
321 rewind(fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
322 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
323 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
324 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
325 if ( i_ret == C_RET_OK ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
326 /* PIDファイルが存在しない場合はPIDファイルの新規作成を行う */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
327 fd = open(file_name, O_CREAT|O_EXCL|O_RDWR, 00644);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
328 if ( fd == -1 ){
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
329 /* open 失敗 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
330 printf("file open error. file_name[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
331 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
332 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
333 if((fp = fdopen(fd, "w+")) == NULL) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
334 /* fdopen()失敗 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
335 printf("fdopen error. fd:[%d].\n", fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
336 close(fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
337 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
338 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
339 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
340 /* fpのPIDファイルが書き込める状態なので、PIDを書き込む */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
341 mypid = getpid();
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
342 snprintf(buf, sizeof(buf), "%d\n", mypid);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
343 i_ret = fputs(buf, fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
344 if(i_ret == EOF) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
345 /* fputs エラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
346 printf("fputs error. file_name[%s].\n", file_name);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
347 fclose(fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
348 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
349 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
350 fclose(fp);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
351 /* PID関係処理終了*/
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
352 /* fd に pt1 デバイスを open */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
353 pt1_fd = open(device, O_RDONLY);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
354 if(pt1_fd == -1) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
355 /* open エラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
356 printf("open error. file_name[%s].\n", device);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
357 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
358 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
359 /* fcntl で LNB 電源を有効化する */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
360 if(ioctl(pt1_fd, LNB_ENABLE, 0) < 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
361 printf("Power on LNB failed. device:[%s].\n", device);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
362 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
363 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
364 close(pt1_fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
365 /* 無限待ち(終了は終了するsignalを受けたらとする) */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
366 while(1){
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
367 sleep(UINT_MAX);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
368 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
369 return C_RET_OK;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
370 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
371
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
372 /* 終了処理実行関数 */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
373 /* プロセスを終了させるべきsignalを受けたときに動作する */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
374 void fin_action(int sig) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
375 int pt1_fd;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
376 char file_name[] = PIDFILENAME;
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
377 /* fd に pt1 デバイスをopen */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
378 pt1_fd = open(device, O_RDONLY);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
379 if(pt1_fd == -1) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
380 /* open エラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
381 printf("open error. file_name[%s].\n", device);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
382 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
383 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
384 /* fcntl で LNB 電源断する */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
385 if(ioctl(pt1_fd, LNB_DISABLE, 0) < 0) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
386 printf("Power on LNB failed. device:[%s].\n", device);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
387 close(pt1_fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
388 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
389 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
390 /* fd close */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
391 close(pt1_fd);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
392 /* pidファイルを削除する */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
393 if ( unlink(file_name) != 0 ) {
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
394 /* pid file unlink エラー */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
395 printf("unlink error. file_name[%s].\n", device);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
396 exit(C_RET_NG);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
397 }
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
398 /* おしまい */
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
399 exit(C_RET_OK);
215a51fa3df3 add lnb daemon.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
diff changeset
400 }