xref: /f-stack/app/redis-5.0.5/src/childinfo.c (revision 572c4311)
1 /*
2  * Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *   * Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *   * Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *   * Neither the name of Redis nor the names of its contributors may be used
14  *     to endorse or promote products derived from this software without
15  *     specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "server.h"
31 #include <unistd.h>
32 
33 /* Open a child-parent channel used in order to move information about the
34  * RDB / AOF saving process from the child to the parent (for instance
35  * the amount of copy on write memory used) */
openChildInfoPipe(void)36 void openChildInfoPipe(void) {
37     if (pipe(server.child_info_pipe) == -1) {
38         /* On error our two file descriptors should be still set to -1,
39          * but we call anyway cloesChildInfoPipe() since can't hurt. */
40         closeChildInfoPipe();
41     } else if (anetNonBlock(NULL,server.child_info_pipe[0]) != ANET_OK) {
42         closeChildInfoPipe();
43     } else {
44         memset(&server.child_info_data,0,sizeof(server.child_info_data));
45     }
46 }
47 
48 /* Close the pipes opened with openChildInfoPipe(). */
closeChildInfoPipe(void)49 void closeChildInfoPipe(void) {
50     if (server.child_info_pipe[0] != -1 ||
51         server.child_info_pipe[1] != -1)
52     {
53         close(server.child_info_pipe[0]);
54         close(server.child_info_pipe[1]);
55         server.child_info_pipe[0] = -1;
56         server.child_info_pipe[1] = -1;
57     }
58 }
59 
60 /* Send COW data to parent. The child should call this function after populating
61  * the corresponding fields it want to sent (according to the process type). */
sendChildInfo(int ptype)62 void sendChildInfo(int ptype) {
63     if (server.child_info_pipe[1] == -1) return;
64     server.child_info_data.magic = CHILD_INFO_MAGIC;
65     server.child_info_data.process_type = ptype;
66     ssize_t wlen = sizeof(server.child_info_data);
67     if (write(server.child_info_pipe[1],&server.child_info_data,wlen) != wlen) {
68         /* Nothing to do on error, this will be detected by the other side. */
69     }
70 }
71 
72 /* Receive COW data from parent. */
receiveChildInfo(void)73 void receiveChildInfo(void) {
74     if (server.child_info_pipe[0] == -1) return;
75     ssize_t wlen = sizeof(server.child_info_data);
76     if (read(server.child_info_pipe[0],&server.child_info_data,wlen) == wlen &&
77         server.child_info_data.magic == CHILD_INFO_MAGIC)
78     {
79         if (server.child_info_data.process_type == CHILD_INFO_TYPE_RDB) {
80             server.stat_rdb_cow_bytes = server.child_info_data.cow_size;
81         } else if (server.child_info_data.process_type == CHILD_INFO_TYPE_AOF) {
82             server.stat_aof_cow_bytes = server.child_info_data.cow_size;
83         }
84     }
85 }
86