better handling of whole files
[tridge/junkcode.git] / config-pidtest.c
1
2 /* Linux-HA: pid test code
3
4 * Author: Jia Ming Pan <jmltc@cn.ibm.com>
5 * Modified by Guochun Shi <gshi@ncsa.uiuc.edu>
6 *
7 * Copyright (c) 2005 International Business Machines
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 *
23 */
24
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <pthread.h>
28 #include <stdio.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <stdlib.h>
32
33 #define SAME 1
34 #define TRUE 1
35 #define FALSE 0
36 int childflag = 0;
37 int grandchildflag = 0;
38 int   pidconsistent = TRUE;
39 void *
40 grandchild_func(void * data)
41 {
42         pid_t pid = (long) data;
43
44         if (pid ==  getpid()){
45                 grandchildflag = SAME;
46         }
47
48         if (grandchildflag ^ childflag){
49                 pidconsistent = FALSE;
50                 printf("Inconsistency detected\n");
51         }
52         return NULL;
53 }
54
55 void *
56 child_func(void * data)
57 {
58         pid_t pid = (long) data;
59         pthread_t thread_id;
60
61         if (pid ==  getpid()){
62                 childflag = SAME;
63         }
64
65         pthread_create(&thread_id, NULL, grandchild_func, (void*)(long)getpid());
66 }
67
68 int
69 main()
70 {
71         pthread_t thread_id;
72         pthread_attr_t tattr;
73         int  firsttime = 1;
74         pid_t   pid;
75         int     status;
76         
77         pthread_attr_init(&tattr);
78         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
79
80 again:
81         pid = fork();
82         if ( pid == 0 ) { 
83                 childflag = 0; 
84                 grandchildflag =0;
85                 if (pthread_create(&thread_id, &tattr, child_func, (void*)(long)getpid()) != 0){
86                         printf("%s: creating thread failed", __FUNCTION__);
87                 }
88                 usleep(500000);
89                 if (firsttime){
90                         firsttime=0;
91                         goto again;
92                 }
93                 if (pidconsistent){
94                         return 0;
95                 }else{
96                         return 1;
97                 }
98         }
99         if (waitpid(pid, &status, 0) <= 0){
100                 printf("ERROR: wait for child %d failed\n",pid);
101         }
102         if (WIFEXITED(status)){
103                 return (WEXITSTATUS(status));
104         }else{
105                 printf("child process %d does not exit normally\n",pid);
106         }
107         
108         return 0;
109 }
110