LCOV - code coverage report
Current view: top level - monitor - monitor_sbus.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 73 0.0 %
Date: 2012-11-29 Functions: 0 7 0.0 %
Branches: 0 153 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :    SSSD
       3                 :            : 
       4                 :            :    Data Provider Helpers
       5                 :            : 
       6                 :            :    Copyright (C) Stephen Gallagher <sgallagh@redhat.com>  2009
       7                 :            : 
       8                 :            :    This program is free software; you can redistribute it and/or modify
       9                 :            :    it under the terms of the GNU General Public License as published by
      10                 :            :    the Free Software Foundation; either version 3 of the License, or
      11                 :            :    (at your option) any later version.
      12                 :            : 
      13                 :            :    This program is distributed in the hope that it will be useful,
      14                 :            :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16                 :            :    GNU General Public License for more details.
      17                 :            : 
      18                 :            :    You should have received a copy of the GNU General Public License
      19                 :            :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20                 :            : */
      21                 :            : 
      22                 :            : /* Needed for res_init() */
      23                 :            : #include <netinet/in.h>
      24                 :            : #include <arpa/nameser.h>
      25                 :            : #include <resolv.h>
      26                 :            : 
      27                 :            : #include "util/util.h"
      28                 :            : #include "confdb/confdb.h"
      29                 :            : #include "sbus/sssd_dbus.h"
      30                 :            : #include "sbus/sbus_client.h"
      31                 :            : #include "monitor/monitor_interfaces.h"
      32                 :            : 
      33                 :          0 : int monitor_get_sbus_address(TALLOC_CTX *mem_ctx, char **address)
      34                 :            : {
      35                 :            :     char *default_address;
      36                 :            : 
      37                 :          0 :     *address = NULL;
      38                 :          0 :     default_address = talloc_asprintf(mem_ctx, "unix:path=%s/%s",
      39                 :            :                                       PIPE_PATH, SSSD_SERVICE_PIPE);
      40         [ #  # ]:          0 :     if (default_address == NULL) {
      41                 :            :         return ENOMEM;
      42                 :            :     }
      43                 :            : 
      44                 :          0 :     *address = default_address;
      45                 :          0 :     return EOK;
      46                 :            : }
      47                 :            : 
      48                 :          0 : static void id_callback(DBusPendingCall *pending, void *ptr)
      49                 :            : {
      50                 :            :     DBusMessage *reply;
      51                 :            :     DBusError dbus_error;
      52                 :            :     dbus_bool_t ret;
      53                 :            :     dbus_uint16_t mon_ver;
      54                 :            :     int type;
      55                 :            : 
      56                 :          0 :     dbus_error_init(&dbus_error);
      57                 :            : 
      58                 :          0 :     reply = dbus_pending_call_steal_reply(pending);
      59         [ #  # ]:          0 :     if (!reply) {
      60                 :            :         /* reply should never be null. This function shouldn't be called
      61                 :            :          * until reply is valid or timeout has occurred. If reply is NULL
      62                 :            :          * here, something is seriously wrong and we should bail out.
      63                 :            :          */
      64 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Severe error. A reply callback was called but no"
         [ #  # ][ #  # ]
                 [ #  # ]
      65                 :            :                   " reply was received and no timeout occurred\n"));
      66                 :            : 
      67                 :            :         /* FIXME: Destroy this connection ? */
      68                 :            :         goto done;
      69                 :            :     }
      70                 :            : 
      71                 :          0 :     type = dbus_message_get_type(reply);
      72      [ #  #  # ]:          0 :     switch (type) {
      73                 :            :     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
      74                 :          0 :         ret = dbus_message_get_args(reply, &dbus_error,
      75                 :            :                                     DBUS_TYPE_UINT16, &mon_ver,
      76                 :            :                                     DBUS_TYPE_INVALID);
      77         [ #  # ]:          0 :         if (!ret) {
      78 [ #  # ][ #  # ]:          0 :             DEBUG(1, ("Failed to parse message\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
      79         [ #  # ]:          0 :             if (dbus_error_is_set(&dbus_error)) dbus_error_free(&dbus_error);
      80                 :            :             /* FIXME: Destroy this connection ? */
      81                 :            :             goto done;
      82                 :            :         }
      83                 :            : 
      84 [ #  # ][ #  # ]:          0 :         DEBUG(4, ("Got id ack and version (%d) from Monitor\n", mon_ver));
         [ #  # ][ #  # ]
                 [ #  # ]
      85                 :            : 
      86                 :            :         break;
      87                 :            : 
      88                 :            :     case DBUS_MESSAGE_TYPE_ERROR:
      89 [ #  # ][ #  # ]:          0 :         DEBUG(0,("The Monitor returned an error [%s]\n",
         [ #  # ][ #  # ]
                 [ #  # ]
      90                 :            :                  dbus_message_get_error_name(reply)));
      91                 :            :         /* Falling through to default intentionally*/
      92                 :            :     default:
      93                 :            :         /*
      94                 :            :          * Timeout or other error occurred or something
      95                 :            :          * unexpected happened.
      96                 :            :          * It doesn't matter which, because either way we
      97                 :            :          * know that this connection isn't trustworthy.
      98                 :            :          * We'll destroy it now.
      99                 :            :          */
     100                 :            : 
     101                 :            :         /* FIXME: Destroy this connection ? */
     102                 :            :         break;
     103                 :            :     }
     104                 :            : 
     105                 :            : done:
     106                 :          0 :     dbus_pending_call_unref(pending);
     107                 :          0 :     dbus_message_unref(reply);
     108                 :          0 : }
     109                 :            : 
     110                 :          0 : int monitor_common_send_id(struct sbus_connection *conn,
     111                 :            :                            const char *name, uint16_t version)
     112                 :            : {
     113                 :            :     DBusMessage *msg;
     114                 :            :     dbus_bool_t ret;
     115                 :            :     int retval;
     116                 :            : 
     117                 :            :     /* create the message */
     118                 :          0 :     msg = dbus_message_new_method_call(NULL,
     119                 :            :                                        MON_SRV_PATH,
     120                 :            :                                        MON_SRV_INTERFACE,
     121                 :            :                                        MON_SRV_METHOD_REGISTER);
     122         [ #  # ]:          0 :     if (msg == NULL) {
     123 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Out of memory?!\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
     124                 :            :         return ENOMEM;
     125                 :            :     }
     126                 :            : 
     127 [ #  # ][ #  # ]:          0 :     DEBUG(4, ("Sending ID: (%s,%d)\n", name, version));
         [ #  # ][ #  # ]
                 [ #  # ]
     128                 :            : 
     129                 :          0 :     ret = dbus_message_append_args(msg,
     130                 :            :                                    DBUS_TYPE_STRING, &name,
     131                 :            :                                    DBUS_TYPE_UINT16, &version,
     132                 :            :                                    DBUS_TYPE_INVALID);
     133         [ #  # ]:          0 :     if (!ret) {
     134 [ #  # ][ #  # ]:          0 :         DEBUG(1, ("Failed to build message\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
     135                 :            :         return EIO;
     136                 :            :     }
     137                 :            : 
     138                 :          0 :     retval = sbus_conn_send(conn, msg, 3000,
     139                 :            :                             id_callback,
     140                 :            :                             NULL, NULL);
     141                 :          0 :     dbus_message_unref(msg);
     142                 :          0 :     return retval;
     143                 :            : }
     144                 :            : 
     145                 :          0 : int monitor_common_pong(DBusMessage *message,
     146                 :            :                         struct sbus_connection *conn)
     147                 :            : {
     148                 :            :     DBusMessage *reply;
     149                 :            :     dbus_bool_t ret;
     150                 :            : 
     151                 :          0 :     reply = dbus_message_new_method_return(message);
     152         [ #  # ]:          0 :     if (!reply) return ENOMEM;
     153                 :            : 
     154                 :          0 :     ret = dbus_message_append_args(reply, DBUS_TYPE_INVALID);
     155         [ #  # ]:          0 :     if (!ret) {
     156                 :          0 :         dbus_message_unref(reply);
     157                 :          0 :         return EIO;
     158                 :            :     }
     159                 :            : 
     160                 :            :     /* send reply back */
     161                 :          0 :     sbus_conn_send_reply(conn, reply);
     162                 :          0 :     dbus_message_unref(reply);
     163                 :            : 
     164                 :          0 :     return EOK;
     165                 :            : }
     166                 :            : 
     167                 :          0 : int monitor_common_res_init(DBusMessage *message,
     168                 :            :                             struct sbus_connection *conn)
     169                 :            : {
     170                 :            :     int ret;
     171                 :            : 
     172                 :          0 :     ret = res_init();
     173         [ #  # ]:          0 :     if(ret != 0) {
     174                 :            :         return EIO;
     175                 :            :     }
     176                 :            : 
     177                 :            :     /* Send an empty reply to acknowledge receipt */
     178                 :          0 :     return monitor_common_pong(message, conn);
     179                 :            : }
     180                 :            : 
     181                 :          0 : errno_t monitor_common_rotate_logs(struct confdb_ctx *confdb,
     182                 :            :                                    const char *conf_path)
     183                 :            : {
     184                 :            :     errno_t ret;
     185                 :          0 :     int old_debug_level = debug_level;
     186                 :            : 
     187                 :          0 :     ret = rotate_debug_files();
     188         [ #  # ]:          0 :     if (ret) {
     189                 :          0 :         sss_log(SSS_LOG_ALERT, "Could not rotate debug files! [%d][%s]\n",
     190                 :            :                                ret, strerror(ret));
     191                 :          0 :         return ret;
     192                 :            :     }
     193                 :            : 
     194                 :            :     /* Get new debug level from the confdb */
     195                 :          0 :     ret = confdb_get_int(confdb, conf_path,
     196                 :            :                          CONFDB_SERVICE_DEBUG_LEVEL,
     197                 :            :                          old_debug_level,
     198                 :            :                          &debug_level);
     199         [ #  # ]:          0 :     if (ret != EOK) {
     200 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Error reading from confdb (%d) [%s]\n",
         [ #  # ][ #  # ]
                 [ #  # ]
     201                 :            :                   ret, strerror(ret)));
     202                 :            :         /* Try to proceed with the old value */
     203                 :          0 :         debug_level = old_debug_level;
     204                 :            :     }
     205                 :            : 
     206         [ #  # ]:          0 :     if (debug_level != old_debug_level) {
     207 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Debug level changed to %#.4x\n", debug_level));
         [ #  # ][ #  # ]
                 [ #  # ]
     208                 :          0 :         debug_level = debug_convert_old_level(debug_level);
     209                 :            :     }
     210                 :            : 
     211                 :            :     return EOK;
     212                 :            : }
     213                 :            : 
     214                 :          0 : errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
     215                 :            :                          struct tevent_context *ev,
     216                 :            :                          struct sbus_interface *intf,
     217                 :            :                          const char *svc_name,
     218                 :            :                          uint16_t svc_version,
     219                 :            :                          void *pvt,
     220                 :            :                          struct sbus_connection **mon_conn)
     221                 :            : {
     222                 :            :     errno_t ret;
     223                 :            :     char *sbus_address;
     224                 :            :     struct sbus_connection *conn;
     225                 :            : 
     226                 :            :     /* Set up SBUS connection to the monitor */
     227                 :          0 :     ret = monitor_get_sbus_address(NULL, &sbus_address);
     228         [ #  # ]:          0 :     if (ret != EOK) {
     229 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Could not locate monitor address.\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
     230                 :            :         return ret;
     231                 :            :     }
     232                 :            : 
     233                 :          0 :     ret = sbus_client_init(mem_ctx, ev, sbus_address,
     234                 :            :                            intf, &conn,
     235                 :            :                            NULL, pvt);
     236         [ #  # ]:          0 :     if (ret != EOK) {
     237 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Failed to connect to monitor services.\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
     238                 :          0 :         talloc_free(sbus_address);
     239                 :            :         return ret;
     240                 :            :     }
     241                 :          0 :     talloc_free(sbus_address);
     242                 :            : 
     243                 :            :     /* Identify ourselves to the monitor */
     244                 :          0 :     ret = monitor_common_send_id(conn, svc_name, svc_version);
     245         [ #  # ]:          0 :     if (ret != EOK) {
     246 [ #  # ][ #  # ]:          0 :         DEBUG(0, ("Failed to identify to the monitor!\n"));
         [ #  # ][ #  # ]
                 [ #  # ]
     247                 :            :         return ret;
     248                 :            :     }
     249                 :            : 
     250                 :          0 :     *mon_conn = conn;
     251                 :            : 
     252                 :            :     return EOK;
     253                 :            : }

Generated by: LCOV version 1.9