Debian Codegraph @

xlators/mgmt/glusterd/src/glusterd-gfproxyd-svc-helper.c

raw symbols SCIP
1 /*
2 Copyright (c) 2016 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10
11 #include "glusterd-utils.h"
12 #include "glusterd-gfproxyd-svc-helper.h"
13 #include "glusterd-messages.h"
14 #include <glusterfs/syscall.h>
15 #include "glusterd-volgen.h"
16
17 void
18 glusterd_svc_build_gfproxyd_rundir(glusterd_volinfo_t *volinfo, char *path,
19 int path_len)
20 {
21 char workdir[PATH_MAX] = {
22 0,
23 };
24 glusterd_conf_t *priv = THIS->private;
25
26 GLUSTERD_GET_VOLUME_PID_DIR(workdir, volinfo, priv);
27
28 snprintf(path, path_len, "%s", workdir);
29 }
30
31 void
32 glusterd_svc_build_gfproxyd_socket_filepath(glusterd_volinfo_t *volinfo,
33 char *path, int path_len)
34 {
35 char sockfilepath[PATH_MAX] = {
36 0,
37 };
38 char rundir[PATH_MAX] = {
39 0,
40 };
41 int32_t len = 0;
42
43 glusterd_svc_build_gfproxyd_rundir(volinfo, rundir, sizeof(rundir));
44 len = snprintf(sockfilepath, sizeof(sockfilepath), "%s/run-%s", rundir,
45 uuid_utoa(MY_UUID));
46 if ((len < 0) || (len >= sizeof(sockfilepath))) {
47 sockfilepath[0] = 0;
48 }
49
50 glusterd_set_socket_filepath(sockfilepath, path, path_len);
51 }
52
53 void
54 glusterd_svc_build_gfproxyd_pidfile(glusterd_volinfo_t *volinfo, char *path,
55 int path_len)
56 {
57 char rundir[PATH_MAX] = {
58 0,
59 };
60
61 glusterd_svc_build_gfproxyd_rundir(volinfo, rundir, sizeof(rundir));
62
63 snprintf(path, path_len, "%s/%s.gfproxyd.pid", rundir, volinfo->volname);
64 }
65
66 void
67 glusterd_svc_build_gfproxyd_volfile_path(glusterd_volinfo_t *volinfo,
68 char *path, int path_len)
69 {
70 char workdir[PATH_MAX] = {
71 0,
72 };
73 glusterd_conf_t *priv = THIS->private;
74
75 GLUSTERD_GET_VOLUME_DIR(workdir, volinfo, priv);
76
77 snprintf(path, path_len, "%s/%s.gfproxyd.vol", workdir, volinfo->volname);
78 }
79
80 void
81 glusterd_svc_build_gfproxyd_logdir(char *logdir, char *volname, size_t len)
82 {
83 glusterd_conf_t *conf = THIS->private;
84 snprintf(logdir, len, "%s/gfproxy/%s", conf->logdir, volname);
85 }
86
87 void
88 glusterd_svc_build_gfproxyd_logfile(char *logfile, char *logdir, size_t len)
89 {
90 snprintf(logfile, len, "%s/gfproxyd.log", logdir);
91 }
92
93 int
94 glusterd_is_gfproxyd_enabled(glusterd_volinfo_t *volinfo)
95 {
96 return glusterd_volinfo_get_boolean(volinfo, VKEY_CONFIG_GFPROXY);
97 }
98
99 static int
100 glusterd_svc_get_gfproxyd_volfile(glusterd_volinfo_t *volinfo, char *svc_name,
101 char *orgvol, char **tmpvol, int path_len)
102 {
103 int tmp_fd = -1;
104 int ret = -1;
105 int need_unlink = 0;
106
107 glusterd_svc_build_gfproxyd_volfile_path(volinfo, orgvol, path_len);
108
109 ret = gf_asprintf(tmpvol, "/tmp/g%s-XXXXXX", svc_name);
110 if (ret < 0) {
111 goto out;
112 }
113
114 /* coverity[SECURE_TEMP] mkstemp uses 0600 as the mode and is safe */
115 tmp_fd = mkstemp(*tmpvol);
116 if (tmp_fd < 0) {
117 gf_msg("glusterd", GF_LOG_WARNING, errno, GD_MSG_FILE_OP_FAILED,
118 "Unable to create temp file"
119 " %s:(%s)",
120 *tmpvol, strerror(errno));
121 ret = -1;
122 goto out;
123 }
124
125 need_unlink = 1;
126 ret = glusterd_build_gfproxyd_volfile(volinfo, *tmpvol);
127 out:
128 if (need_unlink && ret < 0)
129 sys_unlink(*tmpvol);
130
131 if ((ret < 0) && (*tmpvol != NULL)) {
132 GF_FREE(*tmpvol);
133 *tmpvol = NULL;
134 }
135
136 if (tmp_fd >= 0)
137 sys_close(tmp_fd);
138
139 return ret;
140 }
141
142 int
143 glusterd_svc_check_gfproxyd_volfile_identical(char *svc_name,
144 glusterd_volinfo_t *volinfo,
145 gf_boolean_t *identical)
146 {
147 char orgvol[PATH_MAX] = {
148 0,
149 };
150 char *tmpvol = NULL;
151 int ret = -1;
152 int need_unlink = 0;
153
154 GF_VALIDATE_OR_GOTO("glusterd", identical, out);
155
156 ret = glusterd_svc_get_gfproxyd_volfile(volinfo, svc_name, orgvol, &tmpvol,
157 PATH_MAX);
158 if (ret)
159 goto out;
160
161 need_unlink = 1;
162 ret = glusterd_check_files_identical(orgvol, tmpvol, identical);
163 if (ret)
164 goto out;
165
166 out:
167 if (need_unlink)
168 sys_unlink(tmpvol);
169
170 if (tmpvol != NULL)
171 GF_FREE(tmpvol);
172
173 return ret;
174 }
175
176 int
177 glusterd_svc_check_gfproxyd_topology_identical(char *svc_name,
178 glusterd_volinfo_t *volinfo,
179 gf_boolean_t *identical)
180 {
181 char orgvol[PATH_MAX] = {
182 0,
183 };
184 char *tmpvol = NULL;
185 int ret = -1;
186 int tmpclean = 0;
187
188 GF_VALIDATE_OR_GOTO("glusterd", identical, out);
189
190 ret = glusterd_svc_get_gfproxyd_volfile(volinfo, svc_name, orgvol, &tmpvol,
191 PATH_MAX);
192 if (ret)
193 goto out;
194
195 tmpclean = 1; /* SET the flag to unlink() tmpfile */
196
197 /* Compare the topology of volfiles */
198 ret = glusterd_check_topology_identical(orgvol, tmpvol, identical);
199 out:
200 if (tmpclean)
201 sys_unlink(tmpvol);
202
203 if (tmpvol != NULL)
204 GF_FREE(tmpvol);
205
206 return ret;
207 }
208
209 glusterd_volinfo_t *
210 glusterd_gfproxyd_volinfo_from_svc(glusterd_svc_t *svc)
211 {
212 glusterd_volinfo_t *volinfo = NULL;
213 glusterd_gfproxydsvc_t *gfproxyd = NULL;
214
215 /* Get volinfo->gfproxyd from svc object */
216 gfproxyd = cds_list_entry(svc, glusterd_gfproxydsvc_t, svc);
217 if (!gfproxyd) {
218 gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_SNAPD_OBJ_GET_FAIL,
219 "Failed to get gfproxyd "
220 "object from gfproxyd service");
221 goto out;
222 }
223
224 /* Get volinfo from gfproxyd */
225 volinfo = cds_list_entry(gfproxyd, glusterd_volinfo_t, gfproxyd);
226 if (!volinfo) {
227 gf_msg("glusterd", GF_LOG_ERROR, 0, GD_MSG_VOLINFO_GET_FAIL,
228 "Failed to get volinfo from "
229 "from gfproxyd");
230 goto out;
231 }
232 out:
233 return volinfo;
234 }