File: mahelper_file.c

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (280 lines) | stat: -rw-r--r-- 6,613 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>

#include "mahelper.h"
#include "mahelper_priv.h"


static int write_fd(int sockfd, const char *data, size_t datalen, int fd)
{
  struct msghdr msg;
  struct iovec iov[1];
  union {
    struct cmsghdr cm;
    char control[CMSG_SPACE(sizeof(int))];
  } control_un;
  struct cmsghdr *cmptr;
  
  msg.msg_control= control_un.control;
  msg.msg_controllen= sizeof(control_un.control);
  
  cmptr= CMSG_FIRSTHDR(&msg);
  cmptr->cmsg_len= CMSG_LEN(sizeof(int));
  cmptr->cmsg_level= SOL_SOCKET;
  cmptr->cmsg_type= SCM_RIGHTS;
  *((int*)CMSG_DATA(cmptr))= fd;
  
  msg.msg_name= NULL;
  msg.msg_namelen= 0;
  
  iov[0].iov_base= (char*)data;
  iov[0].iov_len= datalen;
  msg.msg_iov= iov;
  msg.msg_iovlen= 1;
  
  DEBUG("sending...");
  return sendmsg(sockfd, &msg, 0);
}


static bool
sendReply(int sockfd, const char *message, int fd)
{
  write(1, "", 1);
  return write_fd(sockfd, message, strlen(message)+1, fd);
}


static bool
sendError(int sockfd, const char *message)
{
  write(1, message, strlen(message)+1);
  return true;
}


static char *get_cnf_value(const char *path, const char *section, const char *option)
{
  FILE *file= fopen(path, "r");
  char *secstr;
  char *value= NULL;
  char buffer[1024];
  if (!file)
    return NULL;
  
  secstr= malloc(strlen(option)+8);
  sprintf(secstr, "[%s]", section); 
  
  while (fgets(buffer, sizeof(buffer), file))
  {
    char *end= buffer+strlen(buffer)-1;
    while (isspace(*end)) --end;
    end++;
    *end= 0;
    if (strcmp(secstr, buffer)==0)
    {
      while (fgets(buffer, sizeof(buffer), file))
      {
        end= buffer+strlen(buffer)-1;
        while (isspace(*end)) --end;
        end++;
        *end= 0;
        if (strncmp(buffer, option, strlen(option))==0 && 
            (isspace(buffer[strlen(option)]) || buffer[strlen(option)]=='='))
        {
          char *p= strchr(buffer, '=');
          if (!p)
            continue;
          p++;
          while (isspace(*p)) ++p;
          value= strdup(p);
          break;
        }
      }
      break;
    }
  }
  free(secstr);
  fclose(file);
  
  return value;
}


static char *
guessLogPath(int type)
{
  const char *suf;
  char host[1024];
  char buffer[1024];
  char *value;
  char *ptr;
  
  switch (type)
  {
    case 0: // error
      value= get_cnf_value("/etc/my.cnf", "mysqld", "log-error");
      if (value && *value)
        return value;
        suf= ".err";
      break;
    case 1: // general
      value= get_cnf_value("/etc/my.cnf", "mysqld", "log");
      if (value && *value)
        return value;
        suf= ".log";
      break;
    case 2: // slow
      value= get_cnf_value("/etc/my.cnf", "mysqld", "log-slow-queries");
      if (value && *value)
        return value;		
        suf= "-slow.log";
      break;
    default:
      return NULL;
  }
  
  if (gethostname(host, sizeof(host)) < 0)
  {
    DEBUG("can't determine hostname");
    return NULL;
  }
  
  snprintf(buffer, sizeof(buffer), "/usr/local/mysql/data/%s%s", host, suf);
  if (close(open(buffer, O_RDONLY)) >= 0)
    return strdup(buffer);
  
  snprintf(buffer, sizeof(buffer), "/var/log/mysql/%s%s", host, suf);
  if (close(open(buffer, O_RDONLY)) >= 0)
    return strdup(buffer);
  
  snprintf(buffer, sizeof(buffer), "/var/log/mysql%s", suf);
  if (close(open(buffer, O_RDONLY)) >= 0)
    return strdup(buffer);
  
  ptr= strrchr(host, '.');
  if (ptr && strcmp(ptr, ".local")==0)
  {
    *ptr= 0;

    snprintf(buffer, sizeof(buffer), "/usr/local/mysql/data/%s%s", host, suf);
    if (close(open(buffer, O_RDONLY)) >= 0)
      return strdup(buffer);

    snprintf(buffer, sizeof(buffer), "/var/log/mysql/%s%s", host, suf);
    if (close(open(buffer, O_RDONLY)) >= 0)
      return strdup(buffer);    
  }
  
  return NULL;
}


