Choco OS  V.0.16.9.0
Join to the chocolate world
oc_process.c
Go to the documentation of this file.
1 
27 #include <oc_process.h>
28 #include <oc_memman.h>
29 #include <oc_user.h>
30 #include <oc_object.h>
31 #include <oc_list.h>
32 #include <oc_system_cfg.h>
33 #include <oc_threadman.h>
34 #include <oc_intman.h>
35 #include <oc_stdtypes.h>
36 #include <oc_system.h>
37 #include <oc_ktime.h>
38 #include <oc_processman.h>
39 #include <string.h>
40 #include <oc_vfs.h>
41 #include <oc_mutex.h>
42 #include <oc_udp.h>
43 #include <oc_icmp.h>
44 #include <oc_tcp.h>
45 #include <oc_exchan.h>
46 
52 #define _________________________________________TYPES_SECTION______________________________________________________________________________
53 
54 struct Process_t
55 {
56  oC_ObjectControl_t ObjectControl;
57  oC_Process_Priority_t Priority;
58  const char * Name;
59  oC_User_t User;
60  oC_Stream_t InputStream;
61  oC_Stream_t OutputStream;
62  oC_Stream_t ErrorStream;
63  oC_Allocator_t Allocator;
64  bool Killed;
65  oC_HeapMap_t HeapMap;
66  oC_IoFlags_t IoFlags;
67  char * Pwd;
68  oC_UInt_t Pid;
69  void * StdioBuffer;
70  oC_Mutex_t StdioMutex;
71 };
72 
73 #undef _________________________________________TYPES_SECTION______________________________________________________________________________
74 
80 #define _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
81 
82 static void MemoryFaultHandler( void * Address , MemoryEventFlags_t Event , const char * Function, uint32_t LineNumber );
83 
84 #undef _________________________________________PROTOTYPES_SECTION_________________________________________________________________________
85 
86 
92 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
93 
94 static const oC_Allocator_t Allocator = {
95  .Name = "process" ,
96  .EventHandler = MemoryFaultHandler ,
97  .EventFlags = MemoryEventFlags_BufferOverflow |
98  MemoryEventFlags_DataSectionOverflow |
99  MemoryEventFlags_MemoryExhausted |
100  MemoryEventFlags_PanicMemoryExhausted |
101  MemoryEventFlags_PossibleMemoryLeak
102 };
103 
104 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
105 
106 
112 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
113 
114 //==========================================================================================================================================
115 //==========================================================================================================================================
116 oC_Process_t oC_Process_New( oC_Process_Priority_t Priority , const char * Name , oC_User_t User , oC_UInt_t HeapMapSize, oC_Stream_t InputStream , oC_Stream_t OutputStream , oC_Stream_t ErrorStream )
117 {
118  oC_Process_t process = NULL;
119 
120  if(
122  oC_User_IsCorrect(User)
123  )
124  {
125  if(InputStream != NULL && !oC_Stream_IsType(InputStream , oC_Stream_Type_Input))
126  {
127  oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
128  }
129  if(OutputStream != NULL && !oC_Stream_IsType(OutputStream , oC_Stream_Type_Output))
130  {
131  oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
132  }
133  if(ErrorStream != NULL && !oC_Stream_IsType(ErrorStream , oC_Stream_Type_Output))
134  {
135  oC_SaveError(Name,oC_ErrorCode_StreamTypeNotCorrect);
136  }
137 
138  process = kmalloc(sizeof(struct Process_t) , &Allocator , AllocationFlags_CanWait1Second);
139 
140  if(process)
141  {
142  oC_Process_t currentProcess = oC_ProcessMan_GetCurrentProcess();
143  process->ObjectControl = oC_CountObjectControl(process,oC_ObjectId_Process);
144  process->ErrorStream = ErrorStream;
145  process->InputStream = InputStream;
146  process->Name = Name;
147  process->OutputStream = OutputStream;
148  process->Priority = Priority;
149  process->User = User;
150  process->Allocator.Name = Name;
151  process->Allocator.EventHandler = MemoryFaultHandler;
152  process->Allocator.EventFlags = MemoryEventFlags_AllErrors;
153  process->Killed = false;
154  process->IoFlags = oC_IoFlags_Default;
155  process->HeapMap = oC_MemMan_AllocateHeapMap(HeapMapSize,&process->Allocator,oC_FUNCTION,__LINE__,AllocationFlags_CanWait1Second);
156  process->Pwd = oC_Process_GetPwd(currentProcess);
157  process->Pid = oC_ProcessMan_GetNextPid();
158  process->StdioBuffer = kmalloc(CFG_BYTES_STDIO_BUFFER_SIZE * sizeof(char) , &process->Allocator , AllocationFlags_CanWait1Second);
159  process->StdioMutex = oC_Mutex_New(oC_Mutex_Type_Normal,&process->Allocator,AllocationFlags_CanWait1Second);
160  }
161  }
162 
163  return process;
164 }
165 
166 //==========================================================================================================================================
167 //==========================================================================================================================================
168 bool oC_Process_Delete( oC_Process_t * Process )
169 {
170  bool deleted = false;
171 
172  if(oC_Process_IsCorrect(*Process) && oC_Process_Kill(*Process))
173  {
174  (*Process)->ObjectControl = 0;
175  deleted = true;
176 
177  oC_ErrorCode_t errorCode = oC_VirtualFileSystem_fcloseFromProcess(*Process);
178 
179  if(oC_ErrorOccur(errorCode))
180  {
181  oC_SaveError("Process delete - VFS error" , errorCode );
182  deleted = false;
183  }
184 
185  errorCode = oC_Udp_ReleaseAllPortsReservedBy(*Process,s(2));
186 
187  if(oC_ErrorOccur(errorCode))
188  {
189  oC_SaveError("Process delete - UDP error - " , errorCode );
190  deleted = false;
191  }
192 
193  errorCode = oC_Icmp_ReleaseAllTypesReservedBy(*Process,s(2));
194 
195  if(oC_ErrorOccur(errorCode))
196  {
197  oC_SaveError("Process delete - ICMP error - " , errorCode );
198  deleted = false;
199  }
200 
201  errorCode = oC_Tcp_CloseProcess(*Process,s(2));
202 
203  if(oC_ErrorOccur(errorCode))
204  {
205  oC_SaveError("Process delete - TCP error - " , errorCode );
206  deleted = false;
207  }
208 
209  if(!oC_Mutex_Delete(&((*Process)->StdioMutex),AllocationFlags_CanWait1Second))
210  {
211  oC_SaveError("Process: Cannot delete StdioMutex",oC_ErrorCode_CannotDeleteObject);
212  deleted = false;
213  }
214 
215  if(oC_MemMan_FreeAllMemoryOfAllocator(&(*Process)->Allocator)==false)
216  {
217  oC_SaveError("Process delete - Error when releasing memory of allocator: " , oC_ErrorCode_ReleaseError);
218  deleted = false;
219  }
220 
221  if(kfree(*Process,AllocationFlags_CanWaitForever)==false)
222  {
223  oC_SaveError("Process delete - cannot release process memory" , oC_ErrorCode_ReleaseError);
224  deleted = false;
225  }
226  else
227  {
228  *Process = NULL;
229  }
230 
231  }
232 
233  return deleted;
234 }
235 
236 //==========================================================================================================================================
237 //==========================================================================================================================================
238 bool oC_Process_IsCorrect( oC_Process_t Process )
239 {
240  return isram(Process) && oC_CheckObjectControl(Process,oC_ObjectId_Process,Process->ObjectControl);
241 }
242 
243 //==========================================================================================================================================
244 //==========================================================================================================================================
245 bool oC_Process_IsKilled( oC_Process_t Process )
246 {
247  return oC_Process_IsCorrect(Process) && Process->Killed;
248 }
249 
250 //==========================================================================================================================================
251 //==========================================================================================================================================
252 bool oC_Process_ContainsThread( oC_Process_t Process , oC_Thread_t Thread )
253 {
254  bool contains = false;
255 
256  if(oC_Process_IsCorrect(Process) && oC_Thread_IsCorrect(Thread))
257  {
258  contains = oC_MemMan_GetAllocatorOfAddress(Thread) == &Process->Allocator;
259  }
260 
261  return contains;
262 }
263 
264 //==========================================================================================================================================
265 //==========================================================================================================================================
266 const char * oC_Process_GetName( oC_Process_t Process )
267 {
268  const char * name = "unknown";
269 
270  if(oC_Process_IsCorrect(Process))
271  {
272  name = Process->Name;
273  }
274 
275  return name;
276 }
277 
278 //==========================================================================================================================================
279 //==========================================================================================================================================
280 oC_Process_State_t oC_Process_GetState( oC_Process_t Process )
281 {
282  oC_Process_State_t state = oC_Process_State_Invalid;
283 
284  if(oC_Process_IsCorrect(Process))
285  {
286  oC_IntMan_EnterCriticalSection();
287  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
288 
289  if(threads)
290  {
291  if(Process->Killed)
292  {
293  state = oC_Process_State_Killed;
294  }
295  else
296  {
297  state = oC_Process_State_Initialized;
298  }
299 
300  uint32_t numberOfthreads = 0;
301 
302  oC_List_Foreach(threads,thread)
303  {
304  if(oC_Process_ContainsThread(Process,thread))
305  {
306  numberOfthreads++;
307 
308  if(Process->Killed)
309  {
310  state = oC_Process_State_Zombie;
311  }
312  else if(oC_Thread_IsBlocked(thread))
313  {
314  state = oC_Process_State_Asleep;
315  }
316  else
317  {
318  state = oC_Process_State_Run;
319  break;
320  }
321  }
322  }
323 
324  if(numberOfthreads == 0)
325  {
326  state = oC_Process_State_Zombie;
327  }
328  }
329  oC_IntMan_ExitCriticalSection();
330  }
331 
332  return state;
333 }
334 
335 //==========================================================================================================================================
336 //==========================================================================================================================================
337 oC_User_t oC_Process_GetUser( oC_Process_t Process )
338 {
339  oC_User_t user = NULL;
340 
341  if(oC_Process_IsCorrect(Process))
342  {
343  user = Process->User;
344  }
345 
346  return user;
347 }
348 
349 //==========================================================================================================================================
350 //==========================================================================================================================================
351 oC_Stream_t oC_Process_GetInputStream( oC_Process_t Process )
352 {
353  oC_Stream_t stream = NULL;
354 
355  if(oC_Process_IsCorrect(Process))
356  {
357  stream = Process->InputStream;
358  }
359 
360  return stream;
361 }
362 
363 //==========================================================================================================================================
364 //==========================================================================================================================================
365 oC_Stream_t oC_Process_GetOutputStream( oC_Process_t Process )
366 {
367  oC_Stream_t stream = NULL;
368 
369  if(oC_Process_IsCorrect(Process))
370  {
371  stream = Process->OutputStream;
372  }
373 
374  return stream;
375 }
376 
377 //==========================================================================================================================================
378 //==========================================================================================================================================
379 oC_Stream_t oC_Process_GetErrorStream( oC_Process_t Process )
380 {
381  oC_Stream_t stream = NULL;
382 
383  if(oC_Process_IsCorrect(Process))
384  {
385  stream = Process->ErrorStream;
386  }
387 
388  return stream;
389 }
390 
391 //==========================================================================================================================================
392 //==========================================================================================================================================
393 oC_ErrorCode_t oC_Process_SetInputStream( oC_Process_t Process , oC_Stream_t Stream )
394 {
395  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
396 
397  if(
398  oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
399  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
400  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Input) , oC_ErrorCode_StreamTypeNotCorrect)
401  )
402  {
403  errorCode = oC_ErrorCode_None;
404  Process->InputStream = Stream;
405  }
406 
407  return errorCode;
408 }
409 
410 //==========================================================================================================================================
411 //==========================================================================================================================================
412 oC_ErrorCode_t oC_Process_SetOutputStream( oC_Process_t Process , oC_Stream_t Stream )
413 {
414  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
415 
416  if(
417  oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
418  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
419  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Output) , oC_ErrorCode_StreamTypeNotCorrect)
420  )
421  {
422  errorCode = oC_ErrorCode_None;
423  Process->OutputStream= Stream;
424  }
425 
426  return errorCode;
427 }
428 
429 //==========================================================================================================================================
430 //==========================================================================================================================================
431 oC_ErrorCode_t oC_Process_SetErrorStream( oC_Process_t Process , oC_Stream_t Stream )
432 {
433  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
434 
435  if(
436  oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
437  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsCorrect(Stream) , oC_ErrorCode_StreamNotCorrect) &&
438  oC_AssignErrorCodeIfFalse(&errorCode , oC_Stream_IsType(Stream,oC_Stream_Type_Output) , oC_ErrorCode_StreamTypeNotCorrect)
439  )
440  {
441  errorCode = oC_ErrorCode_None;
442  Process->ErrorStream = Stream;
443  }
444 
445  return errorCode;
446 }
447 
448 //==========================================================================================================================================
449 //==========================================================================================================================================
450 oC_ErrorCode_t oC_Process_SetPriority( oC_Process_t Process , oC_Process_Priority_t Priority )
451 {
452  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
453 
454  if(ErrorCondition( oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect ))
455  {
456  Process->Priority = Priority;
457  errorCode = oC_ErrorCode_None;
458  }
459 
460  return errorCode;
461 }
462 
463 //==========================================================================================================================================
464 //==========================================================================================================================================
465 oC_Process_Priority_t oC_Process_GetPriority( oC_Process_t Process )
466 {
467  return oC_Process_IsCorrect(Process) ? Process->Priority : 0;
468 }
469 
470 //==========================================================================================================================================
471 //==========================================================================================================================================
472 oC_ErrorCode_t oC_Process_LockStdioBuffer( oC_Process_t Process , char ** outStdioBuffer , oC_Time_t Timeout )
473 {
474  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
475 
476  if(
477  ErrorCondition(oC_Process_IsCorrect(Process) ,oC_ErrorCode_ProcessNotCorrect) &&
478  ErrorCondition(oC_MemMan_IsRamAddress(outStdioBuffer) ,oC_ErrorCode_OutputAddressNotInRAM)
479  )
480  {
481  if(oC_Mutex_Take(Process->StdioMutex,Timeout))
482  {
483  *outStdioBuffer = Process->StdioBuffer;
484  errorCode = oC_ErrorCode_None;
485 
486  memset(Process->StdioBuffer,0,CFG_BYTES_STDIO_BUFFER_SIZE * sizeof(char));
487  }
488  else
489  {
490  errorCode = oC_ErrorCode_TimeoutError;
491  }
492  }
493 
494  return errorCode;
495 }
496 
497 //==========================================================================================================================================
498 //==========================================================================================================================================
499 oC_ErrorCode_t oC_Process_UnlockStdioBuffer( oC_Process_t Process )
500 {
501  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
502 
503  if(ErrorCondition(oC_Process_IsCorrect(Process) ,oC_ErrorCode_ProcessNotCorrect))
504  {
505  if(oC_Mutex_Give(Process->StdioMutex))
506  {
507  errorCode = oC_ErrorCode_None;
508  }
509  else
510  {
511  errorCode = oC_ErrorCode_ReleasingMutexError;
512  }
513  }
514 
515  return errorCode;
516 }
517 
518 //==========================================================================================================================================
519 //==========================================================================================================================================
520 bool oC_Process_IsActive( oC_Process_t Process )
521 {
522  bool active = false;
523 
524  if(oC_Process_IsCorrect(Process))
525  {
526  oC_IntMan_EnterCriticalSection();
527  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
528 
529  foreach(threads,thread)
530  {
531  if(oC_Process_ContainsThread(Process,thread))
532  {
533  active = true;
534  break;
535  }
536  }
537  oC_IntMan_ExitCriticalSection();
538  }
539 
540  return active;
541 }
542 
543 //==========================================================================================================================================
544 //==========================================================================================================================================
545 bool oC_Process_Kill( oC_Process_t Process )
546 {
547  bool allKilled = false;
548 
549  if(oC_Process_IsCorrect(Process))
550  {
551  oC_IntMan_EnterCriticalSection();
552  Process->Killed = true;
553  allKilled = true;
554 
555  oC_ProcessMan_ActivateDeleteDeamon();
556 
557  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
558 
559  if(threads)
560  {
561  oC_List_Foreach(threads,thread)
562  {
563  if(oC_Process_ContainsThread(Process,thread))
564  {
565  if(oC_Thread_Cancel(&thread)==false)
566  {
567  allKilled = false;
568  }
569  }
570  }
571 
572  oC_List_Foreach(threads,thread)
573  {
574  if(oC_Process_ContainsThread(Process,thread))
575  {
576  if(oC_Thread_IsCorrect(thread))
577  {
578  oC_List_RemoveAll(threads,thread);
579  }
580  }
581  }
582  }
583  oC_IntMan_ExitCriticalSection();
584 
585  }
586 
587  return allKilled;
588 }
589 
590 //==========================================================================================================================================
591 //==========================================================================================================================================
592 Allocator_t oC_Process_GetAllocator( oC_Process_t Process )
593 {
594  Allocator_t allocator = NULL;
595 
596  if(oC_Process_IsCorrect(Process))
597  {
598  allocator = &Process->Allocator;
599  }
600 
601  return allocator;
602 }
603 
604 //==========================================================================================================================================
605 //==========================================================================================================================================
606 oC_HeapMap_t oC_Process_GetHeapMap( oC_Process_t Process )
607 {
608  oC_HeapMap_t heapMap = NULL;
609 
610  if(oC_Process_IsCorrect(Process))
611  {
612  heapMap = Process->HeapMap;
613  }
614 
615  return heapMap;
616 }
617 
618 //==========================================================================================================================================
619 //==========================================================================================================================================
620 oC_ErrorCode_t oC_Process_Sleep( oC_Process_t Process , oC_Time_t Timeout )
621 {
622  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
623 
624  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ObjectNotCorrect))
625  {
626  errorCode = oC_ErrorCode_None;
627 
628  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
629 
630  if(threads)
631  {
632  oC_List_Foreach(threads,thread)
633  {
634  oC_IntMan_EnterCriticalSection();
635  if(oC_Process_ContainsThread(Process,thread))
636  {
637  if(oC_Thread_IsBlocked(thread) == false)
638  {
639  oC_Thread_Sleep(thread,Timeout);
640  }
641  }
642  oC_IntMan_ExitCriticalSection();
643  }
644  }
645 
646  }
647 
648  return errorCode;
649 }
650 
651 //==========================================================================================================================================
652 //==========================================================================================================================================
653 oC_UInt_t oC_Process_GetHeapMapSize( oC_Process_t Process )
654 {
655  oC_UInt_t size = 0;
656 
657  if(oC_Process_IsCorrect(Process))
658  {
659  size = oC_MemMan_GetHeapMapSize(Process->HeapMap);
660  }
661 
662  return size;
663 }
664 
665 //==========================================================================================================================================
666 //==========================================================================================================================================
667 oC_UInt_t oC_Process_GetFreeHeapMapSize(oC_Process_t Process )
668 {
669  oC_UInt_t size = 0;
670 
671  if(oC_Process_IsCorrect(Process))
672  {
673  size = oC_MemMan_GetFreeHeapMapSize(Process->HeapMap);
674  }
675 
676  return size;
677 }
678 
679 //==========================================================================================================================================
680 //==========================================================================================================================================
681 oC_UInt_t oC_Process_GetThreadsStackSize(oC_Process_t Process )
682 {
683  oC_UInt_t size = 0;
684 
685  if(oC_Process_IsCorrect(Process))
686  {
687  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
688 
689  if(threads)
690  {
691  oC_List_Foreach(threads,thread)
692  {
693  if(oC_Process_ContainsThread(Process,thread))
694  {
695  size += oC_Thread_GetStackSize(thread);
696  }
697  }
698  }
699  }
700 
701  return size;
702 }
703 
704 //==========================================================================================================================================
705 //==========================================================================================================================================
706 oC_Int_t oC_Process_GetFreeThreadsStackSize(oC_Process_t Process , bool Current )
707 {
708  oC_Int_t size = 0;
709 
710  if(oC_Process_IsCorrect(Process))
711  {
712  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
713 
714  if(threads)
715  {
716  oC_List_Foreach(threads,thread)
717  {
718  if(oC_Process_ContainsThread(Process,thread))
719  {
720  oC_Int_t threadFreeSize = oC_Thread_GetFreeStackSize(thread,Current);
721 
722  if(threadFreeSize > 0 && size >= 0)
723  {
724  size += threadFreeSize;
725  }
726  else if(threadFreeSize < size)
727  {
728  size = threadFreeSize;
729  }
730  }
731  }
732  }
733  }
734 
735  return size;
736 }
737 
738 //==========================================================================================================================================
739 //==========================================================================================================================================
740 oC_Time_t oC_Process_GetExecutionTime( oC_Process_t Process )
741 {
742  oC_Time_t executionTime = 0;
743 
744  if(oC_Process_IsCorrect(Process))
745  {
746  oC_List(oC_Thread_t) threads = oC_ThreadMan_GetList();
747 
748  if(threads)
749  {
750  oC_List_Foreach(threads,thread)
751  {
752  if(oC_Process_ContainsThread(Process,thread))
753  {
754  executionTime += oC_Thread_GetExecutionTime(thread);
755  }
756  }
757  }
758  }
759 
760  return executionTime;
761 }
762 
763 //==========================================================================================================================================
764 //==========================================================================================================================================
765 oC_UInt_t oC_Process_GetUsedRamSize( oC_Process_t Process )
766 {
767  oC_UInt_t size = 0;
768 
769  if(oC_Process_IsCorrect(Process))
770  {
771  size = oC_MemMan_GetMemoryOfAllocatorSize(&Process->Allocator) + sizeof(struct Process_t);
772  }
773 
774  return size;
775 }
776 
777 //==========================================================================================================================================
778 //==========================================================================================================================================
779 oC_IoFlags_t oC_Process_GetIoFlags( oC_Process_t Process )
780 {
781  oC_IoFlags_t flags = oC_IoFlags_Default;
782 
783  if(oC_Process_IsCorrect(Process))
784  {
785  flags = Process->IoFlags;
786  }
787 
788  return flags;
789 }
790 
791 //==========================================================================================================================================
792 //==========================================================================================================================================
793 oC_ErrorCode_t oC_Process_SetIoFlags( oC_Process_t Process , oC_IoFlags_t IoFlags )
794 {
795  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
796 
797  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process),oC_ErrorCode_ProcessNotCorrect))
798  {
799  Process->IoFlags = IoFlags;
800  errorCode = oC_ErrorCode_None;
801  }
802 
803  return errorCode;
804 }
805 
806 //==========================================================================================================================================
807 //==========================================================================================================================================
808 oC_ErrorCode_t oC_Process_WaitForFinish( oC_Process_t Process , oC_Time_t CheckPeriod , oC_Time_t Timeout )
809 {
810  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
811 
812  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process),oC_ErrorCode_ProcessNotCorrect))
813  {
814  errorCode = oC_ErrorCode_None;
815  oC_Process_State_t state = oC_Process_State_Run;
816  oC_Time_t timeoutTime = oC_KTime_GetTimestamp() + Timeout;
817  while( state == oC_Process_State_Run || state == oC_Process_State_Asleep || state == oC_Process_State_Initialized )
818  {
819  sleep(CheckPeriod);
820 
821  state = oC_Process_GetState(Process);
822 
823  if(oC_KTime_GetTimestamp() >= timeoutTime)
824  {
825  errorCode = oC_ErrorCode_Timeout;
826  break;
827  }
828  }
829 
830  }
831 
832  return errorCode;
833 }
834 
835 //==========================================================================================================================================
836 //==========================================================================================================================================
837 char * oC_Process_GetPwd( oC_Process_t Process )
838 {
839  char * pwd = "/";
840 
841  if(oC_Process_IsCorrect(Process))
842  {
843  pwd = Process->Pwd;
844  }
845 
846  return pwd;
847 }
848 
849 //==========================================================================================================================================
850 //==========================================================================================================================================
851 oC_ErrorCode_t oC_Process_SetPwd( oC_Process_t Process , const char * Pwd )
852 {
853  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
854 
855  if(
856  oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(Process) , oC_ErrorCode_ProcessNotCorrect) &&
857  oC_AssignErrorCodeIfFalse(&errorCode , isaddresscorrect(Pwd) , oC_ErrorCode_WrongAddress)
858  )
859  {
860  uint32_t requiredSize = 0;
861  char * newRelativePwd = NULL;
862 
863  errorCode = oC_VirtualFileSystem_ConvertRelativeToAbsolute(Pwd,NULL,&requiredSize,true);
864 
865  if(!oC_ErrorOccur(errorCode))
866  {
867  newRelativePwd = smartalloc(requiredSize,AllocationFlags_CanWait1Second);
868 
869  if(newRelativePwd)
870  {
871  errorCode = oC_VirtualFileSystem_ConvertRelativeToAbsolute(Pwd,newRelativePwd,&requiredSize,true);
872 
873  if(!oC_ErrorOccur(errorCode))
874  {
875  errorCode = oC_ErrorCode_None;
876 
877  if(oC_VirtualFileSystem_DirExists(newRelativePwd))
878  {
879  if(oC_MemMan_IsDynamicAllocatedAddress(Process->Pwd))
880  {
881  if(smartfree(Process->Pwd,strlen(Process->Pwd)+1,AllocationFlags_CanWaitForever)==false)
882  {
883  errorCode = oC_ErrorCode_ReleaseError;
884  }
885  }
886  Process->Pwd = newRelativePwd;
887  }
888  else
889  {
890  if(smartfree(newRelativePwd,requiredSize,AllocationFlags_CanWait1Second) == false)
891  {
892  oC_SaveError("Process: Set pwd, cannot release newRelativePwd: " , oC_ErrorCode_ReleaseError);
893  }
894 
895  errorCode = oC_ErrorCode_DirectoryNotExists;
896  }
897 
898  }
899  else
900  {
901  if(smartfree(newRelativePwd,requiredSize,AllocationFlags_CanWait1Second) == false)
902  {
903  oC_SaveError("Process: Set pwd, cannot release newRelativePwd: " , oC_ErrorCode_ReleaseError);
904  }
905  }
906  }
907  else
908  {
909  errorCode = oC_ErrorCode_AllocationError;
910  }
911  }
912  }
913 
914  return errorCode;
915 }
916 
917 //==========================================================================================================================================
918 //==========================================================================================================================================
919 oC_UInt_t oC_Process_GetPid( oC_Process_t Process )
920 {
921  oC_UInt_t pid = 0;
922 
923  if(oC_Process_IsCorrect(Process))
924  {
925  pid = Process->Pid;
926  }
927 
928  return pid;
929 }
930 
931 //==========================================================================================================================================
932 //==========================================================================================================================================
933 bool oC_Process_SetAllocationLimit( oC_Process_t Process , oC_MemorySize_t Limit )
934 {
935  bool success = false;
936 
937  if(oC_Process_IsCorrect(Process))
938  {
939  Process->Allocator.Limit = Limit;
940  success = true;
941  }
942 
943  return success;
944 }
945 
946 //==========================================================================================================================================
947 //==========================================================================================================================================
948 bool oC_Process_SetAllocationTracking( oC_Process_t Process, bool Enabled )
949 {
950  bool success = false;
951 
952  if(oC_Process_IsCorrect(Process))
953  {
954  if(Enabled)
955  {
956  Process->Allocator.EventFlags |= MemoryEventFlags_MemoryAllocated | MemoryEventFlags_MemoryReleased;
957  }
958  else
959  {
960  Process->Allocator.EventFlags &= ~(MemoryEventFlags_MemoryAllocated | MemoryEventFlags_MemoryReleased);
961  }
962  success = true;
963  }
964 
965  return success;
966 }
967 
968 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
969 
975 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
976 
977 static void MemoryFaultHandler( void * Address , MemoryEventFlags_t Event , const char * Function, uint32_t LineNumber )
978 {
979  char tempString[150] = {0};
980 
981  sprintf(tempString, "Error in allocation of address '%p' allocated in function %s at line %d (%d bytes)\n", Address, Function, LineNumber, oC_MemMan_GetSizeOfAllocation(Address));
982 
983  if(Event & MemoryEventFlags_MemoryFault ) oC_ExcHan_LogEvent( "Memory fault flag. Reason is unknown " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
984  if(Event & MemoryEventFlags_BusFault ) oC_ExcHan_LogEvent( "Bus fault flag. " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
985  if(Event & MemoryEventFlags_AllocationError ) oC_ExcHan_LogEvent( "Error while allocation of memory " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
986  if(Event & MemoryEventFlags_ReleaseError ) oC_ExcHan_LogEvent( "Error while releasing memory (address and allocation line number given in the event arguments) " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
987  if(Event & MemoryEventFlags_PossibleMemoryLeak ) oC_ExcHan_LogEvent( "There is a possibility of memory leak " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
988  if(Event & MemoryEventFlags_BufferOverflow ) oC_ExcHan_LogEvent( "Allocated memory at the given address achieved 'red-zone' " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
989 
990  if(Event & MemoryEventFlags_MemoryAllocated ) kdebuglog(oC_LogType_Track, "allocation - %s:%d (%d bytes)", Function,LineNumber, oC_MemMan_GetSizeOfAllocation(Address));
991  if(Event & MemoryEventFlags_MemoryReleased ) kdebuglog(oC_LogType_Track, "release - %s:%d (%d bytes)", Function,LineNumber, oC_MemMan_GetSizeOfAllocation(Address));
992  if(Event & MemoryEventFlags_RawMemoryAllocated ) kdebuglog(oC_LogType_Track, "allocation - %s:%d (%d bytes)", Function,LineNumber, oC_MemMan_GetSizeOfAllocation(Address));
993  if(Event & MemoryEventFlags_RawMemoryReleased ) kdebuglog(oC_LogType_Track, "release - %s:%d (%d bytes)", Function,LineNumber, oC_MemMan_GetSizeOfAllocation(Address));
994 
995  if(Event & MemoryEventFlags_ModuleTurningOff ) oC_ExcHan_LogEvent( "The MemMan module is turning off " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
996  if(Event & MemoryEventFlags_DataSectionOverflow ) oC_ExcHan_LogEvent( "Some buffer, that is in the data section was overwritten. " , tempString, NULL, NULL, oC_ExcHan_ExceptionType_MemoryAllocationError );
997 
998 }
999 
1000 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
FILE__DESCRIPTION
#define s(time)
Number of s.
Definition: oc_cfg.h:133
oC_ErrorCode_t oC_Udp_ReleaseAllPortsReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all ports reserved by the process
Definition: oc_udp.c:481
const char * Name
Definition: oc_stdlib.h:161
bool oC_MemMan_IsRamAddress(const void *Address)
checks if address is in ram section
Definition: oc_memman.c:1542
identifier for allocations
Definition: oc_stdlib.h:159
oC_MemorySize_t Limit
Definition: oc_stdlib.h:164
bool oC_MemMan_FreeAllMemoryOfAllocator(Allocator_t Allocator)
release all memory of allocator
Definition: oc_memman.c:985
The file with interface for process manager.
oC_UInt_t oC_MemMan_GetSizeOfAllocation(const void *Address)
returns size of allocation
Definition: oc_memman.c:1133
oC_UInt_t oC_MemMan_GetHeapMapSize(oC_HeapMap_t Map)
returns size of heap map
Definition: oc_memman.c:1482
MemoryEventHandler_t EventHandler
Definition: oc_stdlib.h:162
oC_UInt_t oC_MemMan_GetFreeHeapMapSize(oC_HeapMap_t Map)
returns size of free memory in heap map
Definition: oc_memman.c:1504
Definition: oc_user.c:43
The file with helper macros for managing objects.
bool oC_MemMan_IsDynamicAllocatedAddress(const void *Address)
checks if address is in dynamic allocated section
Definition: oc_memman.c:1552
bool oC_MemMan_IsAddressCorrect(const void *Address)
checks if address is correct - in RAM or in FLASH
Definition: oc_memman.c:1577
Allocator_t oC_MemMan_GetAllocatorOfAddress(const void *Address)
returns allocator of address
Definition: oc_memman.c:1098
uint32_t oC_ObjectControl_t
stores object control value
Definition: oc_object.h:141
oC_ErrorCode_t oC_Tcp_CloseProcess(oC_Process_t Process, oC_Time_t Timeout)
closes all objects related with the process
Definition: oc_tcp.c:261
The file with list library.
The file with interface for Virtual File System.
void oC_ExcHan_LogEvent(const char *Name, char *AdditionalInfo, void *Stack, oC_Thread_t Thread, oC_ExcHan_ExceptionType_t Type)
logs system event
Definition: oc_exchan.c:347
The file with interface for interrupt manager.
The file with interface for user system.
static oC_ObjectControl_t oC_CountObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId)
counts object control for object
Definition: oc_object.h:168
static bool oC_CheckObjectControl(void *ObjectPointer, oC_ObjectId_t ObjectId, oC_ObjectControl_t ObjectControl)
checks if object control is correct
Definition: oc_object.h:203
oC_UInt_t oC_MemMan_GetMemoryOfAllocatorSize(Allocator_t Allocator)
returns size of allocations per allocator
Definition: oc_memman.c:1439
The file with interface for mutex managing.
Stores UDP interface.
The file with memory manager interface.
The file with interface for process mechanism.
File with interface of Exception Handler.
File with interface for ICMP.
MemoryEventFlags_t EventFlags
Definition: oc_stdlib.h:163
oC_HeapMap_t oC_MemMan_AllocateHeapMap(oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
allocates memory for new heap map
Definition: oc_memman.c:543
The file with interface for Thread Manager.
oC_ErrorCode_t oC_Icmp_ReleaseAllTypesReservedBy(oC_Process_t Process, oC_Time_t Timeout)
releases all types reserved by the process
Definition: oc_icmp.c:414
FILE__DESCRIPTION
The file with interface of kernel time module.
#define NULL
pointer to a zero
Definition: oc_null.h:37