0004-create-non-forking-softetherd-for-upstart-and-systemd.patch 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. From 64dd780905ae339a0a57e4aba541799016816a1a Mon Sep 17 00:00:00 2001
  2. From: Darik Horn <dajhorn@vanadac.com>
  3. Date: Fri, 3 Oct 2014 13:30:24 -0400
  4. Subject: [PATCH] Create a non-forking softetherd for upstart and systemd.
  5. Implement a daemon that expects to be invoked by a new-style init like upstart
  6. or systemd as:
  7. /usr/sbin/softetherd [vpnbridge|vpnclient|vpnserver]
  8. Alternatively, if the command line argument is empty, then use the
  9. `SOFTETHER_MODE` environment variable instead.
  10. Conflicts:
  11. src/bin/hamcore/strtable_en.stb
  12. Taken from Github at
  13. https://github.com/dajhorn/SoftEtherVPN/commit/64dd780905ae339a0a57e4aba541799016816a1a.
  14. Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
  15. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  16. ---
  17. configure.ac | 1 +
  18. src/Makefile.am | 3 ++
  19. src/bin/hamcore/strtable_en.stb | 1 +
  20. src/softetherd/Makefile.am | 28 ++++++++++
  21. src/softetherd/softetherd.c | 114 ++++++++++++++++++++++++++++++++++++++++
  22. 5 files changed, 147 insertions(+)
  23. create mode 100644 src/softetherd/Makefile.am
  24. create mode 100644 src/softetherd/softetherd.c
  25. Index: b/configure.ac
  26. ===================================================================
  27. --- a/configure.ac
  28. +++ b/configure.ac
  29. @@ -36,6 +36,7 @@
  30. src/vpnclient/Makefile
  31. src/vpnbridge/Makefile
  32. src/vpncmd/Makefile
  33. + src/softetherd/Makefile
  34. ])
  35. Index: b/src/Makefile.am
  36. ===================================================================
  37. --- a/src/Makefile.am
  38. +++ b/src/Makefile.am
  39. @@ -27,3 +27,6 @@
  40. # These are the final build products.
  41. SUBDIRS += vpnserver vpnclient vpnbridge vpncmd
  42. +
  43. +# This is a daemon for upstart and systemd.
  44. +SUBDIRS += softetherd
  45. Index: b/src/bin/hamcore/strtable_en.stb
  46. ===================================================================
  47. --- a/src/bin/hamcore/strtable_en.stb
  48. +++ b/src/bin/hamcore/strtable_en.stb
  49. @@ -1062,6 +1062,7 @@
  50. # Concerning services (UNIX)
  51. +UNIX_DAEMON_HELP SoftEther VPN non-forking daemon for upstart and systemd.\nCommand Usage:\n %S vpnbridge - Enable bridging features.\n %S vpnclient - Enable client features.\n %S vpnserver - Enable all features.\nThe parameter can be set in the SOFTETHER_MODE environment variable.\n\n
  52. UNIX_SVC_HELP %S service program\nCopyright (c) SoftEther VPN Project. All Rights Reserved.\n\n%S command usage:\n %S start - Start the %S service.\n %S stop - Stop the %S service if the service has been already started.\n\n
  53. UNIX_SVC_STARTED The %S service has been started.\n
  54. UNIX_SVC_STOPPING Stopping the %S service ...\n
  55. Index: b/src/softetherd/Makefile.am
  56. ===================================================================
  57. --- /dev/null
  58. +++ b/src/softetherd/Makefile.am
  59. @@ -0,0 +1,28 @@
  60. +# Copyright 2014 Darik Horn <dajhorn@vanadac.com>
  61. +#
  62. +# This file is part of SoftEther.
  63. +#
  64. +# SoftEther is free software: you can redistribute it and/or modify it under
  65. +# the terms of the GNU General Public License as published by the Free
  66. +# Software Foundation, either version 2 of the License, or (at your option)
  67. +# any later version.
  68. +#
  69. +# SoftEther is distributed in the hope that it will be useful, but WITHOUT ANY
  70. +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  71. +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  72. +# details.
  73. +#
  74. +# You should have received a copy of the GNU General Public License along with
  75. +# SoftEther. If not, see <http://www.gnu.org/licenses/>.
  76. +
  77. +
  78. +include $(top_srcdir)/autotools/softether.am
  79. +
  80. +sbin_PROGRAMS = \
  81. + softetherd
  82. +
  83. +softetherd_SOURCES = \
  84. + softetherd.c
  85. +
  86. +softetherd_LDADD = \
  87. + $(top_builddir)/src/libsoftether/libsoftether.la
  88. Index: b/src/softetherd/softetherd.c
  89. ===================================================================
  90. --- /dev/null
  91. +++ b/src/softetherd/softetherd.c
  92. @@ -0,0 +1,114 @@
  93. +// SoftEther VPN daemon for upstart and systemd.
  94. +//
  95. +// Copyright 2014 Darik Horn <dajhorn@vanadac.com>
  96. +//
  97. +// This file is part of SoftEther.
  98. +//
  99. +// SoftEther is free software: you can redistribute it and/or modify it under
  100. +// the terms of the GNU General Public License as published by the Free
  101. +// Software Foundation, either version 2 of the License, or (at your option)
  102. +// any later version.
  103. +//
  104. +// SoftEther is distributed in the hope that it will be useful, but WITHOUT ANY
  105. +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  106. +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  107. +// details.
  108. +//
  109. +// You should have received a copy of the GNU General Public License along with
  110. +// SoftEther. If not, see <http://www.gnu.org/licenses/>.
  111. +
  112. +
  113. +#include <GlobalConst.h>
  114. +
  115. +#define VPN_EXE
  116. +
  117. +#include <stdio.h>
  118. +#include <stdlib.h>
  119. +#include <string.h>
  120. +#include <wchar.h>
  121. +#include <stdarg.h>
  122. +#include <time.h>
  123. +#include <Mayaqua/Mayaqua.h>
  124. +#include <Cedar/Cedar.h>
  125. +
  126. +void DaemonUsage(char *name)
  127. +{
  128. + UniPrint(_UU("UNIX_DAEMON_HELP"), name, name, name);
  129. +}
  130. +
  131. +
  132. +void DaemonStartProcess()
  133. +{
  134. + // This environment variable is exported by upstart.
  135. + char *upstart_job = getenv("UPSTART_JOB");
  136. +
  137. + InitCedar();
  138. + StInit();
  139. + StStartServer(false);
  140. +
  141. + // Notify upstart that softetherd is ready.
  142. + if (upstart_job != NULL)
  143. + {
  144. + unsetenv("UPSTART_JOB");
  145. + raise(SIGSTOP);
  146. + }
  147. +}
  148. +
  149. +
  150. +void DaemonStopProcess()
  151. +{
  152. + StStopServer();
  153. + StFree();
  154. + FreeCedar();
  155. +}
  156. +
  157. +
  158. +int main(int argc, char *argv[])
  159. +{
  160. + // This environment variable is sourced and exported by the init process from /etc/default/softether.
  161. + char *softether_mode = getenv("SOFTETHER_MODE");
  162. +
  163. + InitMayaqua(false, false, argc, argv);
  164. +
  165. + // Check for an explicit invocation. (eg: "/usr/sbin/softetherd vpnserver")
  166. + if (argc >= 2)
  167. + {
  168. + if (StrCmpi(argv[1], "vpnbridge") == 0
  169. + || StrCmpi(argv[1], "vpnclient") == 0
  170. + || StrCmpi(argv[1], "vpnserver") == 0)
  171. + {
  172. + UnixExecService(argv[1], DaemonStartProcess, DaemonStopProcess);
  173. + FreeMayaqua();
  174. + return 0;
  175. + }
  176. +
  177. + // Exit status codes 150..199 are reserved for the application by the LSB.
  178. + fprintf(stderr, "Error: Unrecognized parameter: %s\n", argv[1]);
  179. + fflush(stderr);
  180. + FreeMayaqua();
  181. + return 150;
  182. + }
  183. +
  184. + // Alternatively, use the environment variable.
  185. + if (softether_mode != NULL)
  186. + {
  187. + if (StrCmpi(softether_mode, "vpnbridge") == 0
  188. + || StrCmpi(softether_mode, "vpnclient") == 0
  189. + || StrCmpi(softether_mode, "vpnserver") == 0)
  190. + {
  191. + UnixExecService(softether_mode, DaemonStartProcess, DaemonStopProcess);
  192. + FreeMayaqua();
  193. + return 0;
  194. + }
  195. +
  196. + // Exit status codes 150..199 are reserved for the application by the LSB.
  197. + fprintf(stderr, "Error: Unrecognized environment variable: SOFTETHER_MODE=%s\n", softether_mode);
  198. + fflush(stderr);
  199. + FreeMayaqua();
  200. + return 151;
  201. + }
  202. +
  203. + DaemonUsage(argv[0]);
  204. + FreeMayaqua();
  205. + return 3;
  206. +}