summaryrefslogtreecommitdiff
path: root/extra/mysqld_safe_helper.c
blob: 5c50b2300d4ffdc896ba8dc4551181c0a2efacbb (plain)
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
#include <my_global.h>
#include <m_string.h>
#include <my_sys.h>
#include <my_pthread.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <stdlib.h>
#include <stdio.h>

void my_exit(int c)
{
  my_end(0);
  exit(c);
}

void do_usage(void)
{
  printf("Usage:\n"
         "  %s <user> log  <filename>\n"
         "  %s <user> exec <command> <args>\n",
         my_progname, my_progname);
  my_exit(1);
}

void do_log(const char *logfile)
{
  FILE *f;
  uchar buf[4096];
  int size;

  if (!logfile)
    do_usage();

  f= my_fopen(logfile, O_WRONLY|O_APPEND|O_CREAT, MYF(MY_WME));
  if (!f)
    my_exit(1);

  while ((size= my_fread(stdin, buf, sizeof(buf), MYF(MY_WME))) > 0)
    if ((int)my_fwrite(f, buf, size, MYF(MY_WME)) != size)
      my_exit(1);

  my_fclose(f, MYF(0));
  my_exit(0);
}

void do_exec(char *args[])
{
  if (!args[0])
    do_usage();

  my_end(0);
  execvp(args[0], args);
}

int main(int argc, char *argv[])
{
  struct passwd *user_info;
  MY_INIT(argv[0]);

  if (argc < 3)
    do_usage();

  user_info= my_check_user(argv[1], MYF(0));
  if (user_info ? my_set_user(argv[1], user_info, MYF(MY_WME))
                : my_errno == EINVAL)
    my_exit(1);

  if (strcmp(argv[2], "log") == 0)
    do_log(argv[3]);

  if (strcmp(argv[2], "exec") == 0)
    do_exec(argv+3);

  my_end(0);
  return 1;
}