static bool doOpenLog(int sockfd, int n)
{
  char *fn= guessLogPath(n);
  int fd;
  
  DEBUG("trying to open %s  %i", fn, sockfd);
  
  if (!fn)
    return sendError(sockfd, "Can't locate log file.");
  
  fd= open(fn, O_RDONLY);
  if (fd < 0)
    return sendError(sockfd, strerror(errno));
  else
    return sendReply(sockfd, fn, fd);
}


bool performOpenCommand(const MAHelperCommand *cmd, int sockfd)
{
  int fd;
  char *msg;
  char new_path[sizeof(MYSQL_CONFIG_FILE)+10];
  char bak_path[sizeof(MYSQL_CONFIG_FILE)+10];
  
  sprintf(new_path, "%s.new", MYSQL_CONFIG_FILE);
  sprintf(bak_path, "%s.old", MYSQL_CONFIG_FILE);
  
  switch (cmd->authorizedCommandId)
  {
    case MAHelperOpenMyCnf:
    {
      DEBUG("opening config file ro");
      fd= open(new_path, O_RDONLY, 0644);
      if (fd < 0)
      {
        msg= strerror(errno);
        
        DEBUG("Could not open file '%s': %s", new_path, msg);
        return sendError(sockfd, msg);
      }
      else
        sendReply(sockfd, new_path, fd);
      return true;
    }
      
      // Create temporary file where conf data will be stored until it's
      // renamed to my.cnf
    case MAHelperOpenNewMyCnf: // this must match working of myx_update_mysql_cnf_filef
    {
      DEBUG("opening config file");
      fd= open(new_path, O_RDWR|O_CREAT, 0644);
      
      if (fd < 0)
      {
        msg= strerror(errno);
        DEBUG("Could not open file '%s': %s", new_path, msg);
        return sendError(sockfd, msg);
      }
      else
        return sendReply(sockfd, new_path, fd);
    }
      
      // Backup current my.cnf file to my.cnf.old and 
    case MAHelperCommitMyCnf: // this must match working of myx_update_mysql_cnf_filef
    {
      if (access(MYSQL_CONFIG_FILE, F_OK)==0 &&
          rename(MYSQL_CONFIG_FILE, bak_path) < 0)
      {
        char message[1024];
        DEBUG("failed backuping file");		
        sprintf(message, "Could not create backup of file %s:%s",
                MYSQL_CONFIG_FILE, strerror(errno));		
        return sendError(sockfd, message);
      }
      
      if (rename(new_path, MYSQL_CONFIG_FILE) < 0)
      {
        char message[1024];
        DEBUG("failed renaming to my.cnf");
        sprintf(message, "Could not rename temp. configuration file to %s:%s",
                MYSQL_CONFIG_FILE, strerror(errno));
        return sendError(sockfd, message);
      }
      
      return sendError(sockfd, "OK");
    } 
      
    case MAHelperOpenErrorLog:
      return doOpenLog(sockfd,0);
    case MAHelperOpenGeneralLog:
      return doOpenLog(sockfd,1);
    case MAHelperOpenSlowLog:
      return doOpenLog(sockfd,2);
      
    default:
      return false;
  }
}