Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1655 | giacomo | 1 | /* |
2 | * |
||
3 | * |
||
4 | * |
||
5 | */ |
||
6 | |||
7 | #include <kernel/func.h> |
||
8 | #include <kernel/model.h> |
||
9 | |||
10 | #include <sys/types.h> |
||
11 | #include <sys/stat.h> |
||
12 | #include <sys/mount.h> |
||
13 | #include <dirent.h> |
||
14 | #include <fcntl.h> |
||
15 | #include <unistd.h> |
||
16 | #include <errno.h> |
||
17 | #include <string.h> |
||
18 | #include <stdlib.h> |
||
19 | #include <semaphore.h> |
||
20 | #include <stdio.h> |
||
21 | #include <time.h> |
||
22 | |||
23 | #include "common.h" |
||
24 | |||
25 | #include <trace/trace.h> |
||
26 | #include <trace/queues.h> |
||
27 | |||
28 | #define sem_signal sem_post |
||
29 | |||
30 | int noscroll=0; |
||
31 | SEM console; |
||
32 | |||
33 | #define MPROC ((50<(MAX_PROC-10))?50:MAX_PROC-10) |
||
34 | |||
35 | TASK viewdir(void *prof); |
||
36 | sem_t actmutex,actsync; |
||
37 | char *globpathname; |
||
38 | PID globpid; |
||
39 | int counter=0,actcounter=0; |
||
40 | |||
41 | void activate_task(int prof, char *pathname) |
||
42 | { |
||
43 | char tname[32]; |
||
44 | NRT_TASK_MODEL m; |
||
45 | PID pid; |
||
46 | |||
47 | REPEAT: |
||
48 | sem_wait(&actmutex); |
||
49 | |||
50 | if (actcounter>=MPROC) { |
||
51 | struct timespec delay; |
||
52 | delay.tv_sec = 0; |
||
53 | delay.tv_nsec = 10000000; |
||
54 | sem_signal(&actmutex); |
||
55 | nanosleep(&delay, NULL); |
||
56 | goto REPEAT; |
||
57 | } |
||
58 | |||
59 | globpathname=pathname; |
||
60 | counter++; |
||
61 | sprintf(tname,"tsk%i",counter); |
||
62 | |||
63 | nrt_task_default_model(m); |
||
64 | nrt_task_def_arg(m,(void*)counter); |
||
65 | |||
66 | globpid=pid=task_create(tname,viewdir,&m,NULL); |
||
67 | if (pid==-1) { |
||
68 | sem_wait(&console); |
||
69 | cprintf("can't create '%s'\n",tname); |
||
70 | perror("can't create task"); |
||
71 | sem_signal(&console); |
||
72 | sys_end(); |
||
73 | return; |
||
74 | } |
||
75 | task_activate(pid); |
||
76 | sem_wait(&actsync); |
||
77 | actcounter++; |
||
78 | |||
79 | sem_signal(&actmutex); |
||
80 | } |
||
81 | |||
82 | /* |
||
83 | * |
||
84 | */ |
||
85 | |||
86 | int filecounter=0; |
||
87 | |||
88 | TASK viewdir(void *pointer) |
||
89 | { |
||
90 | struct dirent *den; |
||
91 | struct stat st; |
||
92 | char *str; |
||
93 | DIR *d; |
||
94 | int res; |
||
95 | int x; |
||
96 | char pathname[1024]; |
||
97 | PID mypid; |
||
98 | int prof=(int)pointer; |
||
99 | |||
100 | strcpy(pathname,globpathname); |
||
101 | mypid=globpid; |
||
102 | sem_signal(&actsync); |
||
103 | |||
104 | str=pathname+(x=strlen(pathname)); |
||
105 | d=opendir(pathname); |
||
106 | |||
107 | if (d==NULL) { |
||
108 | sem_wait(&console); |
||
109 | cprintf("%03i ERR: can't open dir %s\n",prof,pathname); |
||
110 | cprintf("errno: %i '%s'\n",errno,strerror(errno)); |
||
111 | sem_signal(&console); |
||
112 | |||
113 | sys_end(); |
||
114 | l1_exit(0); |
||
115 | |||
116 | goto END; |
||
117 | } |
||
118 | |||
119 | while ((den=readdir(d))!=NULL) { |
||
120 | |||
121 | if (x==1&&*pathname=='/') |
||
122 | strcat(pathname,den->d_name); |
||
123 | else |
||
124 | strcat(strcat(pathname,"/"),den->d_name); |
||
125 | |||
126 | sem_wait(&console); |
||
127 | if (noscroll) { |
||
128 | place(0,10); |
||
129 | cprintf(" "); |
||
130 | place(0,10); |
||
131 | } |
||
132 | cprintf("t%03i %s\n",prof,pathname); |
||
133 | filecounter++; |
||
134 | sem_signal(&console); |
||
135 | |||
136 | if (!strcmp(den->d_name,".")) goto SKIP; |
||
137 | if (!strcmp(den->d_name,"..")) goto SKIP; |
||
138 | |||
139 | res=stat(pathname,&st); |
||
140 | if (res!=0) { |
||
141 | sem_wait(&console); |
||
142 | cprintf("t%03i can't stat %s\n",prof,pathname); |
||
143 | cprintf("errno: %i '%s'\n",errno,strerror(errno)); |
||
144 | sem_signal(&console); |
||
145 | |||
146 | sys_end(); |
||
147 | l1_exit(0); |
||
148 | |||
149 | closedir(d); |
||
150 | goto END; |
||
151 | } else { |
||
152 | if (S_ISDIR(st.st_mode)) { |
||
153 | sem_wait(&console); |
||
154 | sem_signal(&console); |
||
155 | activate_task(prof,pathname); |
||
156 | } |
||
157 | |||
158 | } |
||
159 | |||
160 | SKIP: |
||
161 | *str='\0'; |
||
162 | } |
||
163 | |||
164 | closedir(d); |
||
165 | |||
166 | END: |
||
167 | sem_wait(&actmutex); |
||
168 | actcounter--; |
||
169 | sem_signal(&actmutex); |
||
170 | |||
171 | return 0; |
||
172 | } |
||
173 | |||
174 | /* |
||
175 | * |
||
176 | */ |
||
177 | |||
178 | int __register_sub_init_prologue(void) |
||
179 | { |
||
180 | int q; |
||
181 | TRC_init_phase1(NULL); |
||
182 | trc_register_fixed_queue(); |
||
183 | q=trc_create_queue(TRC_FIXED_QUEUE,NULL); |
||
184 | trc_assign_class_to_queue(TRC_CLASS_SYSTEM,q); |
||
185 | trc_trace_class(TRC_CLASS_SYSTEM); |
||
186 | return 0; |
||
187 | } |
||
188 | |||
189 | /* |
||
190 | * |
||
191 | */ |
||
192 | |||
193 | int main(int argc,char *argv[]) |
||
194 | { |
||
195 | // int res; |
||
196 | |||
197 | showmessage("This test show all filenames of a directory of an hardisk\n" |
||
198 | "recursively using a soft task for every directory.\n" |
||
199 | "The tracer with a circular queue is activated.\n"); |
||
200 | |||
201 | sem_init(&console,0,1); |
||
202 | sem_init(&actmutex,0,1); |
||
203 | sem_init(&actsync,0,0); |
||
204 | |||
205 | activate_task(-1,FROMDIR); |
||
206 | |||
207 | { |
||
208 | struct timespec delay; |
||
209 | delay.tv_sec = 0; |
||
210 | delay.tv_nsec = 500000000; |
||
211 | for(;;) { |
||
212 | sem_wait(&actmutex); |
||
213 | if (actcounter==0) break; |
||
214 | sem_signal(&actmutex); |
||
215 | nanosleep(&delay, NULL); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | cprintf("\nfiles: %i\n",filecounter); |
||
220 | |||
221 | waitend(); |
||
222 | |||
223 | return 0; |
||
224 | } |