- Add ability to reconnect to CUPS server after its reload (caused by

cupsAdminSetServerSettings() or cupsPutFile())
This commit is contained in:
mkasik 2009-02-13 11:37:16 +00:00
parent bc1a7ab962
commit 423758b30c
2 changed files with 157 additions and 1 deletions

150
cph_reconnect.patch Normal file
View File

@ -0,0 +1,150 @@
--- src/cups.c 2009-02-13 11:55:38.000000000 +0100
+++ src/cups.c 2009-02-13 11:57:39.000000000 +0100
@@ -45,6 +45,9 @@
#include "cups.h"
+#define MAX_RECONNECT_ATTEMPTS 100
+#define RECONNECT_DELAY 100000
+
/*
getPrinters
getDests
@@ -114,6 +117,7 @@ struct CphCupsPrivate
http_t *connection;
ipp_status_t last_status;
char *internal_status;
+ gboolean reconnecting;
};
static GObject *cph_cups_constructor (GType type,
@@ -172,6 +176,31 @@ cph_cups_init (CphCups *cups)
cups->priv->connection = NULL;
cups->priv->last_status = IPP_OK;
cups->priv->internal_status = NULL;
+ cups->priv->reconnecting = FALSE;
+}
+
+gboolean
+cph_cups_reconnect (CphCups *cups)
+{
+ gint return_value = -1;
+ gint i;
+
+ cups->priv->reconnecting = TRUE;
+
+ for (i = 0; i < MAX_RECONNECT_ATTEMPTS; i++) {
+ return_value = httpReconnect (cups->priv->connection);
+ if (return_value == 0) {
+ break;
+ }
+ g_usleep (RECONNECT_DELAY);
+ }
+
+ cups->priv->reconnecting = FALSE;
+
+ if (return_value == 0)
+ return TRUE;
+ else
+ return FALSE;
}
static void
@@ -848,6 +877,10 @@ cph_cups_file_get (CphCups *cups,
const char *resource,
const char *filename)
{
+ struct stat file_stat;
+ uid_t uid = 0;
+ gid_t gid = 0;
+
g_return_val_if_fail (CPH_IS_CUPS (cups), FALSE);
if (!_cph_cups_is_resource_valid (cups, resource))
@@ -855,12 +888,31 @@ cph_cups_file_get (CphCups *cups,
if (!_cph_cups_is_filename_valid (cups, filename))
return FALSE;
+ stat (filename, &file_stat);
+ uid = file_stat.st_uid;
+ gid = file_stat.st_gid;
+
/* reset the internal status: we'll use the cups status */
_cph_cups_set_internal_status (cups, NULL);
-
cups->priv->last_status = cupsGetFile (cups->priv->connection,
resource, filename);
+ if (cups->priv->last_status != HTTP_OK) {
+ int fd;
+
+ if (cph_cups_reconnect (cups)) {
+
+ /* if cupsGetFile fail then filename is erased */
+ fd = open (filename, O_CREAT, S_IRUSR | S_IWUSR);
+ close (fd);
+ chown (filename, uid, gid);
+
+ _cph_cups_set_internal_status (cups, NULL);
+ cups->priv->last_status = cupsGetFile (cups->priv->connection,
+ resource, filename);
+ }
+ }
+
return cups->priv->last_status == HTTP_OK;
}
@@ -882,6 +934,9 @@ cph_cups_file_put (CphCups *cups,
cups->priv->last_status = cupsPutFile (cups->priv->connection,
resource, filename);
+ /* CUPS is being restarted, so we need to reconnect */
+ cph_cups_reconnect (cups);
+
return (cups->priv->last_status == HTTP_OK ||
cups->priv->last_status == HTTP_CREATED);
}
@@ -1613,6 +1668,9 @@ cph_cups_server_set_settings (CphCups
retval = cupsAdminSetServerSettings (cups->priv->connection,
num_settings, cups_settings);
+ /* CUPS is being restarted, so we need to reconnect */
+ cph_cups_reconnect (cups);
+
cupsFreeOptions (num_settings, cups_settings);
if (retval == 0) {
--- src/cups-pk-helper-mechanism.c 2009-02-13 11:55:38.000000000 +0100
+++ src/cups-pk-helper-mechanism.c 2009-02-13 11:55:52.000000000 +0100
@@ -60,7 +60,8 @@
static gboolean
do_exit (gpointer user_data)
{
- g_object_unref (CPH_MECHANISM (user_data));
+ if (user_data != NULL)
+ g_object_unref (CPH_MECHANISM (user_data));
exit (0);
@@ -540,10 +541,6 @@ cph_mechanism_file_put (CphMechanism
ret = cph_cups_file_put (mechanism->priv->cups, resource, filename);
_cph_mechanism_return_error (mechanism, context, !ret);
- /* TODO: this is a workaround for bnc#447422
- * https://bugzilla.novell.com/show_bug.cgi?id=447422 */
- do_exit (NULL);
-
return TRUE;
}
--- src/org.opensuse.cupspkhelper.mechanism.policy.in 2009-02-13 11:55:38.000000000 +0100
+++ src/org.opensuse.cupspkhelper.mechanism.policy.in 2009-02-13 11:55:52.000000000 +0100
@@ -114,7 +114,7 @@
</action>
<action id="org.opensuse.cupspkhelper.mechanism.job-set-hold-until-another-owner">
- <_description>Set hold-until time of a job owned by another</_description>
+ <_description>Set hold-until time of a job owned by another user</_description>
<_message>Privileges are required to set hold-until time of a job owned by another user.</_message>
<defaults>
<allow_inactive>no</allow_inactive>

View File

@ -1,6 +1,6 @@
Name: cups-pk-helper Name: cups-pk-helper
Version: 0.0.3 Version: 0.0.3
Release: 3%{?dist} Release: 4%{?dist}
Summary: A helper that makes system-config-printer use PolicyKit Summary: A helper that makes system-config-printer use PolicyKit
Group: System Environment/Base Group: System Environment/Base
@ -11,6 +11,7 @@ Source0: http://www.vuntz.net/download/cups-pk-helper/cups-pk-helper-%{ve
Patch0: dependencies.patch Patch0: dependencies.patch
Patch1: pk_order.patch Patch1: pk_order.patch
Patch2: pk_jobs.patch Patch2: pk_jobs.patch
Patch3: cph_reconnect.patch
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
@ -36,6 +37,7 @@ interfaces available under control of PolicyKit.
%patch0 -p0 -b .dependencies %patch0 -p0 -b .dependencies
%patch1 -p0 -b .pk-order %patch1 -p0 -b .pk-order
%patch2 -p0 -b .pk-jobs %patch2 -p0 -b .pk-jobs
%patch3 -p0 -b .cph-reconnect
%build %build
%configure %configure
@ -62,6 +64,10 @@ rm -rf $RPM_BUILD_ROOT
%changelog %changelog
* Fri Feb 13 2009 Marek Kasik <mkasik@redhat.com> 0.0.3-4
- Add ability to reconnect to CUPS server after its reload
(caused by cupsAdminSetServerSettings() or cupsPutFile())
* Tue Jan 28 2009 Marek Kasik <mkasik@redhat.com> 0.0.3-3 * Tue Jan 28 2009 Marek Kasik <mkasik@redhat.com> 0.0.3-3
- Add functions for handling jobs (JobRestart, JobCancel, JobSetHoldUntil) - Add functions for handling jobs (JobRestart, JobCancel, JobSetHoldUntil)