Project

General

Profile

Bug #781 » race-chown-name.c

Mathieu Desnoyers, 04/14/2014 03:14 PM

 
1
/*
2
 * Thread testing
3
 *
4
 * build with gcc -lpthread -o pthread pthread.c
5
 *
6
 * Mathieu Desnoyers
7
 * License: GPL
8
 */
9

    
10
#include <string.h>
11
#include <stdio.h>
12
#include <pthread.h>
13
#include <stdlib.h>
14
#include <sys/types.h>
15
#include <sys/wait.h>
16
#include <unistd.h>
17
#include <stdio.h>
18
#include <signal.h>
19

    
20
static int __thread test = 0;
21

    
22
static char teststr[1024];
23

    
24
/* signal handler */
25
void handler(int signo)
26
{
27
	printf("Sig handler : TID %lu, pid : %lu\n", pthread_self(), getpid());
28
}
29

    
30
void *thr1(void *arg)
31
{
32
	test = 1;
33
	
34
	printf("thread 1, thread id : %lu, pid %lu, test %d\n",
35
			pthread_self(), getpid(), test);
36
	while(1) {
37
		chown(teststr, 0, 0);
38
	}
39
	return ((void*)1);
40

    
41
}
42

    
43
void *thr2(void *arg)
44
{
45
	printf("thread 2, thread id : %lu, pid %lu, test %d\n",
46
			pthread_self(), getpid(), test);
47
	while(1) { 
48
		//memset(teststr, 0, 1024);
49
		strcpy(teststr, "testblah");
50
		strcpy(teststr, "abc");
51
		strcpy(teststr, "abcsdfasdfasdfasdf");
52
	}
53
	return ((void*)2);
54
}
55

    
56
int main()
57
{
58
	int err;
59
	pthread_t tid1, tid2;
60
	void *tret;
61
	static struct sigaction act;
62

    
63
	act.sa_handler = handler;
64
	sigemptyset(&(act.sa_mask));
65
	sigaddset(&(act.sa_mask), SIGUSR1);
66
	sigaction(SIGUSR1, &act, NULL);
67

    
68
	err = pthread_create(&tid1, NULL, thr1, NULL);
69
	if (err != 0)
70
		exit(1);
71

    
72
	err = pthread_create(&tid2, NULL, thr2, NULL);
73
	if (err != 0)
74
		exit(1);
75

    
76
	sleep(10);
77

    
78
	err = pthread_join(tid1, &tret);
79
	if (err != 0)
80
		exit(1);
81

    
82
	err = pthread_join(tid2, &tret);
83
	if (err != 0)
84
		exit(1);
85

    
86
	return 0;
87
}
(1-1/2)