00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <stdio.h>
00040
00041 #include "easydbus-core.h"
00042 #include "acquire_internal.h"
00043 #include "debug.h"
00044 #include "utils.h"
00045
00054 inline int
00055 easydbus_set_request_flags (int easydbus_flags)
00056 {
00057 int dbus_flags = 0;
00058
00059 if (easydbus_flags & EASYDBUS_CONN_ALLOW_REPLACEMENT) {
00060 EasyDbusDebug ("Set ALLOW_REPLACEMENT flag");
00061 dbus_flags |= DBUS_NAME_FLAG_ALLOW_REPLACEMENT;
00062 }
00063 if (easydbus_flags & EASYDBUS_CONN_REPLACE_EXISTING) {
00064 EasyDbusDebug ("Set REPLACE_EXISTING flag");
00065 dbus_flags |= DBUS_NAME_FLAG_REPLACE_EXISTING;
00066 }
00067 if (easydbus_flags & EASYDBUS_CONN_DO_NOT_QUEUE) {
00068 EasyDbusDebug ("Set DO_NOT_QUEUE flag");
00069 dbus_flags |= DBUS_NAME_FLAG_DO_NOT_QUEUE;
00070 }
00071
00072 return dbus_flags;
00073 }
00074
00089 inline int
00090 easydbus_acquire_name (struct EasyDbus_core *core,
00091 char *serviceName, int flags,
00092 DBusError * err)
00093 {
00094 int result = -1;
00095 int dbus_flags = 0;
00096
00097 EasyDbusDebug ("Request name");
00098
00099 dbus_flags = easydbus_set_request_flags (flags);
00100
00101
00102
00103 result = dbus_bus_request_name
00104 (core->conn, serviceName, dbus_flags, err);
00105
00106 if (dbus_error_is_set (err)) {
00107 EasyDbusDebug ("Error %s from "
00108 "dbus_bus_request_name: %s",
00109 err->name, err->message);
00110 return -1;
00111 }
00112
00113 switch (result) {
00114 case DBUS_REQUEST_NAME_REPLY_EXISTS:
00115 EasyDbusDebug ("Owner already exist");
00116 result = EASYDBUS_CONN_NAME_SERVICE_EXISTS;
00117 break;
00118 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
00119 EasyDbusDebug ("Service is primary onwner");
00120 result = EASYDBUS_CONN_NAME_SERVICE_PRIMARY;
00121 break;
00122 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
00123 EasyDbusDebug ("Service in queue");
00124 result = EASYDBUS_CONN_NAME_SERVICE_IN_QUEUE;
00125 break;
00126 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
00127 EasyDbusDebug ("Service already owner");
00128 result = EASYDBUS_CONN_NAME_SERVICE_PRIMARY;
00129 break;
00130 default:
00131 EasyDbusDebug ("Error! Returned value not managed");
00132 return -1;
00133 break;
00134 }
00135
00136 return result;
00137 }
00138
00151 char *
00152 easydbus_found_new_name (struct EasyDbus_core *core,
00153 char *oldName, int flags,
00154 DBusError * err)
00155 {
00156 char *service_name = NULL;
00157 int string_length = 0, string_length_max = 0;
00158 int i, result = -1;
00159 char buffer[5];
00160
00161
00162 string_length_max = strlen (oldName) + 1 + 3;
00163 string_length = strlen (oldName) + 1;
00164 service_name = (char *) malloc (string_length_max);
00165 if (!service_name)
00166 return NULL;
00167 memset (service_name, 0, string_length_max);
00168 memcpy (service_name, oldName, string_length);
00169
00170 EasyDbusDebug ("Check %s service name", oldName);
00171 for (i = 1; i < EASYDBUS_RENAMED_MAX_TEST; i++) {
00172 memset (buffer, 0, 5);
00173 snprintf (buffer, 5, "%d", i);
00174 memcpy (&service_name[string_length - 1],
00175 buffer, strlen (buffer) + 1);
00176 EasyDbusDebug ("Check %s service name", service_name);
00177 if (!dbus_bus_name_has_owner (core->conn, service_name, err)) {
00178 result = easydbus_acquire_name (core, service_name,
00179 flags, err);
00180 if (result == -1)
00181 goto error;
00182 return service_name;
00183 }
00184 }
00185
00186 error:
00187
00188 if (service_name != NULL)
00189 free (service_name);
00190
00191 return NULL;
00192 }
00193
00194