NCDBProcessOpts.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @file NCDBProcessOpts.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stddef.h>
  30. #include <misc/balloc.h>
  31. #include <ncd/extra/value_utils.h>
  32. #include "NCDBProcessOpts.h"
  33. int NCDBProcessOpts_Init (NCDBProcessOpts *o, NCDValRef opts_arg, NCDBProcessOpts_func_unknown func_unknown, void *func_unknown_user, NCDModuleInst *i, int blog_channel)
  34. {
  35. return NCDBProcessOpts_Init2(o, opts_arg, func_unknown, func_unknown_user, i, blog_channel, NULL, NULL);
  36. }
  37. int NCDBProcessOpts_Init2 (NCDBProcessOpts *o, NCDValRef opts_arg, NCDBProcessOpts_func_unknown func_unknown, void *func_unknown_user, NCDModuleInst *i, int blog_channel,
  38. int *out_keep_stdout, int *out_keep_stderr)
  39. {
  40. if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsMap(opts_arg)) {
  41. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "options must be a map");
  42. goto fail0;
  43. }
  44. o->username = NULL;
  45. o->do_setsid = 0;
  46. int keep_stdout = 0;
  47. int keep_stderr = 0;
  48. if (!NCDVal_IsInvalid(opts_arg)) {
  49. for (NCDValMapElem me = NCDVal_MapFirst(opts_arg); !NCDVal_MapElemInvalid(me); me = NCDVal_MapNext(opts_arg, me)) {
  50. NCDValRef key = NCDVal_MapElemKey(opts_arg, me);
  51. NCDValRef val = NCDVal_MapElemVal(opts_arg, me);
  52. if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stdout")) {
  53. if (!ncd_read_boolean(val, &keep_stdout)) {
  54. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad keep_stdout");
  55. goto fail1;
  56. }
  57. }
  58. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stderr")) {
  59. if (!ncd_read_boolean(val, &keep_stderr)) {
  60. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad keep_stderr");
  61. goto fail1;
  62. }
  63. }
  64. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "do_setsid")) {
  65. if (!ncd_read_boolean(val, &o->do_setsid)) {
  66. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad do_setsid");
  67. goto fail1;
  68. }
  69. }
  70. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "username")) {
  71. if (!NCDVal_IsStringNoNulls(val)) {
  72. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "username must be a string without nulls");
  73. goto fail1;
  74. }
  75. b_cstring cstr = NCDVal_StringCstring(val);
  76. o->username = b_cstring_strdup(cstr, 0, cstr.length);
  77. if (!o->username) {
  78. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "b_cstring_strdup failed");
  79. goto fail1;
  80. }
  81. }
  82. else {
  83. if (!func_unknown || !func_unknown(func_unknown_user, key, val)) {
  84. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "unknown option");
  85. goto fail1;
  86. }
  87. }
  88. }
  89. }
  90. o->nfds = 0;
  91. if (keep_stdout) {
  92. o->fds[o->nfds] = 1;
  93. o->fds_map[o->nfds++] = 1;
  94. }
  95. if (keep_stderr) {
  96. o->fds[o->nfds] = 2;
  97. o->fds_map[o->nfds++] = 2;
  98. }
  99. o->fds[o->nfds] = -1;
  100. if (out_keep_stdout) {
  101. *out_keep_stdout = keep_stdout;
  102. }
  103. if (out_keep_stderr) {
  104. *out_keep_stderr = keep_stderr;
  105. }
  106. return 1;
  107. fail1:
  108. if (o->username) {
  109. BFree(o->username);
  110. }
  111. fail0:
  112. return 0;
  113. }
  114. void NCDBProcessOpts_InitOld (NCDBProcessOpts *o, int keep_stdout, int keep_stderr, int do_setsid)
  115. {
  116. o->username = NULL;
  117. o->do_setsid = do_setsid;
  118. o->nfds = 0;
  119. if (keep_stdout) {
  120. o->fds[o->nfds] = 1;
  121. o->fds_map[o->nfds++] = 1;
  122. }
  123. if (keep_stderr) {
  124. o->fds[o->nfds] = 2;
  125. o->fds_map[o->nfds++] = 2;
  126. }
  127. o->fds[o->nfds] = -1;
  128. }
  129. void NCDBProcessOpts_Free (NCDBProcessOpts *o)
  130. {
  131. if (o->username) {
  132. BFree(o->username);
  133. }
  134. }
  135. struct BProcess_params NCDBProcessOpts_GetParams (NCDBProcessOpts *o)
  136. {
  137. struct BProcess_params params;
  138. params.username = o->username;
  139. params.fds = o->fds;
  140. params.fds_map = o->fds_map;
  141. params.do_setsid = o->do_setsid;
  142. return params;
  143. }