init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "config.h"
  15. #include <stdio.h>
  16. #include <stdarg.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include <rpc/xdr.h>
  21. #include "dlinklist.h"
  22. #include "nfs.h"
  23. #include "libnfs-raw.h"
  24. #include "libnfs-private.h"
  25. struct rpc_context *rpc_init_context(void)
  26. {
  27. struct rpc_context *rpc;
  28. rpc = malloc(sizeof(struct rpc_context));
  29. if (rpc == NULL) {
  30. printf("Failed to allocate rpc context\n");
  31. return NULL;
  32. }
  33. bzero(rpc, sizeof(struct rpc_context));
  34. rpc->encodebuflen = 65536;
  35. rpc->encodebuf = malloc(rpc->encodebuflen);
  36. if (rpc->encodebuf == NULL) {
  37. printf("Failed to allocate a buffer for rpc encoding\n");
  38. free(rpc);
  39. return NULL;
  40. }
  41. rpc->auth = authunix_create_default();
  42. if (rpc->auth == NULL) {
  43. printf("failed to create authunix\n");
  44. free(rpc->encodebuf);
  45. free(rpc);
  46. return NULL;
  47. }
  48. rpc->xid = 1;
  49. rpc->fd = -1;
  50. return rpc;
  51. }
  52. void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
  53. {
  54. if (rpc->auth != NULL) {
  55. auth_destroy(rpc->auth);
  56. }
  57. rpc->auth = auth;
  58. }
  59. void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
  60. {
  61. va_list ap;
  62. char *str;
  63. if (rpc->error_string != NULL) {
  64. free(rpc->error_string);
  65. }
  66. va_start(ap, error_string);
  67. vasprintf(&str, error_string, ap);
  68. rpc->error_string = str;
  69. va_end(ap);
  70. }
  71. char *rpc_get_error(struct rpc_context *rpc)
  72. {
  73. return rpc->error_string;
  74. }
  75. void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
  76. {
  77. struct rpc_pdu *pdu;
  78. while((pdu = rpc->outqueue) != NULL) {
  79. pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
  80. DLIST_REMOVE(rpc->outqueue, pdu);
  81. rpc_free_pdu(rpc, pdu);
  82. }
  83. while((pdu = rpc->waitpdu) != NULL) {
  84. pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
  85. DLIST_REMOVE(rpc->waitpdu, pdu);
  86. rpc_free_pdu(rpc, pdu);
  87. }
  88. }
  89. void rpc_destroy_context(struct rpc_context *rpc)
  90. {
  91. struct rpc_pdu *pdu;
  92. while((pdu = rpc->outqueue) != NULL) {
  93. pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
  94. DLIST_REMOVE(rpc->outqueue, pdu);
  95. rpc_free_pdu(rpc, pdu);
  96. }
  97. while((pdu = rpc->waitpdu) != NULL) {
  98. pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
  99. DLIST_REMOVE(rpc->waitpdu, pdu);
  100. rpc_free_pdu(rpc, pdu);
  101. }
  102. auth_destroy(rpc->auth);
  103. rpc->auth =NULL;
  104. if (rpc->fd != -1) {
  105. close(rpc->fd);
  106. }
  107. if (rpc->encodebuf != NULL) {
  108. free(rpc->encodebuf);
  109. rpc->encodebuf = NULL;
  110. }
  111. if (rpc->error_string != NULL) {
  112. free(rpc->error_string);
  113. rpc->error_string = NULL;
  114. }
  115. free(rpc);
  116. }