*NIX用コマンドライン手順書自動作成ツール開発
Revision | 1c4e0445bf74fb453ac931d0af2b961723e72e66 (tree) |
---|---|
Zeit | 2016-10-04 21:03:51 |
Autor | Koine Yuusuke(koinec) <koinec@user...> |
Commiter | Koine Yuusuke(koinec) |
* Implement: Implement Check & Block nested-boot.
@@ -54,7 +54,8 @@ TEST_FLAGS_LIB = -L/usr/local/lib -lcunit | ||
54 | 54 | TEST_TARGET = test_procedure |
55 | 55 | TEST_OBJS = test_procedure.o \ |
56 | 56 | test_cmdline.o \ |
57 | - test_outbuffer.o | |
57 | + test_outbuffer.o \ | |
58 | + test_dependos.o | |
58 | 59 | TEST_HEADERS = test_procedure.h |
59 | 60 | |
60 | 61 |
@@ -92,6 +93,9 @@ test_cmdline.o: test_cmdline.c $(HEADERS) $(TEST_HEADERS) | ||
92 | 93 | test_outbuffer.o: test_outbuffer.c $(HEADERS) $(TEST_HEADERS) |
93 | 94 | $(CC) -c -o test_outbuffer.o $(FLAGS_DEBUG) $(CFLAGS) $(TEST_FLAGS_COMPILE) \ |
94 | 95 | test_outbuffer.c |
96 | +test_dependos.o: test_dependos.c $(HEADERS) $(TEST_HEADERS) | |
97 | + $(CC) -c -o test_dependos.o $(FLAGS_DEBUG) $(CFLAGS) $(TEST_FLAGS_COMPILE) \ | |
98 | + test_dependos.c | |
95 | 99 | |
96 | 100 | $(TEST_TARGET): $(TEST_OBJS) $(OBJS_PARTS) |
97 | 101 | $(CC) -o $(TEST_TARGET) $(FLAGS_DEBUG) $(TEST_FLAGS_LIB) $(FLAGS_LIB) \ |
@@ -35,6 +35,7 @@ PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE*/ | ||
35 | 35 | |
36 | 36 | #define OUTPUT_RINGBUFFER_SIZE 4096 |
37 | 37 | #define DEFAULT_OUTPUT_FILENAME "procedure.txt" |
38 | +#define PROCEDURE_COMMAND_NAME "procedure" | |
38 | 39 | |
39 | 40 | #endif |
40 | 41 |
@@ -38,6 +38,7 @@ PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE*/ | ||
38 | 38 | #include<sys/user.h> |
39 | 39 | #include<limits.h> |
40 | 40 | #include<kvm.h> |
41 | +#include<libgen.h> | |
41 | 42 | |
42 | 43 | static kvm_t *pt_kd = NULL; |
43 | 44 |
@@ -129,6 +130,60 @@ goto_CheckGrandchildProcess: | ||
129 | 130 | /*===========================================================================*/ |
130 | 131 | PROCEDURE_DEPEND_FREEBSD_EXTERN |
131 | 132 | int |
133 | + CheckParentProcess( | |
134 | + int i_pid_now, | |
135 | + char *pstr_sameprocname ) | |
136 | +{ | |
137 | + int i_pid_result = 0x00; | |
138 | + int i_pid_parent; | |
139 | + int i_nentries; | |
140 | + int i_what; | |
141 | + int i_flags; | |
142 | + struct kinfo_proc *pt_kproc; | |
143 | + char str_cmdname[MAXPATHLEN + 8]; | |
144 | + | |
145 | + if(( NULL == pstr_sameprocname ) || ( 0 >= i_pid_now )) { | |
146 | + i_pid_result = -0x01; | |
147 | + goto goto_CheckParentProcess; | |
148 | + } | |
149 | + | |
150 | + if( '\0' == *pstr_sameprocname ) { | |
151 | + i_pid_result = -0x02; | |
152 | + goto goto_CheckParentProcess; | |
153 | + } | |
154 | + | |
155 | + i_pid_parent = i_pid_now; | |
156 | + | |
157 | + while( 1 < i_pid_parent ) { | |
158 | + i_flags = i_pid_parent; | |
159 | + i_what = KERN_PROC_PID; | |
160 | + i_nentries = 0; | |
161 | + | |
162 | + pt_kproc = kvm_getprocs( pt_kd, i_what, i_flags, &i_nentries ); | |
163 | + if( NULL == pt_kproc ) { | |
164 | + i_pid_result = -0x03; | |
165 | + goto goto_CheckParentProcess; | |
166 | + } | |
167 | + | |
168 | + i_pid_parent = (int)(pt_kproc->ki_ppid); | |
169 | + | |
170 | + basename_r( pt_kproc->ki_comm, str_cmdname ); | |
171 | + | |
172 | + if( !strncmp( str_cmdname, pstr_sameprocname, MAXPATHLEN ) ) { | |
173 | + i_pid_result = i_pid_parent; | |
174 | + goto goto_CheckParentProcess; | |
175 | + } | |
176 | + } | |
177 | + | |
178 | + | |
179 | +goto_CheckParentProcess: | |
180 | + return i_pid_result; | |
181 | +} | |
182 | + | |
183 | + | |
184 | +/*===========================================================================*/ | |
185 | +PROCEDURE_DEPEND_FREEBSD_EXTERN | |
186 | +int | |
132 | 187 | GetMaxArgSize( void ) |
133 | 188 | { |
134 | 189 | int i_mib[2]; |
@@ -48,6 +48,8 @@ PROCEDURE_DEPEND_FREEBSD_EXTERN | ||
48 | 48 | PROCEDURE_DEPEND_FREEBSD_EXTERN |
49 | 49 | int CheckGrandchildProcess( int i_pid_child, int i_pid_grandchild ); |
50 | 50 | PROCEDURE_DEPEND_FREEBSD_EXTERN |
51 | + int CheckParentProcess( int i_pid_now, char *pstr_sameprocname ); | |
52 | +PROCEDURE_DEPEND_FREEBSD_EXTERN | |
51 | 53 | int GetMaxArgSize( void ); |
52 | 54 | |
53 | 55 | #endif |
@@ -164,6 +164,7 @@ int | ||
164 | 164 | } |
165 | 165 | if( 0 < i_pos ) { |
166 | 166 | // Keyboard input for Terminal --- |
167 | + | |
167 | 168 | i_pid_grandchild |
168 | 169 | = CheckGrandchildProcess( gi_pid_child, i_pid_grandchild ); |
169 | 170 |
@@ -191,6 +192,11 @@ int | ||
191 | 192 | fflush(fscript); |
192 | 193 | |
193 | 194 | if( 0 < i_rdsz_inbuf ) { |
195 | + // Output ENTER-keycode(0x0d) & the following string for terminal --- | |
196 | + | |
197 | + if( 0 == i_pid_grandchild ) | |
198 | + { (void)write( gi_fd_master, ";./sink", 7 ); } | |
199 | + | |
194 | 200 | i_pid_grandchild = -0xff; |
195 | 201 | (void)write( gi_fd_master, pstr_in, i_rdsz_inbuf ); |
196 | 202 | (void)fprintf( fscript, "**Command: %s\n", CmdLine_GetCmdLineString( NULL )); |
@@ -35,6 +35,29 @@ PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE*/ | ||
35 | 35 | |
36 | 36 | |
37 | 37 | /*===================================================================*/ |
38 | +static int | |
39 | + CheckNestedboot( | |
40 | + void ) | |
41 | +{ | |
42 | + int i_err; | |
43 | + int i_pid_now; | |
44 | + | |
45 | + i_err = InitDependOS(); | |
46 | + if( 0x00 != i_err ) | |
47 | + { goto goto_CheckNestedBoot_post; } | |
48 | + | |
49 | + | |
50 | + i_pid_now = getppid(); | |
51 | + i_err = CheckParentProcess( i_pid_now, PROCEDURE_COMMAND_NAME ); | |
52 | + | |
53 | + (void)TermDependOS(); | |
54 | + | |
55 | +goto_CheckNestedBoot_post: | |
56 | + return i_err; | |
57 | +} | |
58 | + | |
59 | + | |
60 | +/*===================================================================*/ | |
38 | 61 | static void |
39 | 62 | ChildProcess_Main( |
40 | 63 | char **argv) |
@@ -71,9 +94,19 @@ int | ||
71 | 94 | main( |
72 | 95 | int argc, char *argv[]) |
73 | 96 | { |
97 | + int i_result; | |
74 | 98 | struct termios rtt; |
75 | 99 | struct winsize win; |
76 | 100 | |
101 | + // Output Copyright --- | |
102 | + | |
103 | + // Check Nested-boot procedure command. --- | |
104 | + i_result = CheckNestedboot(); | |
105 | + if( 0x00 != i_result ) { | |
106 | + puts("Error! : Can't nested-boot this command."); | |
107 | + return 0x01; | |
108 | + } | |
109 | + | |
77 | 110 | argv += 1; |
78 | 111 | |
79 | 112 | fname = DEFAULT_OUTPUT_FILENAME; |
@@ -36,6 +36,7 @@ PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE*/ | ||
36 | 36 | #include <sys/wait.h> |
37 | 37 | #include <sys/ioctl.h> |
38 | 38 | #include <sys/time.h> |
39 | +#include <sys/types.h> | |
39 | 40 | |
40 | 41 | #include <err.h> |
41 | 42 | #include <errno.h> |
@@ -0,0 +1,123 @@ | ||
1 | +/*PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE | |
2 | + * | |
3 | + * procedure - Command-Line Procedure Manual generater for FreeBSD/Linux | |
4 | + * | |
5 | + * Copyrihgt (c) 2016 Koine Yuusuke (koinec). / All Rights Reserved. | |
6 | + * | |
7 | + * Redistribution and use in source and binary forms, with or without | |
8 | + * modification, are permitted provided that the following conditions | |
9 | + * are met: | |
10 | + * 1. Redistributions of source code must retain the above copyright | |
11 | + * notice, this list of conditions and the following disclaimer. | |
12 | + * 2. Redistributions in binary form must reproduce the above copyright | |
13 | + * notice, this list of conditions and the following disclaimer in the | |
14 | + * documentation and/or other materials provided with the distribution. | |
15 | + * 4. Neither the name of the University nor the names of its contributors | |
16 | + * may be used to endorse or promote products derived from this software | |
17 | + * without specific prior written permission. | |
18 | + * | |
19 | + * THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND | |
20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | + * SUCH DAMAGE. | |
30 | + * | |
31 | +PrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcEdUrEpRoCeDuRePrOcE*/ | |
32 | + | |
33 | +#include "procedure.h" | |
34 | +#define PROCEDURE_SRC_TEST_DEPENDOS | |
35 | +#include "test_procedure.h" | |
36 | + | |
37 | + | |
38 | +/*===========================================================================*/ | |
39 | +void Test_DependOS_InitTermDependOS_test00_001(void) | |
40 | +{ | |
41 | + int i_err; | |
42 | + | |
43 | + i_err = InitDependOS(); | |
44 | + CU_ASSERT( 0x00 == i_err ); | |
45 | + | |
46 | + i_err = TermDependOS(); | |
47 | + CU_ASSERT( 0x00 == i_err ); | |
48 | + | |
49 | + return; | |
50 | +} | |
51 | + | |
52 | + | |
53 | +/*===========================================================================*/ | |
54 | +void Test_DependOS_CheckParentProcess_test00_001(void) | |
55 | +{ | |
56 | + int i_err; | |
57 | + int i_pid_now; | |
58 | + | |
59 | + i_err = InitDependOS(); | |
60 | + CU_ASSERT( 0x00 == i_err ); | |
61 | + | |
62 | + i_pid_now = (int)getpid(); | |
63 | + | |
64 | + i_err = CheckParentProcess( i_pid_now, "make" ); | |
65 | + CU_ASSERT( 0 < i_err ); | |
66 | + | |
67 | + i_err = TermDependOS(); | |
68 | + CU_ASSERT( 0x00 == i_err ); | |
69 | + | |
70 | + return; | |
71 | +} | |
72 | + | |
73 | + | |
74 | +/*---------------------------------------------------------------------------*/ | |
75 | +void Test_DependOS_CheckParentProcess_test01_001(void) | |
76 | +{ | |
77 | + int i_err; | |
78 | + int i_pid_now; | |
79 | + | |
80 | + i_err = InitDependOS(); | |
81 | + CU_ASSERT( 0x00 == i_err ); | |
82 | + | |
83 | + i_pid_now = (int)getpid(); | |
84 | + | |
85 | + i_err = CheckParentProcess( 0, "test" ); | |
86 | + CU_ASSERT( -0x01 == i_err ); | |
87 | + | |
88 | + i_err = CheckParentProcess( i_pid_now, NULL ); | |
89 | + CU_ASSERT( -0x01 == i_err ); | |
90 | + | |
91 | + i_err = CheckParentProcess( i_pid_now, "" ); | |
92 | + CU_ASSERT( -0x02 == i_err ); | |
93 | + | |
94 | + i_err = TermDependOS(); | |
95 | + CU_ASSERT( 0x00 == i_err ); | |
96 | + | |
97 | + return; | |
98 | +} | |
99 | + | |
100 | + | |
101 | +/*===========================================================================*/ | |
102 | +PROCEDURE_TEST_DEPENDOS_EXTERN | |
103 | +int | |
104 | + Test_DependOS( void ) | |
105 | +{ | |
106 | + CU_pSuite pt_test; | |
107 | + | |
108 | + pt_test = CU_add_suite( "DependOS", NULL, NULL ); | |
109 | + | |
110 | + CU_add_test( pt_test, "DependOS_InitTermDependOS_test00_001", | |
111 | + Test_DependOS_InitTermDependOS_test00_001 ); | |
112 | + | |
113 | + CU_add_test( pt_test, "DependOS_CheckParentProcess_test00_001", | |
114 | + Test_DependOS_CheckParentProcess_test00_001 ); | |
115 | + CU_add_test( pt_test, "DependOS_CheckParentProcess_test01_001", | |
116 | + Test_DependOS_CheckParentProcess_test01_001 ); | |
117 | + | |
118 | + return 0x00; | |
119 | +} | |
120 | + | |
121 | + | |
122 | +/* EOF ======================================================================*/ | |
123 | + |
@@ -44,6 +44,7 @@ int | ||
44 | 44 | |
45 | 45 | Test_CmdLine(); |
46 | 46 | Test_OutBuffer(); |
47 | + Test_DependOS(); | |
47 | 48 | |
48 | 49 | CU_basic_run_tests(); |
49 | 50 | CU_cleanup_registry(); |
@@ -52,6 +52,13 @@ PROCEDURE_TEST_CMDLINE_EXTERN int Test_CmdLine( void ); | ||
52 | 52 | #endif |
53 | 53 | PROCEDURE_TEST_OUTBUFFER_EXTERN int Test_OutBuffer( void ); |
54 | 54 | |
55 | +#ifdef PROCEDURE_SRC_TEST_DEPENDOS | |
56 | + #define PROCEDURE_TEST_DEPENDOS_EXTERN | |
57 | +#else | |
58 | + #define PROCEDURE_TEST_DEPENDOS_EXTERN extern | |
59 | +#endif | |
60 | +PROCEDURE_TEST_DEPENDOS_EXTERN int Test_DependOS( void ); | |
61 | + | |
55 | 62 | #endif |
56 | 63 | |
57 | 64 | /* EOF ======================================================================*/ |