summaryrefslogtreecommitdiff
path: root/server-tools/instance-manager/mysqlmanager.cc
blob: bde5cf0ccda5cb0398bc5acefb4822385dbcc001 (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
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
/* Copyright (c) 2003-2007 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */

#include <my_global.h>
#include <my_dir.h>
#include <my_sys.h>

#include <string.h>

#ifndef __WIN__
#include <pwd.h>
#include <grp.h>
#endif

#include "angel.h"
#include "log.h"
#include "manager.h"
#include "options.h"
#include "user_management_commands.h"

#ifdef __WIN__
#include "IMService.h"
#endif


/*
  Instance Manager consists of two processes: the angel process (IM-angel),
  and the manager process (IM-main). Responsibilities of IM-angel is to
  monitor IM-main, and restart it in case of failure/shutdown. IM-angel is
  started only if startup option '--run-as-service' is provided.

  IM-main consists of several subsystems (thread sets):

    - the signal handling thread

      The signal thread handles user signals and propagates them to the
      other threads. All other threads are accounted in the signal handler
      thread Thread Registry.

    - the listener

      The listener listens to all sockets. There is a listening socket for
      each subsystem (TCP/IP, UNIX socket).

    - mysql subsystem

      Instance Manager acts like an ordinary MySQL Server, but with very
      restricted command set. Each MySQL client connection is handled in a
      separate thread. All MySQL client connections threads constitute
      mysql subsystem.
*/

static int main_impl(int argc, char *argv[]);

#ifndef __WIN__
static struct passwd *check_user();
static bool switch_user();
#endif


/************************************************************************/
/**
  The entry point.
*************************************************************************/

int main(int argc, char *argv[])
{
  int return_value;

  puts("\n"
       "WARNING: This program is deprecated and will be removed in 6.0.\n");

  /* Initialize. */

  MY_INIT(argv[0]);
  log_init();
  umask(0117);
  srand((uint) time(0));

  /* Main function. */

  log_info("IM: started.");

  return_value= main_impl(argc, argv);

  log_info("IM: finished.");

  /* Cleanup. */

  Options::cleanup();
  my_end(0);

  return return_value;
}


/************************************************************************/
/**
  Instance Manager main functionality.
*************************************************************************/

int main_impl(int argc, char *argv[])
{
  int rc;

  if ((rc= Options::load(argc, argv)))
    return rc;

  if (Options::User_management::cmd)
    return Options::User_management::cmd->execute();

#ifndef __WIN__

  if (switch_user())
    return 1;

  return Options::Daemon::run_as_service ?
         Angel::main() :
         Manager::main();

#else

  return Options::Service::stand_alone ?
         Manager::main() :
         IMService::main();

#endif
}

/**************************************************************************
 OS-specific functions implementation.
**************************************************************************/

#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)

/************************************************************************/
/**
  Change to run as another user if started with --user.
*************************************************************************/

static struct passwd *check_user()
{
  const char *user= Options::Daemon::user;
  struct passwd *user_info;
  uid_t user_id= geteuid();

  /* Don't bother if we aren't superuser */
  if (user_id)
  {
    if (user)
    {
      /* Don't give a warning, if real user is same as given with --user */
      user_info= getpwnam(user);
      if ((!user_info || user_id != user_info->pw_uid))
        log_info("One can only use the --user switch if running as root\n");
    }
    return NULL;
  }
  if (!user)
  {
    log_info("You are running mysqlmanager as root! This might introduce security problems. It is safer to use --user option istead.\n");
    return NULL;
  }
  if (!strcmp(user, "root"))
    return NULL;                 /* Avoid problem with dynamic libraries */
 if (!(user_info= getpwnam(user)))
  {
    /* Allow a numeric uid to be used */
    const char *pos;
    for (pos= user; my_isdigit(default_charset_info, *pos); pos++)
    {}
    if (*pos)                                   /* Not numeric id */
      goto err;
    if (!(user_info= getpwuid(atoi(user))))
      goto err;
    else
      return user_info;
  }
  else
    return user_info;

err:
  log_error("Can not start under user '%s'.",
            (const char *) user);
  return NULL;
}


/************************************************************************/
/**
  Switch user.
*************************************************************************/

static bool switch_user()
{
  struct passwd *user_info= check_user();

  if (!user_info)
    return FALSE;

#ifdef HAVE_INITGROUPS
  initgroups(Options::Daemon::user, user_info->pw_gid);
#endif

  if (setgid(user_info->pw_gid) == -1)
  {
    log_error("setgid() failed");
    return TRUE;
  }

  if (setuid(user_info->pw_uid) == -1)
  {
    log_error("setuid() failed");
    return TRUE;
  }

  return FALSE;
}

#endif