MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbConfig.c
1 /*
2  Copyright (C) 2003-2006, 2008 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 #include <NdbConfig.h>
21 #include <NdbEnv.h>
22 #include <NdbMem.h>
23 #include <NdbHost.h>
24 #include <basestring_vsnprintf.h>
25 
26 static const char *datadir_path= 0;
27 
28 const char *
29 NdbConfig_get_path(int *_len)
30 {
31  const char *path= NdbEnv_GetEnv("NDB_HOME", 0, 0);
32  int path_len= 0;
33  if (path)
34  path_len= strlen(path);
35  if (path_len == 0 && datadir_path) {
36  path= datadir_path;
37  path_len= strlen(path);
38  }
39  if (path_len == 0) {
40  path= ".";
41  path_len= strlen(path);
42  }
43  if (_len)
44  *_len= path_len;
45  return path;
46 }
47 
48 static char*
49 NdbConfig_AllocHomePath(int _len)
50 {
51  int path_len;
52  const char *path= NdbConfig_get_path(&path_len);
53  int len= _len+path_len;
54  char *buf= NdbMem_Allocate(len);
55  basestring_snprintf(buf, len, "%s%s", path, DIR_SEPARATOR);
56  return buf;
57 }
58 
59 void
60 NdbConfig_SetPath(const char* path){
61  datadir_path= path;
62 }
63 
64 char*
65 NdbConfig_NdbCfgName(int with_ndb_home){
66  char *buf;
67  int len= 0;
68 
69  if (with_ndb_home) {
70  buf= NdbConfig_AllocHomePath(PATH_MAX);
71  len= strlen(buf);
72  } else
73  buf= NdbMem_Allocate(PATH_MAX);
74  basestring_snprintf(buf+len, PATH_MAX, "Ndb.cfg");
75  return buf;
76 }
77 
78 static
79 char *get_prefix_buf(int len, int node_id)
80 {
81  char tmp_buf[sizeof("ndb_pid#############")+1];
82  char *buf;
83  if (node_id > 0)
84  basestring_snprintf(tmp_buf, sizeof(tmp_buf), "ndb_%u", node_id);
85  else
86  basestring_snprintf(tmp_buf, sizeof(tmp_buf), "ndb_pid%u",
87  NdbHost_GetProcessId());
88  tmp_buf[sizeof(tmp_buf)-1]= 0;
89 
90  buf= NdbConfig_AllocHomePath(len+strlen(tmp_buf));
91  strcat(buf, tmp_buf);
92  return buf;
93 }
94 
95 char*
96 NdbConfig_ErrorFileName(int node_id){
97  char *buf= get_prefix_buf(PATH_MAX, node_id);
98  int len= strlen(buf);
99  basestring_snprintf(buf+len, PATH_MAX, "_error.log");
100  return buf;
101 }
102 
103 char*
104 NdbConfig_ClusterLogFileName(int node_id){
105  char *buf= get_prefix_buf(PATH_MAX, node_id);
106  int len= strlen(buf);
107  basestring_snprintf(buf+len, PATH_MAX, "_cluster.log");
108  return buf;
109 }
110 
111 char*
112 NdbConfig_SignalLogFileName(int node_id){
113  char *buf= get_prefix_buf(PATH_MAX, node_id);
114  int len= strlen(buf);
115  basestring_snprintf(buf+len, PATH_MAX, "_signal.log");
116  return buf;
117 }
118 
119 char*
120 NdbConfig_TraceFileName(int node_id, int file_no){
121  char *buf= get_prefix_buf(PATH_MAX, node_id);
122  int len= strlen(buf);
123  basestring_snprintf(buf+len, PATH_MAX, "_trace.log.%u", file_no);
124  return buf;
125 }
126 
127 char*
128 NdbConfig_NextTraceFileName(int node_id){
129  char *buf= get_prefix_buf(PATH_MAX, node_id);
130  int len= strlen(buf);
131  basestring_snprintf(buf+len, PATH_MAX, "_trace.log.next");
132  return buf;
133 }
134 
135 char*
136 NdbConfig_PidFileName(int node_id){
137  char *buf= get_prefix_buf(PATH_MAX, node_id);
138  int len= strlen(buf);
139  basestring_snprintf(buf+len, PATH_MAX, ".pid");
140  return buf;
141 }
142 
143 char*
144 NdbConfig_StdoutFileName(int node_id){
145  char *buf= get_prefix_buf(PATH_MAX, node_id);
146  int len= strlen(buf);
147  basestring_snprintf(buf+len, PATH_MAX, "_out.log");
148  return buf;
149 }