implement proc_end event
Stefan Schuermans

Stefan Schuermans commited on 2020-05-17 17:11:52
Showing 7 changed files, with 87 additions and 26 deletions.

... ...
@@ -2,6 +2,9 @@ add_library(
2 2
   lwptpl
3 3
   SHARED
4 4
   src/constructor.c
5
+  src/destructor.c
6
+  src/write.c
7
+  src/write.h
5 8
 )
6 9
 
7 10
 target_link_libraries(lwptpl lwpttr)
... ...
@@ -0,0 +1,12 @@
1
+#include <liblwpttr/proc_end.h>
2
+
3
+#include "write.h"
4
+
5
+#include <stdlib.h>
6
+
7
+__attribute__((destructor)) static void destructor(void) {
8
+  void *data = NULL;
9
+  size_t size = 0;
10
+  lwpttr_proc_end(&data, &size);
11
+  lwptpl_write(data, size);
12
+}
... ...
@@ -1,6 +1,28 @@
1 1
 #include "write.h"
2 2
 
3
+#include <fcntl.h>
3 4
 #include <stdlib.h>
5
+#include <string.h>
6
+#include <sys/file.h>
7
+#include <unistd.h>
4 8
 
5 9
 void lwptpl_write(void const *data, size_t size) {
10
+  if (! data || ! size) {
11
+    return;
12
+  }
13
+  char const *filename = getenv("LWPTPL_OUTPUT");
14
+  if (! filename) {
15
+    return;
16
+  }
17
+  int fd = open(filename, O_WRONLY | O_APPEND);
18
+  if (fd == -1) {
19
+    return;
20
+  }
21
+  if (flock(fd, LOCK_EX) == -1) {
22
+    close(fd);
23
+    return;
24
+  }
25
+  write(fd, data, size);
26
+  flock(fd, LOCK_UN);
27
+  close(fd);
6 28
 }
... ...
@@ -1,33 +1,8 @@
1 1
 #ifndef LWPTPL_WRITE_H
2 2
 #define LWPTPL_WRITE_H
3 3
 
4
-#include "write.h"
5
-
6
-#include <fcntl.h>
7 4
 #include <stdlib.h>
8
-#include <string.h>
9
-#include <sys/file.h>
10
-#include <unistd.h>
11 5
 
12
-void lwptpl_write(void const *data, size_t size) {
13
-  if (! data || ! size) {
14
-    return;
15
-  }
16
-  char const *filename = getenv("LWPTPL_OUTPUT");
17
-  if (! filename) {
18
-    return;
19
-  }
20
-  int fd = open(filename, O_WRONLY | O_APPEND);
21
-  if (fd == -1) {
22
-    return;
23
-  }
24
-  if (flock(fd, LOCK_EX) == -1) {
25
-    close(fd);
26
-    return;
27
-  }
28
-  write(fd, data, size);
29
-  flock(fd, LOCK_UN);
30
-  close(fd);
31
-}
6
+void lwptpl_write(void const *data, size_t size);
32 7
 
33 8
 #endif /* #ifndef LWPTPL_WRITE_H */
... ...
@@ -14,12 +14,14 @@ add_library(
14 14
   lwpttr
15 15
   STATIC
16 16
   include/liblwpttr/proc_begin.h
17
+  include/liblwpttr/proc_end.h
17 18
   src/cleaner.c
18 19
   src/cleaner.h
19 20
   src/event.c
20 21
   src/event.h
21 22
   src/macros.h
22 23
   src/proc_begin.c
24
+  src/proc_end.c
23 25
   src/stringlist.c
24 26
   src/stringlist.h
25 27
   src/symlink.c
... ...
@@ -0,0 +1,15 @@
1
+#ifndef LWPTTR_PROC_END_H
2
+#define LWPTTR_PROC_END_H
3
+
4
+#include <stdlib.h>
5
+
6
+/**
7
+ * @brief make a process end event
8
+ * @param[out] *data pointer to event data (malloc-ed)
9
+ * @param[out] *size size of data
10
+ * @return 0 on success (*data, *size set),
11
+ *         -1 on error (*data = NULL, *size = 0)
12
+ */
13
+int lwpttr_proc_end(void **data, size_t *size);
14
+
15
+#endif /* #ifndef LWPTTR_PROC_END_H */
... ...
@@ -0,0 +1,32 @@
1
+#include <liblwpttr/proc_end.h>
2
+#include "cleaner.h"
3
+#include "event.h"
4
+#include "timestamp.h"
5
+
6
+#include <lwproctrace.pb-c.h>
7
+
8
+#include <stdlib.h>
9
+#include <sys/types.h>
10
+#include <unistd.h>
11
+
12
+int lwpttr_proc_end(void **data, size_t *size) {
13
+  *data = NULL;
14
+  *size = 0;
15
+
16
+  lwpttr_cleaner_t *cleaner = lwpttr_cleaner_new();
17
+  if (! cleaner) {
18
+    return -1;
19
+  }
20
+
21
+  struct _Lwproctrace__Timespec timestamp = LWPROCTRACE__TIMESPEC__INIT;
22
+  lwpttr_event_get_timestamp(&timestamp);
23
+
24
+  struct _Lwproctrace__ProcEnd proc_end = LWPROCTRACE__PROC_END__INIT;
25
+  proc_end.pid = getpid();
26
+
27
+  struct _Lwproctrace__Event event = LWPROCTRACE__EVENT__INIT;
28
+  event.timestamp = &timestamp;
29
+  event.proc_end = &proc_end;
30
+
31
+  return lwpttr_event_pack(&event, data, size, cleaner);
32
+}
0 33