Skip to content

Commit f5710a4

Browse files
committed
libcomcom 0.1.1 support and wrapper for initializer/destructor
1 parent 73aa8b4 commit f5710a4

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

C/libcomcom.h

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern "C" {
3131
* Initialize the library. Call it before libcomcom_run_command().
3232
* Note that this erases the old SIGCHLD handler (if any).
3333
* @return 0 on success and -1 on error (also sets `errno`).
34+
*
35+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
3436
*/
3537
int libcomcom_init(void);
3638

@@ -41,6 +43,8 @@ int libcomcom_init(void);
4143
* The old signal handler (the one obtained from `old` variable) is also
4244
* called from our SIGCHLD handler.
4345
* @return 0 on success and -1 on error (also sets `errno`).
46+
*
47+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
4448
*/
4549
int libcomcom_init2(struct sigaction *old);
4650

@@ -53,6 +57,8 @@ int libcomcom_init2(struct sigaction *old);
5357
* (not by sigaction()), then this function may not work (leads to undefined
5458
* behavior) on some non-Unix systems.
5559
* @return 0 on success and -1 on error (also sets `errno`).
60+
*
61+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
5662
*/
5763
int libcomcom_init_stratum(void);
5864

@@ -64,7 +70,7 @@ int libcomcom_init_stratum(void);
6470
* @param output_len at this location is stored the length of command's stdout
6571
* @param file the command to run (PATH used)
6672
* @param argv arguments for the command to run
67-
* @param envp environment for the command to run (pass `environ` to duplicate our environment)
73+
* @param envp environment for the command to run (pass `NULL` to duplicate our environment)
6874
* @param timeout timeout in milliseconds, -1 means infinite timeout
6975
* @return 0 on success and -1 on error (also sets `errno`).
7076
*/
@@ -76,6 +82,7 @@ int libcomcom_run_command(const char *input, size_t input_len,
7682

7783
/**
7884
* Should be run for normal termination (not in SIGTERM/SIGINT handler)
85+
* of our program.
7986
* @return 0 on success and -1 on error (also sets `errno`).
8087
*/
8188
int libcomcom_destroy(void);
@@ -88,20 +95,41 @@ int libcomcom_terminate(void);
8895

8996
/**
9097
* Install SIGTERM and SIGINT handler which calls libcomcom_terminate().
91-
* If your program needs to handle SIGTERM or SIGINT in another way,
92-
* use libcomcom_terminate() instead.
9398
* @return 0 on success and -1 on error (also sets `errno`).
99+
*
100+
* You are recommended to use libcomcom_set_default_terminate2()
101+
* instead.
94102
*/
95103
int libcomcom_set_default_terminate(void);
96104

97105
/**
98106
* Uninstall SIGTERM and SIGINT handler which calls libcomcom_terminate().
99-
* If your program needs to handle SIGTERM or SIGINT in another way,
100-
* use libcomcom_terminate() instead.
101107
* @return 0 on success and -1 on error (also sets `errno`).
108+
*
109+
* You are recommended to use libcomcom_reset_default_terminate2()
110+
* instead.
102111
*/
103112
int libcomcom_reset_default_terminate(void);
104113

114+
/**
115+
* Install SIGTERM and SIGINT handler which calls libcomcom_terminate().
116+
* @return 0 on success and -1 on error (also sets `errno`).
117+
*
118+
* The installed signal handler also calls old signal handler automatically.
119+
*
120+
* WARNING: If before calling these handlers were set by signal()
121+
* (not by sigaction()), then this function may not work (leads to undefined
122+
* behavior) on some non-Unix systems.
123+
*/
124+
int libcomcom_set_default_terminate2(void);
125+
126+
/**
127+
* Resets to signal handlers which were before calling
128+
* libcomcom_set_default_terminate2().
129+
* @return 0 on success and -1 on error (also sets `errno`).
130+
*/
131+
int libcomcom_reset_default_terminate2(void);
132+
105133
#ifdef __cplusplus
106134
}
107135
#endif

deimos/libcomcom.d

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern extern(C):
2626
* Initialize the library. Call it before libcomcom_run_command().
2727
* Note that this erases the old SIGCHLD handler (if any).
2828
* @return 0 on success and -1 on error (also sets `errno`).
29+
*
30+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
2931
*/
3032
int libcomcom_init();
3133

@@ -36,6 +38,8 @@ int libcomcom_init();
3638
* The old signal handler (the one obtained from `old` variable) is also
3739
* called from our SIGCHLD handler.
3840
* @return 0 on success and -1 on error (also sets `errno`).
41+
*
42+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
3943
*/
4044
int libcomcom_init2(sigaction_t *old);
4145

@@ -48,6 +52,8 @@ int libcomcom_init2(sigaction_t *old);
4852
* (not by sigaction()), then this function may not work (leads to undefined
4953
* behavior) on some non-Unix systems.
5054
* @return 0 on success and -1 on error (also sets `errno`).
55+
*
56+
* You should usually also initialize SIGTERM/SIGINT signal handlers.
5157
*/
5258
int libcomcom_init_stratum();
5359

@@ -59,7 +65,7 @@ int libcomcom_init_stratum();
5965
* @param output_len at this location is stored the length of command's stdout
6066
* @param file the command to run (PATH used)
6167
* @param argv arguments for the command to run
62-
* @param envp environment for the command to run (pass `environ` to duplicate our environment)
68+
* @param envp environment for the command to run (pass `NULL` to duplicate our environment)
6369
* @param timeout timeout in milliseconds, -1 means infinite timeout
6470
* @return 0 on success and -1 on error (also sets `errno`).
6571
*/
@@ -71,6 +77,7 @@ int libcomcom_run_command(const char *input, size_t input_len,
7177

7278
/**
7379
* Should be run for normal termination (not in SIGTERM/SIGINT handler)
80+
* of our program.
7481
* @return 0 on success and -1 on error (also sets `errno`).
7582
*/
7683
int libcomcom_destroy();
@@ -83,17 +90,37 @@ int libcomcom_terminate();
8390

8491
/**
8592
* Install SIGTERM and SIGINT handler which calls libcomcom_terminate().
86-
* If your program needs to handle SIGTERM or SIGINT in another way,
87-
* use libcomcom_terminate() instead.
8893
* @return 0 on success and -1 on error (also sets `errno`).
94+
*
95+
* You are recommended to use libcomcom_set_default_terminate2()
96+
* instead.
8997
*/
9098
int libcomcom_set_default_terminate();
9199

92100
/**
93101
* Uninstall SIGTERM and SIGINT handler which calls libcomcom_terminate().
94-
* If your program needs to handle SIGTERM or SIGINT in another way,
95-
* use libcomcom_terminate() instead.
96102
* @return 0 on success and -1 on error (also sets `errno`).
103+
*
104+
* You are recommended to use libcomcom_reset_default_terminate2()
105+
* instead.
97106
*/
98107
int libcomcom_reset_default_terminate();
99108

109+
/**
110+
* Install SIGTERM and SIGINT handler which calls libcomcom_terminate().
111+
* @return 0 on success and -1 on error (also sets `errno`).
112+
*
113+
* The installed signal handler also calls old signal handler automatically.
114+
*
115+
* WARNING: If before calling these handlers were set by signal()
116+
* (not by sigaction()), then this function may not work (leads to undefined
117+
* behavior) on some non-Unix systems.
118+
*/
119+
int libcomcom_set_default_terminate2();
120+
121+
/**
122+
* Resets to signal handlers which were before calling
123+
* libcomcom_set_default_terminate2().
124+
* @return 0 on success and -1 on error (also sets `errno`).
125+
*/
126+
int libcomcom_reset_default_terminate2();

deimos/libcomcom_wrapper.d

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ import std.array : array;
2424
import std.exception : ErrnoException;
2525
import libcomcom;
2626

27+
void libComComInitializer()
28+
{
29+
int res;
30+
res = libcomcom_init_stratum();
31+
if (res != 0) {
32+
throw new ErrnoException("libcomcom_init_stratum()"); // TODO: Localization
33+
}
34+
libcomcom_set_default_terminate();
35+
if (res != 0) {
36+
throw new ErrnoException("libcomcom_set_default_terminate()"); // TODO: Localization
37+
}
38+
}
39+
40+
void libComComDestructor()
41+
{
42+
cast(void) libcomcom_reset_default_terminate();
43+
cast(void) libcomcom_destroy();
44+
}
45+
2746
string _runCommand(string file,
2847
string[] argv,
2948
const char** childEnvp,

dub.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@
3030
"name": "static-library",
3131
"targetType": "staticLibrary"
3232
}
33-
]
34-
33+
]
3534
}

0 commit comments

Comments
 (0)