kproc.c

最終更新:2009/12/16

kproc.c

001: /* kproc.c
002:  * プロセス処理のサンプル
003:  */
004: 
005: #define USAGE "Usage: kproc <prog_name> <args>"
006: 
007: #include "apr_general.h"
008: #include "apr_file_io.h"
009: #include "apr_errno.h"
010: #include "apr_env.h"
011: #include "apr_strings.h"
012: #include "apr_thread_proc.h"
013: 
014: #include "mystab.h"
015: 
016: int apr_my_main (
017:   int ac, char **av
018:   , apr_file_t * astdin
019:   , apr_file_t * astdout
020:   , apr_file_t * astderr
021:   , apr_pool_t * pool
022: ) {
023: 
024:   apr_status_t rv=APR_SUCCESS;
025:   
026:   char *prog_name = NULL;
027:   char **args = NULL;
028:   
029:   apr_proc_t proc;
030:   apr_pool_t *pool2 = NULL;
031:   
032:   apr_procattr_t *proc_attr = NULL;
033:   
034:   if (ac < 2) {
035:     apr_file_printf(astderr, "%s\n", USAGE);
036:     goto _FINALLY_;
037:   }
038: 
039:   /* 子プロセスが実行するコマンド */
040: 
041:   prog_name = av[1];
042: 
043:   /* 子プロセスの実行するコマンドへの引数配列の準備 */
044: 
045:   if (ac > 1) {
046:     int i;
047:     args = (char**)apr_palloc(pool, sizeof(char*)*(ac));
048:     for (i=0; i<ac-1; i++) { /* args[0] には av[1] が入る */
049:       args[i] = av[i+1];
050:     }
051:     args[i]=NULL; /* 最後はヌル */
052:     
053:   }
054: 
055:   /* 子プロセス用引数配列の確認 */
056: 
057:   if (args) {
058:     char **x=args;
059:     printf("#prog_name=[%s]\n", prog_name);
060:     for (; *x; x++) {
061:       printf("#args[%d]=[%s]\n", x-args, *x);
062:     }
063:   }
064:   
065:   /* 子プロセス用メモリプール生成 */
066: 
067:   rv=apr_pool_create(&pool2, NULL);
068:   if (APR_SUCCESS != rv) {
069:     goto _FINALLY_;
070:   }
071: 
072:   /* 子プロセスの属性情報生成 */
073: 
074:   rv=apr_procattr_create(&proc_attr, pool);
075:   if (APR_SUCCESS != rv) {
076:     goto _FINALLY_;
077:   }
078: 
079:   /* 子プロセスのコマンドタイプ設定 */
080: 
081:   rv=apr_procattr_cmdtype_set(proc_attr, APR_PROGRAM);
082:   if (APR_SUCCESS != rv) {
083:     goto _FINALLY_;
084:   }
085: 
086:   /* 子プロセスのパイプ準備 */
087: 
088:   rv=apr_procattr_io_set(proc_attr, APR_FULL_BLOCK, APR_FULL_BLOCK, APR_NO_PIPE);
089:   if (APR_SUCCESS != rv) {
090:     goto _FINALLY_;
091:   }
092: 
093:   /* 子プロセスの生成 */
094: 
095:   rv = apr_proc_create(&proc, prog_name, (const char*const*)args, NULL, proc_attr, pool2);
096:   if (APR_SUCCESS!=rv) {
097:     goto _FINALLY_;
098:   }
099:   
100:   /* 子プロセスへの送信 */
101:   
102:   if (proc.in) {
103:     apr_file_printf(proc.in, "This is a pen.\n");
104:     apr_file_printf(proc.in, "That is a pencil.\n");
105:     apr_file_close(proc.in);
106:   }
107:   
108:   /* 子プロセスからの受信 */
109: 
110:   if (proc.out) {
111:     #define BUF_SIZE 1024
112:     char buf[BUF_SIZE];
113:     while (1) {
114:       if(APR_EOF == apr_file_eof(proc.out)) {
115:         break;
116:       }
117:       if (APR_SUCCESS == apr_file_gets(buf, BUF_SIZE, proc.out)) {
118:         apr_file_printf(astdout, "[%s]\n", buf);
119:       }
120:     }
121:     apr_file_close(proc.out);
122:   }
123: 
124:   /* 子プロセスの終了待ち */
125: 
126:   {
127:     int exitcode=0;
128:     apr_exit_why_e why;
129:     char *tmp = "";
130:     rv = apr_proc_wait(&proc, &exitcode, &why, APR_WAIT);
131:     if (APR_CHILD_DONE != rv) {
132:       goto _FINALLY_;
133:     }
134:     
135:     rv = APR_SUCCESS;
136:     
137:     apr_file_printf(astdout, "exitcode=[%d], [", exitcode);
138:     switch (why) {
139:       case APR_PROC_EXIT:
140:       tmp = "process exited normally";
141:       break;
142:       case APR_PROC_SIGNAL:
143:       tmp = "process exited due to a signal";
144:       break;
145:       case APR_PROC_SIGNAL_CORE:
146:       tmp = "process exited and dumped a core file";
147:     }
148:     apr_file_printf(astdout, "%s]\n", tmp);
149:   }
150:   
151:   _FINALLY_:
152: 
153:   /* 子プロセス用メモリプールの解放 */
154:   
155:   if (pool2) {
156:     apr_pool_destroy(pool2);
157:     pool2 = NULL;
158:   }
159: 
160:   if (APR_SUCCESS != rv) {
161:     char error_buf[64];
162:     apr_file_printf(astderr, 
163:       "ERROR: %s\n", apr_strerror(rv, error_buf, sizeof(error_buf)));
164: 
165:     apr_file_printf(astderr, "failed!\n");
166:     return 1; /* 異常終了 */
167: 
168:   }
169: 
170:   apr_file_printf(astdout, "\ndone.\n");
171: 
172:   return 0; /* 正常終了 */
173:   
174: 
175: } /* end of main */
176: 
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)