lib/util: Remove obsolete sys_getpid() and sys_fork().
[sfrench/samba-autobuild/.git] / source3 / smbd / process.c
index c163212bc0dd5c901d3f95a3fd78e5f45f6c099c..ed19e7f42bf1aa8ae946509237d552a1cedae2fb 100644 (file)
@@ -76,7 +76,7 @@ static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn)
                return true;
        }
 
-       DEBUG(10,("pid[%d] wait for socket lock\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] wait for socket lock\n", (int)getpid()));
 
        do {
                ok = fcntl_lock(
@@ -89,7 +89,7 @@ static bool smbd_lock_socket_internal(struct smbd_server_connection *sconn)
                return false;
        }
 
-       DEBUG(10,("pid[%d] got for socket lock\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] got for socket lock\n", (int)getpid()));
 
        return true;
 }
@@ -126,7 +126,7 @@ static bool smbd_unlock_socket_internal(struct smbd_server_connection *sconn)
                return false;
        }
 
-       DEBUG(10,("pid[%d] unlocked socket\n", (int)sys_getpid()));
+       DEBUG(10,("pid[%d] unlocked socket\n", (int)getpid()));
 
        return true;
 }
@@ -182,7 +182,7 @@ bool srv_send_smb(struct smbd_server_connection *sconn, char *buffer,
                 * client failed.
                 */
                DEBUG(1,("pid[%d] Error writing %d bytes to client %s. %d. (%s)\n",
-                        (int)sys_getpid(), (int)len,
+                        (int)getpid(), (int)len,
                         get_peer_addr(sconn->sock, addr, sizeof(addr)),
                         (int)ret, strerror(errno) ));
 
@@ -538,10 +538,9 @@ static bool init_smb_request(struct smb_request *req,
        req->sconn = sconn;
        req->conn = conn_find(sconn,req->tid);
        req->chain_fsp = NULL;
-       req->chain_outbuf = NULL;
-       req->done = false;
        req->smb2req = NULL;
        req->priv_paths = NULL;
+       req->chain = NULL;
        smb_init_perfcount_data(&req->pcd);
 
        /* Ensure we have at least wct words and 2 bytes of bcc. */
@@ -1316,12 +1315,17 @@ static void smb_dump(const char *name, int type, const char *data)
 
        len = smb_len_tcp(data)+4;
        for (i=1;i<100;i++) {
-               if (asprintf(&fname, "/tmp/%s.%d.%s", name, i,
-                            type ? "req" : "resp") == -1) {
+               fname = talloc_asprintf(talloc_tos(),
+                               "/tmp/%s.%d.%s",
+                               name,
+                               i,
+                               type ? "req" : "resp");
+               if (fname == NULL) {
                        return;
                }
                fd = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0644);
                if (fd != -1 || errno != EEXIST) break;
+               TALLOC_FREE(fname);
        }
        if (fd != -1) {
                ssize_t ret = write(fd, data, len);
@@ -1330,7 +1334,7 @@ static void smb_dump(const char *name, int type, const char *data)
                close(fd);
                DEBUG(0,("created %s len %lu\n", fname, (unsigned long)len));
        }
-       SAFE_FREE(fname);
+       TALLOC_FREE(fname);
 }
 
 /****************************************************************************
@@ -1369,7 +1373,7 @@ static connection_struct *switch_message(uint8 type, struct smb_request *req)
        conn = req->conn;
 
        DEBUG(3,("switch message %s (pid %d) conn 0x%lx\n", smb_fn_name(type),
-                (int)sys_getpid(), (unsigned long)conn));
+                (int)getpid(), (unsigned long)conn));
 
        smb_dump(smb_fn_name(type), 1, (const char *)req->inbuf);
 
@@ -1538,11 +1542,6 @@ static void construct_reply(struct smbd_server_connection *sconn,
 
        conn = switch_message(req->cmd, req);
 
-       if (req->done) {
-               TALLOC_FREE(req);
-               return;
-       }
-
        if (req->outbuf == NULL) {
                return;
        }
@@ -1569,79 +1568,110 @@ static void construct_reply_chain(struct smbd_server_connection *sconn,
                                  bool encrypted,
                                  struct smb_perfcount_data *deferred_pcd)
 {
-       struct connection_struct *conn = NULL;
        struct smb_request **reqs = NULL;
-       struct smb_request *req, *first_req, *last_req;
-       unsigned i, num_reqs;
-       NTSTATUS status;
+       struct smb_request *req;
+       unsigned num_reqs;
        bool ok;
 
        ok = smb1_parse_chain(talloc_tos(), (uint8_t *)inbuf, sconn, encrypted,
                              seqnum, &reqs, &num_reqs);
        if (!ok) {
-               status = NT_STATUS_INVALID_PARAMETER;
-               goto error;
+               char errbuf[smb_size];
+               error_packet(errbuf, 0, 0, NT_STATUS_INVALID_PARAMETER,
+                            __LINE__, __FILE__);
+               if (!srv_send_smb(sconn, errbuf, true, seqnum, encrypted,
+                                 NULL)) {
+                       exit_server_cleanly("construct_reply_chain: "
+                                           "srv_send_smb failed.");
+               }
+               return;
        }
 
-       for (i=0; i<num_reqs; i++) {
-               req = reqs[i];
+       req = reqs[0];
+       req->inbuf = (uint8_t *)talloc_move(reqs, &inbuf);
 
-               if (i > 0) {
-                       /*
-                        * Get the session and tree ids and chain fsp
-                        * from the previous request into the current
-                        * one
-                        */
-                       struct smb_request *prev_req = reqs[i-1];
-                       const uint8_t *prev_outbuf = prev_req->outbuf;
+       req->conn = switch_message(req->cmd, req);
 
-                       req->vuid = SVAL(prev_outbuf, smb_uid);
-                       req->tid  = SVAL(prev_outbuf, smb_tid);
-                       req->conn = conn_find(req->sconn, req->tid);
-                       req->chain_fsp = prev_req->chain_fsp;
-               }
-               req->inbuf = (uint8_t *)inbuf;
-               conn = switch_message(req->cmd, req);
+       if (req->outbuf == NULL) {
+               /*
+                * Request has suspended itself, will come
+                * back here.
+                */
+               return;
+       }
+       smb_request_done(req);
+}
 
