1 /* Copyright 2010 Codership Oy <http://www.codership.com>
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
15 
16 #include "mariadb.h"
17 #include <mysqld.h>
18 #include "wsrep_priv.h"
19 #include "wsrep_utils.h"
20 
21 
_status_str(wsrep_member_status_t status)22 static const char* _status_str(wsrep_member_status_t status)
23 {
24   switch (status)
25   {
26   case WSREP_MEMBER_UNDEFINED: return "Undefined";
27   case WSREP_MEMBER_JOINER:    return "Joiner";
28   case WSREP_MEMBER_DONOR:     return "Donor";
29   case WSREP_MEMBER_JOINED:    return "Joined";
30   case WSREP_MEMBER_SYNCED:    return "Synced";
31   default:                     return "Error(?)";
32   }
33 }
34 
wsrep_notify_status(wsrep_member_status_t status,const wsrep_view_info_t * view)35 void wsrep_notify_status (wsrep_member_status_t    status,
36                           const wsrep_view_info_t* view)
37 {
38   if (!wsrep_notify_cmd || 0 == strlen(wsrep_notify_cmd))
39   {
40     WSREP_INFO("wsrep_notify_cmd is not defined, skipping notification.");
41     return;
42   }
43 
44   const long  cmd_len = (1 << 16) - 1;
45   char* cmd_ptr = (char*) my_malloc(cmd_len + 1, MYF(MY_WME));
46   long  cmd_off = 0;
47 
48   if (!cmd_ptr)
49     return; // the warning is in the log
50 
51   cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, "%s",
52                        wsrep_notify_cmd);
53 
54   if (status >= WSREP_MEMBER_UNDEFINED && status < WSREP_MEMBER_ERROR)
55   {
56     cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, " --status %s",
57                          _status_str(status));
58   }
59   else
60   {
61     /* here we preserve provider error codes */
62     cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off,
63                          " --status 'Error(%d)'", status);
64   }
65 
66   if (0 != view)
67   {
68     char uuid_str[40];
69 
70     wsrep_uuid_print (&view->state_id.uuid, uuid_str, sizeof(uuid_str));
71     cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off,
72                          " --uuid %s", uuid_str);
73 
74     cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off,
75                          " --primary %s", view->view >= 0 ? "yes" : "no");
76 
77     cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off,
78                          " --index %d", view->my_idx);
79 
80     if (view->memb_num)
81     {
82         cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off, " --members");
83 
84         for (int i = 0; i < view->memb_num; i++)
85         {
86             wsrep_uuid_print (&view->members[i].id, uuid_str, sizeof(uuid_str));
87             cmd_off += snprintf (cmd_ptr + cmd_off, cmd_len - cmd_off,
88                                  "%c%s/%s/%s", i > 0 ? ',' : ' ',
89                                  uuid_str, view->members[i].name,
90                                  view->members[i].incoming);
91         }
92     }
93   }
94 
95   if (cmd_off == cmd_len)
96   {
97     WSREP_ERROR("Notification buffer too short (%ld). Aborting notification.",
98                cmd_len);
99     my_free(cmd_ptr);
100     return;
101   }
102 
103   wsp::process p(cmd_ptr, "r", NULL);
104 
105   p.wait();
106   int err = p.error();
107 
108   if (err)
109   {
110     WSREP_ERROR("Notification command failed: %d (%s): \"%s\"",
111                 err, strerror(err), cmd_ptr);
112   }
113   my_free(cmd_ptr);
114 }
115 
116