NCDBProcessOpts.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. keep_stdout = ncd_read_boolean(val);
  54. }
  55. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stderr")) {
  56. keep_stderr = ncd_read_boolean(val);
  57. }
  58. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "do_setsid")) {
  59. o->do_setsid = ncd_read_boolean(val);
  60. }
  61. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "username")) {
  62. if (!NCDVal_IsStringNoNulls(val)) {
  63. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "username must be a string without nulls");
  64. goto fail1;
  65. }
  66. b_cstring cstr = NCDVal_StringCstring(val);
  67. o->username = b_cstring_strdup(cstr, 0, cstr.length);
  68. if (!o->username) {
  69. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "b_cstring_strdup failed");
  70. goto fail1;
  71. }
  72. }
  73. else {
  74. if (!func_unknown || !func_unknown(func_unknown_user, key, val)) {
  75. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "unknown option");
  76. goto fail1;
  77. }
  78. }
  79. }
  80. }
  81. o->nfds = 0;
  82. if (keep_stdout) {
  83. o->fds[o->nfds] = 1;
  84. o->fds_map[o->nfds++] = 1;
  85. }
  86. if (keep_stderr) {
  87. o->fds[o->nfds] = 2;
  88. o->fds_map[o->nfds++] = 2;
  89. }
  90. o->fds[o->nfds] = -1;
  91. if (out_keep_stdout) {
  92. *out_keep_stdout = keep_stdout;
  93. }
  94. if (out_keep_stderr) {
  95. *out_keep_stderr = keep_stderr;
  96. }
  97. return 1;
  98. fail1:
  99. if (o->username) {
  100. BFree(o->username);
  101. }
  102. fail0:
  103. return 0;
  104. }
  105. void NCDBProcessOpts_InitOld (NCDBProcessOpts *o, int keep_stdout, int keep_stderr, int do_setsid)
  106. {
  107. o->username = NULL;
  108. o->do_setsid = do_setsid;
  109. o->nfds = 0;
  110. if (keep_stdout) {
  111. o->fds[o->nfds] = 1;
  112. o->fds_map[o->nfds++] = 1;
  113. }
  114. if (keep_stderr) {
  115. o->fds[o->nfds] = 2;
  116. o->fds_map[o->nfds++] = 2;
  117. }
  118. o->fds[o->nfds] = -1;
  119. }
  120. void NCDBProcessOpts_Free (NCDBProcessOpts *o)
  121. {
  122. if (o->username) {
  123. BFree(o->username);
  124. }
  125. }
  126. struct BProcess_params NCDBProcessOpts_GetParams (NCDBProcessOpts *o)
  127. {
  128. struct BProcess_params params;
  129. params.username = o->username;
  130. params.fds = o->fds;
  131. params.fds_map = o->fds_map;
  132. params.do_setsid = o->do_setsid;
  133. return params;
  134. }