NCDBProcessOpts.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsMap(opts_arg)) {
  36. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "options must be a map");
  37. goto fail0;
  38. }
  39. o->username = NULL;
  40. o->do_setsid = 0;
  41. int keep_stdout = 0;
  42. int keep_stderr = 0;
  43. if (!NCDVal_IsInvalid(opts_arg)) {
  44. for (NCDValMapElem me = NCDVal_MapFirst(opts_arg); !NCDVal_MapElemInvalid(me); me = NCDVal_MapNext(opts_arg, me)) {
  45. NCDValRef key = NCDVal_MapElemKey(opts_arg, me);
  46. NCDValRef val = NCDVal_MapElemVal(opts_arg, me);
  47. if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stdout")) {
  48. keep_stdout = ncd_read_boolean(val);
  49. }
  50. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stderr")) {
  51. keep_stderr = ncd_read_boolean(val);
  52. }
  53. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "do_setsid")) {
  54. o->do_setsid = ncd_read_boolean(val);
  55. }
  56. else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "username")) {
  57. if (!NCDVal_IsStringNoNulls(val)) {
  58. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "username must be a string without nulls");
  59. goto fail1;
  60. }
  61. b_cstring cstr = NCDVal_StringCstring(val);
  62. o->username = b_cstring_strdup(cstr, 0, cstr.length);
  63. if (!o->username) {
  64. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "b_cstring_strdup failed");
  65. goto fail1;
  66. }
  67. }
  68. else {
  69. if (!func_unknown || !func_unknown(func_unknown_user, key, val)) {
  70. NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "unknown option");
  71. goto fail1;
  72. }
  73. }
  74. }
  75. }
  76. o->nfds = 0;
  77. if (keep_stdout) {
  78. o->fds[o->nfds] = 1;
  79. o->fds_map[o->nfds++] = 1;
  80. }
  81. if (keep_stderr) {
  82. o->fds[o->nfds] = 2;
  83. o->fds_map[o->nfds++] = 2;
  84. }
  85. o->fds[o->nfds] = -1;
  86. return 1;
  87. fail1:
  88. if (o->username) {
  89. BFree(o->username);
  90. }
  91. fail0:
  92. return 0;
  93. }
  94. void NCDBProcessOpts_InitOld (NCDBProcessOpts *o, int keep_stdout, int keep_stderr, int do_setsid)
  95. {
  96. o->username = NULL;
  97. o->do_setsid = do_setsid;
  98. o->nfds = 0;
  99. if (keep_stdout) {
  100. o->fds[o->nfds] = 1;
  101. o->fds_map[o->nfds++] = 1;
  102. }
  103. if (keep_stderr) {
  104. o->fds[o->nfds] = 2;
  105. o->fds_map[o->nfds++] = 2;
  106. }
  107. o->fds[o->nfds] = -1;
  108. }
  109. void NCDBProcessOpts_Free (NCDBProcessOpts *o)
  110. {
  111. if (o->username) {
  112. BFree(o->username);
  113. }
  114. }
  115. struct BProcess_params NCDBProcessOpts_GetParams (NCDBProcessOpts *o)
  116. {
  117. struct BProcess_params params;
  118. params.username = o->username;
  119. params.fds = o->fds;
  120. params.fds_map = o->fds_map;
  121. params.do_setsid = o->do_setsid;
  122. return params;
  123. }