hadoop_err.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hadoop_err.h"
  19. #include <errno.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <uv.h>
  25. struct hadoop_err {
  26. int code;
  27. char *msg;
  28. int malloced;
  29. };
  30. #define SUFFIX ": "
  31. #define SUFFIX_LEN (sizeof(SUFFIX) - 1)
  32. #define RUNTIME_EXCEPTION_ERROR_CODE EFAULT
  33. #define RUNTIME_EXCEPTION_CLASS \
  34. "org.apache.hadoop.native.HadoopCore.RuntimeException"
  35. static const struct hadoop_err HADOOP_OOM_ERR = {
  36. RUNTIME_EXCEPTION_ERROR_CODE,
  37. RUNTIME_EXCEPTION_CLASS": Failed to allcoate memory for an error message.",
  38. 0
  39. };
  40. static const struct hadoop_err HADOOP_VSNPRINTF_ERR = {
  41. RUNTIME_EXCEPTION_ERROR_CODE,
  42. RUNTIME_EXCEPTION_CLASS": vsnprintf error while preparing an error "
  43. "message.",
  44. 0
  45. };
  46. static const char *errno_to_class(int code)
  47. {
  48. switch (code) {
  49. case RUNTIME_EXCEPTION_ERROR_CODE:
  50. return RUNTIME_EXCEPTION_CLASS;
  51. case EINVAL:
  52. return "org.apache.hadoop.native.HadoopCore.InvalidRequestException";
  53. case ENOMEM:
  54. return "org.apache.hadoop.native.HadoopCore.OutOfMemoryException";
  55. default:
  56. return "org.apache.hadoop.native.HadoopCore.IOException";
  57. }
  58. }
  59. static const char *uv_code_to_class(int code)
  60. {
  61. switch (code) {
  62. case UV_EOF:
  63. return "org.apache.hadoop.native.HadoopCore.EOFException";
  64. case UV_EINVAL:
  65. return "org.apache.hadoop.native.HadoopCore.InvalidRequestException";
  66. case UV_ECONNREFUSED:
  67. return "org.apache.hadoop.native.HadoopCore.ConnectionRefusedException";
  68. case UV_ENOMEM:
  69. return "org.apache.hadoop.native.HadoopCore.OutOfMemoryException";
  70. default:
  71. return "org.apache.hadoop.native.HadoopCore.IOException";
  72. }
  73. }
  74. struct hadoop_err *hadoop_lerr_alloc(int code, const char *fmt, ...)
  75. {
  76. struct hadoop_err *err = NULL, *ierr = NULL;
  77. va_list ap, ap2;
  78. const char *class;
  79. char test_buf[1];
  80. int fmt_len, class_len, msg_len;
  81. err = calloc(1, sizeof(*err));
  82. if (!err) {
  83. ierr = (struct hadoop_err*)&HADOOP_OOM_ERR;
  84. goto done;
  85. }
  86. err->code = code;
  87. err->malloced = 1;
  88. va_start(ap, fmt);
  89. va_copy(ap2, ap);
  90. fmt_len = vsnprintf(test_buf, sizeof(test_buf), fmt, ap);
  91. if (fmt_len < 0) {
  92. ierr = (struct hadoop_err*)&HADOOP_VSNPRINTF_ERR;
  93. goto done;
  94. }
  95. class = errno_to_class(code);
  96. class_len = strlen(class);
  97. msg_len = class_len + SUFFIX_LEN + fmt_len + 1;
  98. err->msg = malloc(msg_len);
  99. if (!err->msg) {
  100. ierr = (struct hadoop_err*)&HADOOP_OOM_ERR;
  101. goto done;
  102. }
  103. strcpy(err->msg, class);
  104. strcpy(err->msg + class_len, SUFFIX);
  105. vsprintf(err->msg + class_len + SUFFIX_LEN, fmt, ap2);
  106. va_end(ap2);
  107. va_end(ap);
  108. done:
  109. if (ierr) {
  110. hadoop_err_free(err);
  111. return ierr;
  112. }
  113. return err;
  114. }
  115. struct hadoop_err *hadoop_uverr_alloc(int code, const char *fmt, ...)
  116. {
  117. struct hadoop_err *err = NULL, *ierr = NULL;
  118. va_list ap, ap2;
  119. const char *class;
  120. const char *umsg;
  121. char test_buf[1];
  122. int fmt_len, umsg_len, class_len, msg_len;
  123. err = calloc(1, sizeof(*err));
  124. if (!err) {
  125. ierr = (struct hadoop_err*)&HADOOP_OOM_ERR;
  126. goto done;
  127. }
  128. err->code = code;
  129. umsg = uv_strerror(code);
  130. umsg_len = strlen(umsg);
  131. err->malloced = 1;
  132. va_start(ap, fmt);
  133. va_copy(ap2, ap);
  134. fmt_len = vsnprintf(test_buf, sizeof(test_buf), fmt, ap);
  135. if (fmt_len < 0) {
  136. ierr = (struct hadoop_err*)&HADOOP_VSNPRINTF_ERR;
  137. goto done;
  138. }
  139. class = uv_code_to_class(err->code);
  140. class_len = strlen(class);
  141. msg_len = class_len + SUFFIX_LEN + umsg_len + SUFFIX_LEN + fmt_len + 1;
  142. err->msg = malloc(msg_len);
  143. if (!err->msg) {
  144. ierr = (struct hadoop_err*)&HADOOP_OOM_ERR;
  145. goto done;
  146. }
  147. strcpy(err->msg, class);
  148. strcpy(err->msg + class_len, SUFFIX);
  149. strcpy(err->msg + class_len + SUFFIX_LEN, umsg);
  150. strcpy(err->msg + class_len + SUFFIX_LEN + umsg_len, SUFFIX);
  151. vsprintf(err->msg + class_len + SUFFIX_LEN + umsg_len + SUFFIX_LEN,
  152. fmt, ap2);
  153. va_end(ap2);
  154. va_end(ap);
  155. done:
  156. if (ierr) {
  157. hadoop_err_free(err);
  158. return ierr;
  159. }
  160. return err;
  161. }
  162. void hadoop_err_free(struct hadoop_err *err)
  163. {
  164. if (!err)
  165. return;
  166. if (!err->malloced)
  167. return;
  168. free(err->msg);
  169. free(err);
  170. }
  171. int hadoop_err_code(const struct hadoop_err *err)
  172. {
  173. return err->code;
  174. }
  175. const char *hadoop_err_msg(const struct hadoop_err *err)
  176. {
  177. return err->msg;
  178. }
  179. // vim: ts=4:sw=4:tw=79:et