s3: VFS: default: Remove fallback if we don't have HAVE_PWRITE set. Samba doesn't...
[samba.git] / ctdb / server / ctdb_update_record.c
index ce8e8afe117573e2c1744ca8c93d591acc7c2079..04b6901ce55eb51d4b41c98c79192df105fe0cb2 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "db_wrap.h"
-#include "tdb.h"
+#include "replace.h"
+#include "system/network.h"
+#include "system/time.h"
+
+#include <talloc.h>
+#include <tevent.h>
+
+#include "lib/tdb_wrap/tdb_wrap.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+#include "lib/util/sys_rw.h"
+#include "lib/util/util_process.h"
+
 #include "ctdb_private.h"
+#include "ctdb_client.h"
+
+#include "common/system.h"
+#include "common/common.h"
+#include "common/logging.h"
 
 struct ctdb_persistent_write_state {
        struct ctdb_db_context *ctdb_db;
        struct ctdb_marshall_buffer *m;
-       struct ctdb_req_control *c;
+       struct ctdb_req_control_old *c;
        uint32_t flags;
 };
 
-/* dont create/update records that does not exist locally */
+/* don't create/update records that does not exist locally */
 #define UPDATE_FLAGS_REPLACE_ONLY      1
 
 /*
@@ -39,7 +54,7 @@ struct ctdb_persistent_write_state {
 static int ctdb_persistent_store(struct ctdb_persistent_write_state *state)
 {
        int ret, i;
-       struct ctdb_rec_data *rec = NULL;
+       struct ctdb_rec_data_old *rec = NULL;
        struct ctdb_marshall_buffer *m = state->m;
 
        ret = tdb_transaction_start(state->ctdb_db->ltdb->tdb);
@@ -139,7 +154,8 @@ static void ctdb_persistent_write_callback(int status, void *private_data)
 /*
   called if our lockwait child times out
  */
-static void ctdb_persistent_lock_timeout(struct event_context *ev, struct timed_event *te,
+static void ctdb_persistent_lock_timeout(struct tevent_context *ev,
+                                        struct tevent_timer *te,
                                         struct timeval t, void *private_data)
 {
        struct ctdb_persistent_write_state *state = talloc_get_type(private_data,
@@ -151,7 +167,7 @@ static void ctdb_persistent_lock_timeout(struct event_context *ev, struct timed_
 struct childwrite_handle {
        struct ctdb_context *ctdb;
        struct ctdb_db_context *ctdb_db;
-       struct fd_event *fde;
+       struct tevent_fd *fde;
        int fd[2];
        pid_t child;
        void *private_data;
@@ -169,8 +185,9 @@ static int childwrite_destructor(struct childwrite_handle *h)
 /* called when the child process has finished writing the record to the
    database
 */
-static void childwrite_handler(struct event_context *ev, struct fd_event *fde,
-                            uint16_t flags, void *private_data)
+static void childwrite_handler(struct tevent_context *ev,
+                              struct tevent_fd *fde,
+                              uint16_t flags, void *private_data)
 {
        struct childwrite_handle *h = talloc_get_type(private_data,
                                                     struct childwrite_handle);
@@ -191,7 +208,7 @@ static void childwrite_handler(struct event_context *ev, struct fd_event *fde,
 
        talloc_set_destructor(h, NULL);
 
-       ret = read(h->fd[0], &c, 1);
+       ret = sys_read(h->fd[0], &c, 1);
        if (ret < 1) {
                DEBUG(DEBUG_ERR, (__location__ " Read returned %d. Childwrite failed\n", ret));
                c = 1;
@@ -250,19 +267,16 @@ static struct childwrite_handle *ctdb_childwrite(
                char c = 0;
 
                close(result->fd[0]);
-               debug_extra = talloc_asprintf(NULL, "childwrite-%s:", ctdb_db->db_name);
+               prctl_set_comment("ctdb_write_persistent");
                ret = ctdb_persistent_store(state);
                if (ret != 0) {
                        DEBUG(DEBUG_ERR, (__location__ " Failed to write persistent data\n"));
                        c = 1;
                }
 
-               write(result->fd[1], &c, 1);
+               sys_write(result->fd[1], &c, 1);
 
-               /* make sure we die when our parent dies */
-               while (ctdb_kill(ctdb_db->ctdb, parent, 0) == 0 || errno != ESRCH) {
-                       sleep(5);
-               }
+               ctdb_wait_for_process_to_exit(parent);
                _exit(0);
        }
 
@@ -273,9 +287,9 @@ static struct childwrite_handle *ctdb_childwrite(
 
        DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d for ctdb_childwrite\n", result->fd[0]));
 
-       result->fde = event_add_fd(ctdb_db->ctdb->ev, result, result->fd[0],
-                                  EVENT_FD_READ, childwrite_handler,
-                                  (void *)result);
+       result->fde = tevent_add_fd(ctdb_db->ctdb->ev, result, result->fd[0],
+                                   TEVENT_FD_READ, childwrite_handler,
+                                   (void *)result);
        if (result->fde == NULL) {
                talloc_free(result);
                CTDB_DECREMENT_STAT(ctdb_db->ctdb, pending_childwrite_calls);
@@ -293,7 +307,7 @@ static struct childwrite_handle *ctdb_childwrite(
    current record
  */
 int32_t ctdb_control_update_record(struct ctdb_context *ctdb,
-                                  struct ctdb_req_control *c, TDB_DATA recdata,
+                                  struct ctdb_req_control_old *c, TDB_DATA recdata,
                                   bool *async_reply)
 {
        struct ctdb_db_context *ctdb_db;
@@ -325,7 +339,7 @@ int32_t ctdb_control_update_record(struct ctdb_context *ctdb,
        state->c       = c;
        state->m       = m;
        state->flags   = 0;
-       if (!ctdb_db->persistent) {
+       if (ctdb_db_volatile(ctdb_db)) {
                state->flags   = UPDATE_FLAGS_REPLACE_ONLY;
        }
 
@@ -346,8 +360,9 @@ int32_t ctdb_control_update_record(struct ctdb_context *ctdb,
        talloc_steal(state, c);
 
        /* but we won't wait forever */
-       event_add_timed(ctdb->ev, state, timeval_current_ofs(ctdb->tunable.control_timeout, 0),
-                       ctdb_persistent_lock_timeout, state);
+       tevent_add_timer(ctdb->ev, state,
+                        timeval_current_ofs(ctdb->tunable.control_timeout, 0),
+                        ctdb_persistent_lock_timeout, state);
 
        return 0;
 }