-               if (req->outbuf == NULL) {
-                       if (open_was_deferred(req->sconn, req->mid)) {
-                               TALLOC_FREE(reqs);
-                               return;
-                       }
-                       status = NT_STATUS_INTERNAL_ERROR;
-                       goto error;
-               }
-               if (IVAL(req->outbuf, smb_rcls) != 0) {
-                       break;
-               }
+/*
+ * To be called from an async SMB handler that is potentially chained
+ * when it is finished for shipping.
+ */
+
+void smb_request_done(struct smb_request *req)
+{
+       struct smb_request **reqs = NULL;
+       struct smb_request *first_req;
+       size_t i, num_reqs, next_index;
+       NTSTATUS status;
+
+       if (req->chain == NULL) {
+               first_req = req;
+               goto shipit;
        }
 
-       first_req = reqs[0];
-       last_req = req;
+       reqs = req->chain;
+       num_reqs = talloc_array_length(reqs);
 
-       if (i == 0) {
+       for (i=0; i<num_reqs; i++) {
+               if (reqs[i] == req) {
+                       break;
+               }
+       }
+       if (i == num_reqs) {
                /*
-                * The first request already gave an error, no need to
-                * do any splicing
+                * Invalid chain, should not happen
                 */
-               goto shipit;
+               status = NT_STATUS_INTERNAL_ERROR;
+               goto error;
+       }
+       next_index = i+1;
+
+       while ((next_index < num_reqs) && (IVAL(req->outbuf, smb_rcls) == 0)) {
+               struct smb_request *next = reqs[next_index];
+
+               next->vuid = SVAL(req->outbuf, smb_uid);
+               next->tid  = SVAL(req->outbuf, smb_tid);
+               next->conn = conn_find(req->sconn, req->tid);
+               next->chain_fsp = req->chain_fsp;
+               next->inbuf = (uint8_t *)req->inbuf;
+
+               req = next;
+               req->conn = switch_message(req->cmd, req);
+
+               if (req->outbuf == NULL) {
+                       /*
+                        * Request has suspended itself, will come
+                        * back here.
+                        */
+                       return;
+               }
+               next_index += 1;
        }
 
+       first_req = reqs[0];
+
        for (i=1; i<next_index; i++) {
-               req = reqs[i];
+               bool ok;
 
-               ok = smb_splice_chain(&first_req->outbuf, req->outbuf);
+               ok = smb_splice_chain(&first_req->outbuf, reqs[i]->outbuf);
                if (!ok) {
                        status = NT_STATUS_INTERNAL_ERROR;
                        goto error;
                }
-               if (req == last_req) {
-                       break;
-               }
        }
 
-       SSVAL(first_req->outbuf, smb_uid, SVAL(last_req->outbuf, smb_uid));
-       SSVAL(first_req->outbuf, smb_tid, SVAL(last_req->outbuf, smb_tid));
+       SSVAL(first_req->outbuf, smb_uid, SVAL(req->outbuf, smb_uid));
+       SSVAL(first_req->outbuf, smb_tid, SVAL(req->outbuf, smb_tid));
 
        /*
         * This scary statement intends to set the
@@ -1650,42 +1680,43 @@ static void construct_reply_chain(struct smbd_server_connection *sconn,
         */
        SSVAL(first_req->outbuf, smb_flg2,
              (SVAL(first_req->outbuf, smb_flg2) & ~FLAGS2_32_BIT_ERROR_CODES)
-             |(SVAL(last_req->outbuf, smb_flg2) & FLAGS2_32_BIT_ERROR_CODES));
+             |(SVAL(req->outbuf, smb_flg2) & FLAGS2_32_BIT_ERROR_CODES));
 
        /*
         * Transfer the error codes from the subrequest to the main one
         */
-       SSVAL(first_req->outbuf, smb_rcls, SVAL(last_req->outbuf, smb_rcls));
-       SSVAL(first_req->outbuf, smb_err,  SVAL(last_req->outbuf, smb_err));
+       SSVAL(first_req->outbuf, smb_rcls, SVAL(req->outbuf, smb_rcls));
+       SSVAL(first_req->outbuf, smb_err,  SVAL(req->outbuf, smb_err));
 
-       goto shipit;
+       _smb_setlen_large(
+               first_req->outbuf, talloc_get_size(first_req->outbuf) - 4);
 
 shipit:
-       _smb_setlen_large(first_req->outbuf,
-                         talloc_get_size(first_req->outbuf) - 4);
-
        if (!srv_send_smb(first_req->sconn,
                          (char *)first_req->outbuf,
                          true, first_req->seqnum+1,
-                         IS_CONN_ENCRYPTED(conn)||first_req->encrypted,
+                         IS_CONN_ENCRYPTED(req->conn)||first_req->encrypted,
                          &first_req->pcd)) {
                exit_server_cleanly("construct_reply_chain: srv_send_smb "
                                    "failed.");
        }
-       TALLOC_FREE(reqs);
+       TALLOC_FREE(req);       /* non-chained case */
+       TALLOC_FREE(reqs);      /* chained case */
        return;
 
 error:
        {
                char errbuf[smb_size];
                error_packet(errbuf, 0, 0, status, __LINE__, __FILE__);
-               if (!srv_send_smb(sconn, errbuf, true, seqnum, encrypted,
+               if (!srv_send_smb(req->sconn, errbuf, true,
+                                 req->seqnum+1, req->encrypted,
                                  NULL)) {
                        exit_server_cleanly("construct_reply_chain: "
                                            "srv_send_smb failed.");
                }
        }
-       TALLOC_FREE(reqs);
+       TALLOC_FREE(req);       /* non-chained case */
+       TALLOC_FREE(reqs);      /* chained case */
 }
 
 /****************************************************************************
@@ -1833,25 +1864,6 @@ void construct_reply_common_req(struct smb_request *req, char *outbuf)
        construct_reply_common(req, (const char *)req->inbuf, outbuf);
 }
 
-/*
- * How many bytes have we already accumulated up to the current wct field
- * offset?
- */
-
-size_t req_wct_ofs(struct smb_request *req)
-{
-       size_t buf_size;
-
-       if (req->chain_outbuf == NULL) {
-               return smb_wct - 4;
-       }
-       buf_size = talloc_get_size(req->chain_outbuf);
-       if ((buf_size % 4) != 0) {
-               buf_size += (4 - (buf_size % 4));
-       }
-       return buf_size - 4;
-}
-
 /**
  * @brief Find the smb_cmd offset of the last command pushed
  * @param[in] buf      The buffer we're building up
@@ -2246,6 +2258,7 @@ bool smb1_parse_chain(TALLOC_CTX *mem_ctx, const uint8_t *buf,
                      struct smb_request ***reqs, unsigned *num_reqs)
 {
        struct smb1_parse_chain_state state;
+       unsigned i;
 
        state.mem_ctx = mem_ctx;
        state.buf = buf;
@@ -2259,6 +2272,9 @@ bool smb1_parse_chain(TALLOC_CTX *mem_ctx, const uint8_t *buf,
                TALLOC_FREE(state.reqs);
                return false;
        }
+       for (i=0; i<state.num_reqs; i++) {
+               state.reqs[i]->chain = state.reqs;
+       }
        *reqs = state.reqs;
        *num_reqs = state.num_reqs;
        return true;
@@ -2622,7 +2638,7 @@ static void smbd_echo_read_waited(struct tevent_req *subreq)
 
        if (!fd_is_readable(sconn->sock)) {
                DEBUG(10,("echo_handler[%d] the parent smbd was faster\n",
-                         (int)sys_getpid()));
+                         (int)getpid()));
 
                ok = smbd_unlock_socket_internal(sconn);
                if (!ok) {
@@ -2651,7 +2667,7 @@ static void smbd_echo_read_waited(struct tevent_req *subreq)
        if (tevent_req_nterror(req, status)) {
                tevent_req_nterror(req, status);
                DEBUG(1, ("echo_handler[%d]: receive_smb_raw_talloc failed: %s\n",
-                         (int)sys_getpid(), nt_errstr(status)));
+                         (int)getpid(), nt_errstr(status)));
                return;
        }
 
@@ -2735,7 +2751,7 @@ static void smbd_echo_writer_done(struct tevent_req *req)
                DEBUG(1, ("writev to parent failed: %s\n", strerror(err)));
                exit(1);
        }
-       DEBUG(10,("echo_handler[%d]: forwarded pdu to main\n", (int)sys_getpid()));
+       DEBUG(10,("echo_handler[%d]: forwarded pdu to main\n", (int)getpid()));
        smbd_echo_activate_writer(state);
 }
 
@@ -2916,7 +2932,7 @@ static void smbd_echo_got_packet(struct tevent_req *req)
                iov->iov_len = buflen;
 
                DEBUG(10,("echo_handler[%d]: forward to main\n",
-                         (int)sys_getpid()));
+                         (int)getpid()));
                smbd_echo_activate_writer(state);
        }
 
@@ -2949,7 +2965,7 @@ bool fork_echo_handler(struct smbd_server_connection *sconn)
                goto fail;
        }
 
-       child = sys_fork();
+       child = fork();
        if (child == 0) {
                NTSTATUS status;
 
@@ -2971,7 +2987,7 @@ bool fork_echo_handler(struct smbd_server_connection *sconn)
        listener_pipe[1] = -1;
        sconn->smb1.echo_handler.trusted_fd = listener_pipe[0];
 
-       DEBUG(10,("fork_echo_handler: main[%d] echo_child[%d]\n", (int)sys_getpid(), child));
+       DEBUG(10,("fork_echo_handler: main[%d] echo_child[%d]\n", (int)getpid(), child));
 
        /*
         * Without smb signing this is the same as the normal smbd