Commit 12679f3871c941183b0979d6ca2ec53e6309fe12

Authored by Perry Werneck
1 parent 377115b6
Exists in master and in 1 other branch develop

If the session as an id try it first.

Showing 1 changed file with 103 additions and 65 deletions   Show diff stats
server/src/core/linux/start.c
... ... @@ -113,10 +113,7 @@ static gboolean
113 113  
114 114 }
115 115  
116   -void ipc3270_export_object(GObject *object, const char *name, GError **error) {
117   -
118   - char id;
119   - gchar *ptr;
  116 +static gboolean register_object(ipc3270 *ipc, const char *name, char id) {
120 117  
121 118 #pragma GCC diagnostic push
122 119 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
... ... @@ -127,92 +124,133 @@ void ipc3270_export_object(GObject *object, const char *name, GError **error) {
127 124 };
128 125 #pragma GCC diagnostic pop
129 126  
130   - ipc3270 * ipc = IPC3270(object);
  127 + GError * error = NULL;
  128 + gchar *ptr;
131 129  
132   - ipc->dbus.connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, error);
133   - if(*error) {
134   - g_message("Can't get session bus: %s",(*error)->message);
135   - return;
136   - }
  130 + g_autofree gchar *object_name = g_strdup_printf(PW3270_IPC_SESSION_BUS_NAME,name,id);
137 131  
138   - g_dbus_connection_set_exit_on_close(ipc->dbus.connection,FALSE);
  132 + for(ptr=object_name;*ptr;ptr++)
  133 + *ptr = g_ascii_tolower(*ptr);
139 134  
140   - for(id='a'; id < 'z' && !ipc->dbus.id && !*error; id++) {
  135 + debug("Requesting \"%s\"",object_name);
141 136  
142   - g_autofree gchar *object_name = g_strdup_printf(PW3270_IPC_SESSION_BUS_NAME,name,id);
  137 + // https://dbus.freedesktop.org/doc/dbus-specification.html
  138 + GVariant * response =
  139 + g_dbus_connection_call_sync (
  140 + ipc->dbus.connection,
  141 + DBUS_SERVICE_DBUS,
  142 + DBUS_PATH_DBUS,
  143 + DBUS_INTERFACE_DBUS,
  144 + "RequestName",
  145 + g_variant_new ("(su)", object_name, DBUS_NAME_FLAG_DO_NOT_QUEUE),
  146 + NULL,
  147 + G_DBUS_CALL_FLAGS_NONE,
  148 + -1,
  149 + NULL,
  150 + &error
  151 + );
143 152  
144   - for(ptr=object_name;*ptr;ptr++)
145   - *ptr = g_ascii_tolower(*ptr);
146 153  
147   - debug("Requesting \"%s\"",object_name);
  154 + if(error) {
148 155  
149   - // https://dbus.freedesktop.org/doc/dbus-specification.html
150   - GError *err = NULL;
  156 + g_message("Can't request \"%s\": %s",object_name,error->message);
  157 + g_error_free(error);
  158 + return FALSE;
151 159  
152   - GVariant * response =
153   - g_dbus_connection_call_sync (
154   - ipc->dbus.connection,
155   - DBUS_SERVICE_DBUS,
156   - DBUS_PATH_DBUS,
157   - DBUS_INTERFACE_DBUS,
158   - "RequestName",
159   - g_variant_new ("(su)", object_name, DBUS_NAME_FLAG_DO_NOT_QUEUE),
160   - NULL,
161   - G_DBUS_CALL_FLAGS_NONE,
162   - -1,
163   - NULL,
164   - &err
165   - );
  160 + }
166 161  
167   - if(err) {
  162 + if(!response) {
168 163  
169   - g_message("Can't request \"%s\": %s",object_name,err->message);
170   - g_error_free(err);
171   - err = NULL;
  164 + g_message("Empty response when requesting \"%s\"",object_name);
  165 + return FALSE;
172 166  
173   - } else if(response) {
  167 + }
174 168  
175   - guint32 reply = 0;
176   - g_variant_get(response, "(u)", &reply);
177   - g_variant_unref(response);
  169 + guint32 reply = 0;
  170 + g_variant_get(response, "(u)", &reply);
  171 + g_variant_unref(response);
178 172  
179   - if(reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
  173 + if(reply == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
180 174  
181   - ipc->dbus.name = g_strdup(object_name);
182   - g_message("Got %s", ipc->dbus.name);
  175 + ipc->dbus.name = g_strdup(object_name);
  176 + lib3270_set_session_id(ipc->hSession, id);
  177 + g_message("Got %s", ipc->dbus.name);
183 178  
184   - lib3270_set_session_id(ipc->hSession, id);
  179 + // Introspection data for the service we are exporting
  180 + GString * introspection = g_string_new("<node><interface name='" PW3270_IPC_SESSION_INTERFACE_NAME "'>");
  181 + ipc3270_add_terminal_introspection(introspection);
  182 + g_string_append(introspection,"</interface></node>");
185 183  
186   - // Introspection data for the service we are exporting
187   - GString * introspection = g_string_new("<node><interface name='" PW3270_IPC_SESSION_INTERFACE_NAME "'>");
188   - ipc3270_add_terminal_introspection(introspection);
189   - g_string_append(introspection,"</interface></node>");
  184 + gchar * introspection_xml = g_string_free(introspection,FALSE);
190 185  
191   - gchar * introspection_xml = g_string_free(introspection,FALSE);
  186 + // debug("\n%s\n",introspection_xml);
192 187  
193   - // debug("\n%s\n",introspection_xml);
  188 + GDBusNodeInfo * introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
194 189  
195   - GDBusNodeInfo * introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
  190 + // Register object-id
  191 + ipc->dbus.id = g_dbus_connection_register_object (
  192 + ipc->dbus.connection,
  193 + PW3270_IPC_SESSION_OBJECT_PATH,
  194 + introspection_data->interfaces[0],
  195 + &interface_vtable,
  196 + ipc,
  197 + NULL,
  198 + &error
  199 + );
196 200  
197   - // Register object-id
198   - ipc->dbus.id = g_dbus_connection_register_object (
199   - ipc->dbus.connection,
200   - PW3270_IPC_SESSION_OBJECT_PATH,
201   - introspection_data->interfaces[0],
202   - &interface_vtable,
203   - ipc,
204   - NULL,
205   - error
206   - );
  201 + g_dbus_node_info_unref(introspection_data);
  202 + g_free(introspection_xml);
207 203  
208   - g_dbus_node_info_unref(introspection_data);
209   - g_free(introspection_xml);
  204 + if(error) {
210 205  
211   - return;
212   - }
  206 + g_message("Can't register object \"%s\": %s",object_name,error->message);
  207 + g_error_free(error);
  208 + return FALSE;
213 209  
214 210 }
215 211  
  212 + return TRUE;
  213 +
216 214 }
217 215  
  216 + return FALSE;
  217 +}
  218 +
  219 +void ipc3270_export_object(GObject *object, const char *name, GError **error) {
  220 +
  221 + ipc3270 * ipc = IPC3270(object);
  222 +
  223 + ipc->dbus.connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, error);
  224 + if(*error) {
  225 + g_message("Can't get session bus: %s",(*error)->message);
  226 + return;
  227 + }
  228 +
  229 + g_autofree gchar *basename = g_strdup(name);
  230 + {
  231 + gchar *ptr = strrchr(basename,':');
  232 + if(ptr)
  233 + *ptr = 0;
  234 + }
  235 +
  236 + g_dbus_connection_set_exit_on_close(ipc->dbus.connection,FALSE);
  237 +
  238 + char id = lib3270_get_session_id(ipc->hSession);
  239 +
  240 + if(id) {
  241 +
  242 + if(register_object(ipc,basename,id))
  243 + return;
  244 +
  245 + }
  246 +
  247 + for(id='a'; id < 'z' && !ipc->dbus.id && !*error; id++) {
  248 +
  249 + if(register_object(ipc,basename,id))
  250 + return;
  251 +
  252 + }
  253 +
  254 + g_message("Can't register IPC object for session \"%s\"",basename);
  255 +
218 256 }
... ...