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