From 423758b30c80346f9777a28aaa1d070e8aeb5917 Mon Sep 17 00:00:00 2001 From: mkasik Date: Feb 13 2009 11:37:16 +0000 Subject: - Add ability to reconnect to CUPS server after its reload (caused by cupsAdminSetServerSettings() or cupsPutFile()) --- diff --git a/cph_reconnect.patch b/cph_reconnect.patch new file mode 100644 index 0000000..2573f18 --- /dev/null +++ b/cph_reconnect.patch @@ -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 @@ + + + +- <_description>Set hold-until time of a job owned by another ++ <_description>Set hold-until time of a job owned by another user + <_message>Privileges are required to set hold-until time of a job owned by another user. + + no diff --git a/cups-pk-helper.spec b/cups-pk-helper.spec index 5e9fdac..c2bac48 100644 --- a/cups-pk-helper.spec +++ b/cups-pk-helper.spec @@ -1,6 +1,6 @@ Name: cups-pk-helper Version: 0.0.3 -Release: 3%{?dist} +Release: 4%{?dist} Summary: A helper that makes system-config-printer use PolicyKit Group: System Environment/Base @@ -11,6 +11,7 @@ Source0: http://www.vuntz.net/download/cups-pk-helper/cups-pk-helper-%{ve Patch0: dependencies.patch Patch1: pk_order.patch Patch2: pk_jobs.patch +Patch3: cph_reconnect.patch BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) @@ -36,6 +37,7 @@ interfaces available under control of PolicyKit. %patch0 -p0 -b .dependencies %patch1 -p0 -b .pk-order %patch2 -p0 -b .pk-jobs +%patch3 -p0 -b .cph-reconnect %build %configure @@ -62,6 +64,10 @@ rm -rf $RPM_BUILD_ROOT %changelog +* Fri Feb 13 2009 Marek Kasik 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 0.0.3-3 - Add functions for handling jobs (JobRestart, JobCancel, JobSetHoldUntil)