[141] | 1 | /*
|
---|
| 2 | Unix SMB/Netbios implementation.
|
---|
| 3 | Version 3.0
|
---|
| 4 | printing backend routines
|
---|
| 5 | Copyright (C) Andrew Tridgell 1992-2000
|
---|
| 6 | Copyright (C) Jeremy Allison 2002
|
---|
| 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 | #include "includes.h"
|
---|
| 23 | #include "printing.h"
|
---|
| 24 |
|
---|
| 25 | extern SIG_ATOMIC_T got_sig_term;
|
---|
| 26 | extern SIG_ATOMIC_T reload_after_sighup;
|
---|
| 27 | extern struct current_user current_user;
|
---|
| 28 | extern userdom_struct current_user_info;
|
---|
| 29 |
|
---|
| 30 | /* Current printer interface */
|
---|
| 31 | static bool remove_from_jobs_changed(const char* sharename, uint32 jobid);
|
---|
| 32 |
|
---|
| 33 | /*
|
---|
| 34 | the printing backend revolves around a tdb database that stores the
|
---|
| 35 | SMB view of the print queue
|
---|
| 36 |
|
---|
| 37 | The key for this database is a jobid - a internally generated number that
|
---|
| 38 | uniquely identifies a print job
|
---|
| 39 |
|
---|
| 40 | reading the print queue involves two steps:
|
---|
| 41 | - possibly running lpq and updating the internal database from that
|
---|
| 42 | - reading entries from the database
|
---|
| 43 |
|
---|
| 44 | jobids are assigned when a job starts spooling.
|
---|
| 45 | */
|
---|
| 46 |
|
---|
| 47 | static TDB_CONTEXT *rap_tdb;
|
---|
| 48 | static uint16 next_rap_jobid;
|
---|
| 49 | struct rap_jobid_key {
|
---|
| 50 | fstring sharename;
|
---|
| 51 | uint32 jobid;
|
---|
| 52 | };
|
---|
| 53 |
|
---|
| 54 | /***************************************************************************
|
---|
| 55 | Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
|
---|
| 56 | bit RPC jobids.... JRA.
|
---|
| 57 | ***************************************************************************/
|
---|
| 58 |
|
---|
| 59 | uint16 pjobid_to_rap(const char* sharename, uint32 jobid)
|
---|
| 60 | {
|
---|
| 61 | uint16 rap_jobid;
|
---|
| 62 | TDB_DATA data, key;
|
---|
| 63 | struct rap_jobid_key jinfo;
|
---|
| 64 | uint8 buf[2];
|
---|
| 65 |
|
---|
| 66 | DEBUG(10,("pjobid_to_rap: called.\n"));
|
---|
| 67 |
|
---|
| 68 | if (!rap_tdb) {
|
---|
| 69 | /* Create the in-memory tdb. */
|
---|
| 70 | rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
|
---|
| 71 | if (!rap_tdb)
|
---|
| 72 | return 0;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | ZERO_STRUCT( jinfo );
|
---|
| 76 | fstrcpy( jinfo.sharename, sharename );
|
---|
| 77 | jinfo.jobid = jobid;
|
---|
| 78 | key.dptr = (uint8 *)&jinfo;
|
---|
| 79 | key.dsize = sizeof(jinfo);
|
---|
| 80 |
|
---|
| 81 | data = tdb_fetch(rap_tdb, key);
|
---|
| 82 | if (data.dptr && data.dsize == sizeof(uint16)) {
|
---|
| 83 | rap_jobid = SVAL(data.dptr, 0);
|
---|
| 84 | SAFE_FREE(data.dptr);
|
---|
| 85 | DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
|
---|
| 86 | (unsigned int)jobid, (unsigned int)rap_jobid));
|
---|
| 87 | return rap_jobid;
|
---|
| 88 | }
|
---|
| 89 | SAFE_FREE(data.dptr);
|
---|
| 90 | /* Not found - create and store mapping. */
|
---|
| 91 | rap_jobid = ++next_rap_jobid;
|
---|
| 92 | if (rap_jobid == 0)
|
---|
| 93 | rap_jobid = ++next_rap_jobid;
|
---|
| 94 | SSVAL(buf,0,rap_jobid);
|
---|
| 95 | data.dptr = buf;
|
---|
| 96 | data.dsize = sizeof(rap_jobid);
|
---|
| 97 | tdb_store(rap_tdb, key, data, TDB_REPLACE);
|
---|
| 98 | tdb_store(rap_tdb, data, key, TDB_REPLACE);
|
---|
| 99 |
|
---|
| 100 | DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
|
---|
| 101 | (unsigned int)jobid, (unsigned int)rap_jobid));
|
---|
| 102 | return rap_jobid;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | bool rap_to_pjobid(uint16 rap_jobid, fstring sharename, uint32 *pjobid)
|
---|
| 106 | {
|
---|
| 107 | TDB_DATA data, key;
|
---|
| 108 | uint8 buf[2];
|
---|
| 109 |
|
---|
| 110 | DEBUG(10,("rap_to_pjobid called.\n"));
|
---|
| 111 |
|
---|
| 112 | if (!rap_tdb)
|
---|
| 113 | return False;
|
---|
| 114 |
|
---|
| 115 | SSVAL(buf,0,rap_jobid);
|
---|
| 116 | key.dptr = buf;
|
---|
| 117 | key.dsize = sizeof(rap_jobid);
|
---|
| 118 | data = tdb_fetch(rap_tdb, key);
|
---|
| 119 | if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
|
---|
| 120 | {
|
---|
| 121 | struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
|
---|
| 122 | fstrcpy( sharename, jinfo->sharename );
|
---|
| 123 | *pjobid = jinfo->jobid;
|
---|
| 124 | DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
|
---|
| 125 | (unsigned int)*pjobid, (unsigned int)rap_jobid));
|
---|
| 126 | SAFE_FREE(data.dptr);
|
---|
| 127 | return True;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
|
---|
| 131 | (unsigned int)rap_jobid));
|
---|
| 132 | SAFE_FREE(data.dptr);
|
---|
| 133 | return False;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | static void rap_jobid_delete(const char* sharename, uint32 jobid)
|
---|
| 137 | {
|
---|
| 138 | TDB_DATA key, data;
|
---|
| 139 | uint16 rap_jobid;
|
---|
| 140 | struct rap_jobid_key jinfo;
|
---|
| 141 | uint8 buf[2];
|
---|
| 142 |
|
---|
| 143 | DEBUG(10,("rap_jobid_delete: called.\n"));
|
---|
| 144 |
|
---|
| 145 | if (!rap_tdb)
|
---|
| 146 | return;
|
---|
| 147 |
|
---|
| 148 | ZERO_STRUCT( jinfo );
|
---|
| 149 | fstrcpy( jinfo.sharename, sharename );
|
---|
| 150 | jinfo.jobid = jobid;
|
---|
| 151 | key.dptr = (uint8 *)&jinfo;
|
---|
| 152 | key.dsize = sizeof(jinfo);
|
---|
| 153 |
|
---|
| 154 | data = tdb_fetch(rap_tdb, key);
|
---|
| 155 | if (!data.dptr || (data.dsize != sizeof(uint16))) {
|
---|
| 156 | DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
|
---|
| 157 | (unsigned int)jobid ));
|
---|
| 158 | SAFE_FREE(data.dptr);
|
---|
| 159 | return;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
|
---|
| 163 | (unsigned int)jobid ));
|
---|
| 164 |
|
---|
| 165 | rap_jobid = SVAL(data.dptr, 0);
|
---|
| 166 | SAFE_FREE(data.dptr);
|
---|
| 167 | SSVAL(buf,0,rap_jobid);
|
---|
| 168 | data.dptr = buf;
|
---|
| 169 | data.dsize = sizeof(rap_jobid);
|
---|
| 170 | tdb_delete(rap_tdb, key);
|
---|
| 171 | tdb_delete(rap_tdb, data);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | static int get_queue_status(const char* sharename, print_status_struct *);
|
---|
| 175 |
|
---|
| 176 | /****************************************************************************
|
---|
| 177 | Initialise the printing backend. Called once at startup before the fork().
|
---|
| 178 | ****************************************************************************/
|
---|
| 179 |
|
---|
| 180 | bool print_backend_init(struct messaging_context *msg_ctx)
|
---|
| 181 | {
|
---|
| 182 | const char *sversion = "INFO/version";
|
---|
| 183 | int services = lp_numservices();
|
---|
| 184 | int snum;
|
---|
| 185 |
|
---|
| 186 | unlink(lock_path("printing.tdb"));
|
---|
| 187 | mkdir(lock_path("printing"),0755);
|
---|
| 188 |
|
---|
| 189 | /* handle a Samba upgrade */
|
---|
| 190 |
|
---|
| 191 | for (snum = 0; snum < services; snum++) {
|
---|
| 192 | struct tdb_print_db *pdb;
|
---|
| 193 | if (!lp_print_ok(snum))
|
---|
| 194 | continue;
|
---|
| 195 |
|
---|
| 196 | pdb = get_print_db_byname(lp_const_servicename(snum));
|
---|
| 197 | if (!pdb)
|
---|
| 198 | continue;
|
---|
| 199 | if (tdb_lock_bystring(pdb->tdb, sversion) == -1) {
|
---|
| 200 | DEBUG(0,("print_backend_init: Failed to open printer %s database\n", lp_const_servicename(snum) ));
|
---|
| 201 | release_print_db(pdb);
|
---|
| 202 | return False;
|
---|
| 203 | }
|
---|
| 204 | if (tdb_fetch_int32(pdb->tdb, sversion) != PRINT_DATABASE_VERSION) {
|
---|
| 205 | tdb_wipe_all(pdb->tdb);
|
---|
| 206 | tdb_store_int32(pdb->tdb, sversion, PRINT_DATABASE_VERSION);
|
---|
| 207 | }
|
---|
| 208 | tdb_unlock_bystring(pdb->tdb, sversion);
|
---|
| 209 | release_print_db(pdb);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | close_all_print_db(); /* Don't leave any open. */
|
---|
| 213 |
|
---|
| 214 | /* do NT print initialization... */
|
---|
| 215 | return nt_printing_init(msg_ctx);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /****************************************************************************
|
---|
| 219 | Shut down printing backend. Called once at shutdown to close the tdb.
|
---|
| 220 | ****************************************************************************/
|
---|
| 221 |
|
---|
| 222 | void printing_end(void)
|
---|
| 223 | {
|
---|
| 224 | close_all_print_db(); /* Don't leave any open. */
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /****************************************************************************
|
---|
| 228 | Retrieve the set of printing functions for a given service. This allows
|
---|
| 229 | us to set the printer function table based on the value of the 'printing'
|
---|
| 230 | service parameter.
|
---|
| 231 |
|
---|
| 232 | Use the generic interface as the default and only use cups interface only
|
---|
| 233 | when asked for (and only when supported)
|
---|
| 234 | ****************************************************************************/
|
---|
| 235 |
|
---|
| 236 | static struct printif *get_printer_fns_from_type( enum printing_types type )
|
---|
| 237 | {
|
---|
| 238 | struct printif *printer_fns = &generic_printif;
|
---|
| 239 |
|
---|
| 240 | #ifdef HAVE_CUPS
|
---|
| 241 | if ( type == PRINT_CUPS ) {
|
---|
| 242 | printer_fns = &cups_printif;
|
---|
| 243 | }
|
---|
| 244 | #endif /* HAVE_CUPS */
|
---|
| 245 |
|
---|
| 246 | #ifdef HAVE_IPRINT
|
---|
| 247 | if ( type == PRINT_IPRINT ) {
|
---|
| 248 | printer_fns = &iprint_printif;
|
---|
| 249 | }
|
---|
| 250 | #endif /* HAVE_IPRINT */
|
---|
| 251 |
|
---|
| 252 | printer_fns->type = type;
|
---|
| 253 |
|
---|
| 254 | return printer_fns;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | static struct printif *get_printer_fns( int snum )
|
---|
| 258 | {
|
---|
| 259 | return get_printer_fns_from_type( (enum printing_types)lp_printing(snum) );
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | /****************************************************************************
|
---|
| 264 | Useful function to generate a tdb key.
|
---|
| 265 | ****************************************************************************/
|
---|
| 266 |
|
---|
| 267 | static TDB_DATA print_key(uint32 jobid, uint32 *tmp)
|
---|
| 268 | {
|
---|
| 269 | TDB_DATA ret;
|
---|
| 270 |
|
---|
| 271 | SIVAL(tmp, 0, jobid);
|
---|
| 272 | ret.dptr = (uint8 *)tmp;
|
---|
| 273 | ret.dsize = sizeof(*tmp);
|
---|
| 274 | return ret;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /***********************************************************************
|
---|
| 278 | unpack a pjob from a tdb buffer
|
---|
| 279 | ***********************************************************************/
|
---|
| 280 |
|
---|
| 281 | int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
|
---|
| 282 | {
|
---|
| 283 | int len = 0;
|
---|
| 284 | int used;
|
---|
| 285 | uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
|
---|
| 286 | uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
|
---|
| 287 |
|
---|
| 288 | if ( !buf || !pjob )
|
---|
| 289 | return -1;
|
---|
| 290 |
|
---|
| 291 | len += tdb_unpack(buf+len, buflen-len, "dddddddddffff",
|
---|
| 292 | &pjpid,
|
---|
| 293 | &pjsysjob,
|
---|
| 294 | &pjfd,
|
---|
| 295 | &pjstarttime,
|
---|
| 296 | &pjstatus,
|
---|
| 297 | &pjsize,
|
---|
| 298 | &pjpage_count,
|
---|
| 299 | &pjspooled,
|
---|
| 300 | &pjsmbjob,
|
---|
| 301 | pjob->filename,
|
---|
| 302 | pjob->jobname,
|
---|
| 303 | pjob->user,
|
---|
| 304 | pjob->queuename);
|
---|
| 305 |
|
---|
| 306 | if ( len == -1 )
|
---|
| 307 | return -1;
|
---|
| 308 |
|
---|
| 309 | if ( (used = unpack_devicemode(&pjob->nt_devmode, buf+len, buflen-len)) == -1 )
|
---|
| 310 | return -1;
|
---|
| 311 |
|
---|
| 312 | len += used;
|
---|
| 313 |
|
---|
| 314 | pjob->pid = pjpid;
|
---|
| 315 | pjob->sysjob = pjsysjob;
|
---|
| 316 | pjob->fd = pjfd;
|
---|
| 317 | pjob->starttime = pjstarttime;
|
---|
| 318 | pjob->status = pjstatus;
|
---|
| 319 | pjob->size = pjsize;
|
---|
| 320 | pjob->page_count = pjpage_count;
|
---|
| 321 | pjob->spooled = pjspooled;
|
---|
| 322 | pjob->smbjob = pjsmbjob;
|
---|
| 323 |
|
---|
| 324 | return len;
|
---|
| 325 |
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /****************************************************************************
|
---|
| 329 | Useful function to find a print job in the database.
|
---|
| 330 | ****************************************************************************/
|
---|
| 331 |
|
---|
| 332 | static struct printjob *print_job_find(const char *sharename, uint32 jobid)
|
---|
| 333 | {
|
---|
| 334 | static struct printjob pjob;
|
---|
| 335 | uint32_t tmp;
|
---|
| 336 | TDB_DATA ret;
|
---|
| 337 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 338 |
|
---|
| 339 | DEBUG(10,("print_job_find: looking up job %u for share %s\n",
|
---|
| 340 | (unsigned int)jobid, sharename ));
|
---|
| 341 |
|
---|
| 342 | if (!pdb) {
|
---|
| 343 | return NULL;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | ret = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
|
---|
| 347 | release_print_db(pdb);
|
---|
| 348 |
|
---|
| 349 | if (!ret.dptr) {
|
---|
| 350 | DEBUG(10,("print_job_find: failed to find jobid %u.\n", (unsigned int)jobid ));
|
---|
| 351 | return NULL;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | if ( pjob.nt_devmode ) {
|
---|
| 355 | free_nt_devicemode( &pjob.nt_devmode );
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | ZERO_STRUCT( pjob );
|
---|
| 359 |
|
---|
| 360 | if ( unpack_pjob( ret.dptr, ret.dsize, &pjob ) == -1 ) {
|
---|
| 361 | DEBUG(10,("print_job_find: failed to unpack jobid %u.\n", (unsigned int)jobid ));
|
---|
| 362 | SAFE_FREE(ret.dptr);
|
---|
| 363 | return NULL;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | SAFE_FREE(ret.dptr);
|
---|
| 367 |
|
---|
| 368 | DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
|
---|
| 369 | (int)pjob.sysjob, (unsigned int)jobid ));
|
---|
| 370 |
|
---|
| 371 | return &pjob;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
| 374 | /* Convert a unix jobid to a smb jobid */
|
---|
| 375 |
|
---|
| 376 | struct unixjob_traverse_state {
|
---|
| 377 | int sysjob;
|
---|
| 378 | uint32 sysjob_to_jobid_value;
|
---|
| 379 | };
|
---|
| 380 |
|
---|
| 381 | static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
|
---|
| 382 | TDB_DATA data, void *private_data)
|
---|
| 383 | {
|
---|
| 384 | struct printjob *pjob;
|
---|
| 385 | struct unixjob_traverse_state *state =
|
---|
| 386 | (struct unixjob_traverse_state *)private_data;
|
---|
| 387 |
|
---|
| 388 | if (!data.dptr || data.dsize == 0)
|
---|
| 389 | return 0;
|
---|
| 390 |
|
---|
| 391 | pjob = (struct printjob *)data.dptr;
|
---|
| 392 | if (key.dsize != sizeof(uint32))
|
---|
| 393 | return 0;
|
---|
| 394 |
|
---|
| 395 | if (state->sysjob == pjob->sysjob) {
|
---|
| 396 | uint32 jobid = IVAL(key.dptr,0);
|
---|
| 397 |
|
---|
| 398 | state->sysjob_to_jobid_value = jobid;
|
---|
| 399 | return 1;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | return 0;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | /****************************************************************************
|
---|
| 406 | This is a *horribly expensive call as we have to iterate through all the
|
---|
| 407 | current printer tdb's. Don't do this often ! JRA.
|
---|
| 408 | ****************************************************************************/
|
---|
| 409 |
|
---|
| 410 | uint32 sysjob_to_jobid(int unix_jobid)
|
---|
| 411 | {
|
---|
| 412 | int services = lp_numservices();
|
---|
| 413 | int snum;
|
---|
| 414 | struct unixjob_traverse_state state;
|
---|
| 415 |
|
---|
| 416 | state.sysjob = unix_jobid;
|
---|
| 417 | state.sysjob_to_jobid_value = (uint32)-1;
|
---|
| 418 |
|
---|
| 419 | for (snum = 0; snum < services; snum++) {
|
---|
| 420 | struct tdb_print_db *pdb;
|
---|
| 421 | if (!lp_print_ok(snum))
|
---|
| 422 | continue;
|
---|
| 423 | pdb = get_print_db_byname(lp_const_servicename(snum));
|
---|
| 424 | if (!pdb) {
|
---|
| 425 | continue;
|
---|
| 426 | }
|
---|
| 427 | tdb_traverse(pdb->tdb, unixjob_traverse_fn, &state);
|
---|
| 428 | release_print_db(pdb);
|
---|
| 429 | if (state.sysjob_to_jobid_value != (uint32)-1)
|
---|
| 430 | return state.sysjob_to_jobid_value;
|
---|
| 431 | }
|
---|
| 432 | return (uint32)-1;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | /****************************************************************************
|
---|
| 436 | Send notifications based on what has changed after a pjob_store.
|
---|
| 437 | ****************************************************************************/
|
---|
| 438 |
|
---|
| 439 | static const struct {
|
---|
| 440 | uint32 lpq_status;
|
---|
| 441 | uint32 spoolss_status;
|
---|
| 442 | } lpq_to_spoolss_status_map[] = {
|
---|
| 443 | { LPQ_QUEUED, JOB_STATUS_QUEUED },
|
---|
| 444 | { LPQ_PAUSED, JOB_STATUS_PAUSED },
|
---|
| 445 | { LPQ_SPOOLING, JOB_STATUS_SPOOLING },
|
---|
| 446 | { LPQ_PRINTING, JOB_STATUS_PRINTING },
|
---|
| 447 | { LPQ_DELETING, JOB_STATUS_DELETING },
|
---|
| 448 | { LPQ_OFFLINE, JOB_STATUS_OFFLINE },
|
---|
| 449 | { LPQ_PAPEROUT, JOB_STATUS_PAPEROUT },
|
---|
| 450 | { LPQ_PRINTED, JOB_STATUS_PRINTED },
|
---|
| 451 | { LPQ_DELETED, JOB_STATUS_DELETED },
|
---|
| 452 | { LPQ_BLOCKED, JOB_STATUS_BLOCKED },
|
---|
| 453 | { LPQ_USER_INTERVENTION, JOB_STATUS_USER_INTERVENTION },
|
---|
| 454 | { -1, 0 }
|
---|
| 455 | };
|
---|
| 456 |
|
---|
| 457 | /* Convert a lpq status value stored in printing.tdb into the
|
---|
| 458 | appropriate win32 API constant. */
|
---|
| 459 |
|
---|
| 460 | static uint32 map_to_spoolss_status(uint32 lpq_status)
|
---|
| 461 | {
|
---|
| 462 | int i = 0;
|
---|
| 463 |
|
---|
| 464 | while (lpq_to_spoolss_status_map[i].lpq_status != -1) {
|
---|
| 465 | if (lpq_to_spoolss_status_map[i].lpq_status == lpq_status)
|
---|
| 466 | return lpq_to_spoolss_status_map[i].spoolss_status;
|
---|
| 467 | i++;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | return 0;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | static void pjob_store_notify(const char* sharename, uint32 jobid, struct printjob *old_data,
|
---|
| 474 | struct printjob *new_data)
|
---|
| 475 | {
|
---|
| 476 | bool new_job = False;
|
---|
| 477 |
|
---|
| 478 | if (!old_data)
|
---|
| 479 | new_job = True;
|
---|
| 480 |
|
---|
| 481 | /* Job attributes that can't be changed. We only send
|
---|
| 482 | notification for these on a new job. */
|
---|
| 483 |
|
---|
| 484 | /* ACHTUNG! Due to a bug in Samba's spoolss parsing of the
|
---|
| 485 | NOTIFY_INFO_DATA buffer, we *have* to send the job submission
|
---|
| 486 | time first or else we'll end up with potential alignment
|
---|
| 487 | errors. I don't think the systemtime should be spooled as
|
---|
| 488 | a string, but this gets us around that error.
|
---|
| 489 | --jerry (i'll feel dirty for this) */
|
---|
| 490 |
|
---|
| 491 | if (new_job) {
|
---|
| 492 | notify_job_submitted(sharename, jobid, new_data->starttime);
|
---|
| 493 | notify_job_username(sharename, jobid, new_data->user);
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | if (new_job || !strequal(old_data->jobname, new_data->jobname))
|
---|
| 497 | notify_job_name(sharename, jobid, new_data->jobname);
|
---|
| 498 |
|
---|
| 499 | /* Job attributes of a new job or attributes that can be
|
---|
| 500 | modified. */
|
---|
| 501 |
|
---|
| 502 | if (new_job || !strequal(old_data->jobname, new_data->jobname))
|
---|
| 503 | notify_job_name(sharename, jobid, new_data->jobname);
|
---|
| 504 |
|
---|
| 505 | if (new_job || old_data->status != new_data->status)
|
---|
| 506 | notify_job_status(sharename, jobid, map_to_spoolss_status(new_data->status));
|
---|
| 507 |
|
---|
| 508 | if (new_job || old_data->size != new_data->size)
|
---|
| 509 | notify_job_total_bytes(sharename, jobid, new_data->size);
|
---|
| 510 |
|
---|
| 511 | if (new_job || old_data->page_count != new_data->page_count)
|
---|
| 512 | notify_job_total_pages(sharename, jobid, new_data->page_count);
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | /****************************************************************************
|
---|
| 516 | Store a job structure back to the database.
|
---|
| 517 | ****************************************************************************/
|
---|
| 518 |
|
---|
| 519 | static bool pjob_store(const char* sharename, uint32 jobid, struct printjob *pjob)
|
---|
| 520 | {
|
---|
| 521 | uint32_t tmp;
|
---|
| 522 | TDB_DATA old_data, new_data;
|
---|
| 523 | bool ret = False;
|
---|
| 524 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 525 | uint8 *buf = NULL;
|
---|
| 526 | int len, newlen, buflen;
|
---|
| 527 |
|
---|
| 528 |
|
---|
| 529 | if (!pdb)
|
---|
| 530 | return False;
|
---|
| 531 |
|
---|
| 532 | /* Get old data */
|
---|
| 533 |
|
---|
| 534 | old_data = tdb_fetch(pdb->tdb, print_key(jobid, &tmp));
|
---|
| 535 |
|
---|
| 536 | /* Doh! Now we have to pack/unpack data since the NT_DEVICEMODE was added */
|
---|
| 537 |
|
---|
| 538 | newlen = 0;
|
---|
| 539 |
|
---|
| 540 | do {
|
---|
| 541 | len = 0;
|
---|
| 542 | buflen = newlen;
|
---|
| 543 | len += tdb_pack(buf+len, buflen-len, "dddddddddffff",
|
---|
| 544 | (uint32)pjob->pid,
|
---|
| 545 | (uint32)pjob->sysjob,
|
---|
| 546 | (uint32)pjob->fd,
|
---|
| 547 | (uint32)pjob->starttime,
|
---|
| 548 | (uint32)pjob->status,
|
---|
| 549 | (uint32)pjob->size,
|
---|
| 550 | (uint32)pjob->page_count,
|
---|
| 551 | (uint32)pjob->spooled,
|
---|
| 552 | (uint32)pjob->smbjob,
|
---|
| 553 | pjob->filename,
|
---|
| 554 | pjob->jobname,
|
---|
| 555 | pjob->user,
|
---|
| 556 | pjob->queuename);
|
---|
| 557 |
|
---|
| 558 | len += pack_devicemode(pjob->nt_devmode, buf+len, buflen-len);
|
---|
| 559 |
|
---|
| 560 | if (buflen != len) {
|
---|
| 561 | buf = (uint8 *)SMB_REALLOC(buf, len);
|
---|
| 562 | if (!buf) {
|
---|
| 563 | DEBUG(0,("pjob_store: failed to enlarge buffer!\n"));
|
---|
| 564 | goto done;
|
---|
| 565 | }
|
---|
| 566 | newlen = len;
|
---|
| 567 | }
|
---|
| 568 | } while ( buflen != len );
|
---|
| 569 |
|
---|
| 570 |
|
---|
| 571 | /* Store new data */
|
---|
| 572 |
|
---|
| 573 | new_data.dptr = buf;
|
---|
| 574 | new_data.dsize = len;
|
---|
| 575 | ret = (tdb_store(pdb->tdb, print_key(jobid, &tmp), new_data,
|
---|
| 576 | TDB_REPLACE) == 0);
|
---|
| 577 |
|
---|
| 578 | release_print_db(pdb);
|
---|
| 579 |
|
---|
| 580 | /* Send notify updates for what has changed */
|
---|
| 581 |
|
---|
| 582 | if ( ret ) {
|
---|
| 583 | struct printjob old_pjob;
|
---|
| 584 |
|
---|
| 585 | if ( old_data.dsize )
|
---|
| 586 | {
|
---|
| 587 | if ( unpack_pjob( old_data.dptr, old_data.dsize, &old_pjob ) != -1 )
|
---|
| 588 | {
|
---|
| 589 | pjob_store_notify( sharename, jobid, &old_pjob , pjob );
|
---|
| 590 | free_nt_devicemode( &old_pjob.nt_devmode );
|
---|
| 591 | }
|
---|
| 592 | }
|
---|
| 593 | else {
|
---|
| 594 | /* new job */
|
---|
| 595 | pjob_store_notify( sharename, jobid, NULL, pjob );
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | done:
|
---|
| 600 | SAFE_FREE( old_data.dptr );
|
---|
| 601 | SAFE_FREE( buf );
|
---|
| 602 |
|
---|
| 603 | return ret;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | /****************************************************************************
|
---|
| 607 | Remove a job structure from the database.
|
---|
| 608 | ****************************************************************************/
|
---|
| 609 |
|
---|
| 610 | void pjob_delete(const char* sharename, uint32 jobid)
|
---|
| 611 | {
|
---|
| 612 | uint32_t tmp;
|
---|
| 613 | struct printjob *pjob;
|
---|
| 614 | uint32 job_status = 0;
|
---|
| 615 | struct tdb_print_db *pdb;
|
---|
| 616 |
|
---|
| 617 | pdb = get_print_db_byname( sharename );
|
---|
| 618 |
|
---|
| 619 | if (!pdb)
|
---|
| 620 | return;
|
---|
| 621 |
|
---|
| 622 | pjob = print_job_find( sharename, jobid );
|
---|
| 623 |
|
---|
| 624 | if (!pjob) {
|
---|
| 625 | DEBUG(5, ("pjob_delete: we were asked to delete nonexistent job %u\n",
|
---|
| 626 | (unsigned int)jobid));
|
---|
| 627 | release_print_db(pdb);
|
---|
| 628 | return;
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | /* We must cycle through JOB_STATUS_DELETING and
|
---|
| 632 | JOB_STATUS_DELETED for the port monitor to delete the job
|
---|
| 633 | properly. */
|
---|
| 634 |
|
---|
| 635 | job_status = JOB_STATUS_DELETING|JOB_STATUS_DELETED;
|
---|
| 636 | notify_job_status(sharename, jobid, job_status);
|
---|
| 637 |
|
---|
| 638 | /* Remove from printing.tdb */
|
---|
| 639 |
|
---|
| 640 | tdb_delete(pdb->tdb, print_key(jobid, &tmp));
|
---|
| 641 | remove_from_jobs_changed(sharename, jobid);
|
---|
| 642 | release_print_db( pdb );
|
---|
| 643 | rap_jobid_delete(sharename, jobid);
|
---|
| 644 | }
|
---|
| 645 |
|
---|
| 646 | /****************************************************************************
|
---|
| 647 | Parse a file name from the system spooler to generate a jobid.
|
---|
| 648 | ****************************************************************************/
|
---|
| 649 |
|
---|
| 650 | static uint32 print_parse_jobid(char *fname)
|
---|
| 651 | {
|
---|
| 652 | int jobid;
|
---|
| 653 |
|
---|
| 654 | if (strncmp(fname,PRINT_SPOOL_PREFIX,strlen(PRINT_SPOOL_PREFIX)) != 0)
|
---|
| 655 | return (uint32)-1;
|
---|
| 656 | fname += strlen(PRINT_SPOOL_PREFIX);
|
---|
| 657 |
|
---|
| 658 | jobid = atoi(fname);
|
---|
| 659 | if (jobid <= 0)
|
---|
| 660 | return (uint32)-1;
|
---|
| 661 |
|
---|
| 662 | return (uint32)jobid;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | /****************************************************************************
|
---|
| 666 | List a unix job in the print database.
|
---|
| 667 | ****************************************************************************/
|
---|
| 668 |
|
---|
| 669 | static void print_unix_job(const char *sharename, print_queue_struct *q, uint32 jobid)
|
---|
| 670 | {
|
---|
| 671 | struct printjob pj, *old_pj;
|
---|
| 672 |
|
---|
| 673 | if (jobid == (uint32)-1)
|
---|
| 674 | jobid = q->job + UNIX_JOB_START;
|
---|
| 675 |
|
---|
| 676 | /* Preserve the timestamp on an existing unix print job */
|
---|
| 677 |
|
---|
| 678 | old_pj = print_job_find(sharename, jobid);
|
---|
| 679 |
|
---|
| 680 | ZERO_STRUCT(pj);
|
---|
| 681 |
|
---|
| 682 | pj.pid = (pid_t)-1;
|
---|
| 683 | pj.sysjob = q->job;
|
---|
| 684 | pj.fd = -1;
|
---|
| 685 | pj.starttime = old_pj ? old_pj->starttime : q->time;
|
---|
| 686 | pj.status = q->status;
|
---|
| 687 | pj.size = q->size;
|
---|
| 688 | pj.spooled = True;
|
---|
| 689 | fstrcpy(pj.filename, old_pj ? old_pj->filename : "");
|
---|
| 690 | if (jobid < UNIX_JOB_START) {
|
---|
| 691 | pj.smbjob = True;
|
---|
| 692 | fstrcpy(pj.jobname, old_pj ? old_pj->jobname : "Remote Downlevel Document");
|
---|
| 693 | } else {
|
---|
| 694 | pj.smbjob = False;
|
---|
| 695 | fstrcpy(pj.jobname, old_pj ? old_pj->jobname : q->fs_file);
|
---|
| 696 | }
|
---|
| 697 | fstrcpy(pj.user, old_pj ? old_pj->user : q->fs_user);
|
---|
| 698 | fstrcpy(pj.queuename, old_pj ? old_pj->queuename : sharename );
|
---|
| 699 |
|
---|
| 700 | pjob_store(sharename, jobid, &pj);
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 |
|
---|
| 704 | struct traverse_struct {
|
---|
| 705 | print_queue_struct *queue;
|
---|
| 706 | int qcount, snum, maxcount, total_jobs;
|
---|
| 707 | const char *sharename;
|
---|
| 708 | time_t lpq_time;
|
---|
| 709 | const char *lprm_command;
|
---|
| 710 | struct printif *print_if;
|
---|
| 711 | };
|
---|
| 712 |
|
---|
| 713 | /****************************************************************************
|
---|
| 714 | Utility fn to delete any jobs that are no longer active.
|
---|
| 715 | ****************************************************************************/
|
---|
| 716 |
|
---|
| 717 | static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
|
---|
| 718 | {
|
---|
| 719 | struct traverse_struct *ts = (struct traverse_struct *)state;
|
---|
| 720 | struct printjob pjob;
|
---|
| 721 | uint32 jobid;
|
---|
| 722 | int i = 0;
|
---|
| 723 |
|
---|
| 724 | if ( key.dsize != sizeof(jobid) )
|
---|
| 725 | return 0;
|
---|
| 726 |
|
---|
| 727 | jobid = IVAL(key.dptr, 0);
|
---|
| 728 | if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
|
---|
| 729 | return 0;
|
---|
| 730 | free_nt_devicemode( &pjob.nt_devmode );
|
---|
| 731 |
|
---|
| 732 |
|
---|
| 733 | if (!pjob.smbjob) {
|
---|
| 734 | /* remove a unix job if it isn't in the system queue any more */
|
---|
| 735 |
|
---|
| 736 | for (i=0;i<ts->qcount;i++) {
|
---|
| 737 | uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
|
---|
| 738 | if (jobid == u_jobid)
|
---|
| 739 | break;
|
---|
| 740 | }
|
---|
| 741 | if (i == ts->qcount) {
|
---|
| 742 | DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
|
---|
| 743 | (unsigned int)jobid ));
|
---|
| 744 | pjob_delete(ts->sharename, jobid);
|
---|
| 745 | return 0;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | /* need to continue the the bottom of the function to
|
---|
| 749 | save the correct attributes */
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | /* maybe it hasn't been spooled yet */
|
---|
| 753 | if (!pjob.spooled) {
|
---|
| 754 | /* if a job is not spooled and the process doesn't
|
---|
| 755 | exist then kill it. This cleans up after smbd
|
---|
| 756 | deaths */
|
---|
| 757 | if (!process_exists_by_pid(pjob.pid)) {
|
---|
| 758 | DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !process_exists (%u)\n",
|
---|
| 759 | (unsigned int)jobid, (unsigned int)pjob.pid ));
|
---|
| 760 | pjob_delete(ts->sharename, jobid);
|
---|
| 761 | } else
|
---|
| 762 | ts->total_jobs++;
|
---|
| 763 | return 0;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | /* this check only makes sense for jobs submitted from Windows clients */
|
---|
| 767 |
|
---|
| 768 | if ( pjob.smbjob ) {
|
---|
| 769 | for (i=0;i<ts->qcount;i++) {
|
---|
| 770 | uint32 curr_jobid;
|
---|
| 771 |
|
---|
| 772 | if ( pjob.status == LPQ_DELETED )
|
---|
| 773 | continue;
|
---|
| 774 |
|
---|
| 775 | curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
|
---|
| 776 |
|
---|
| 777 | if (jobid == curr_jobid) {
|
---|
| 778 |
|
---|
| 779 | /* try to clean up any jobs that need to be deleted */
|
---|
| 780 |
|
---|
| 781 | if ( pjob.status == LPQ_DELETING ) {
|
---|
| 782 | int result;
|
---|
| 783 |
|
---|
| 784 | result = (*(ts->print_if->job_delete))(
|
---|
| 785 | ts->sharename, ts->lprm_command, &pjob );
|
---|
| 786 |
|
---|
| 787 | if ( result != 0 ) {
|
---|
| 788 | /* if we can't delete, then reset the job status */
|
---|
| 789 | pjob.status = LPQ_QUEUED;
|
---|
| 790 | pjob_store(ts->sharename, jobid, &pjob);
|
---|
| 791 | }
|
---|
| 792 | else {
|
---|
| 793 | /* if we deleted the job, the remove the tdb record */
|
---|
| 794 | pjob_delete(ts->sharename, jobid);
|
---|
| 795 | pjob.status = LPQ_DELETED;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | break;
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | /* The job isn't in the system queue - we have to assume it has
|
---|
| 806 | completed, so delete the database entry. */
|
---|
| 807 |
|
---|
| 808 | if (i == ts->qcount) {
|
---|
| 809 |
|
---|
| 810 | /* A race can occur between the time a job is spooled and
|
---|
| 811 | when it appears in the lpq output. This happens when
|
---|
| 812 | the job is added to printing.tdb when another smbd
|
---|
| 813 | running print_queue_update() has completed a lpq and
|
---|
| 814 | is currently traversing the printing tdb and deleting jobs.
|
---|
| 815 | Don't delete the job if it was submitted after the lpq_time. */
|
---|
| 816 |
|
---|
| 817 | if (pjob.starttime < ts->lpq_time) {
|
---|
| 818 | DEBUG(10,("traverse_fn_delete: pjob %u deleted due to pjob.starttime (%u) < ts->lpq_time (%u)\n",
|
---|
| 819 | (unsigned int)jobid,
|
---|
| 820 | (unsigned int)pjob.starttime,
|
---|
| 821 | (unsigned int)ts->lpq_time ));
|
---|
| 822 | pjob_delete(ts->sharename, jobid);
|
---|
| 823 | } else
|
---|
| 824 | ts->total_jobs++;
|
---|
| 825 | return 0;
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | /* Save the pjob attributes we will store.
|
---|
| 829 | FIXME!!! This is the only place where queue->job
|
---|
| 830 | represents the SMB jobid --jerry */
|
---|
| 831 |
|
---|
| 832 | ts->queue[i].job = jobid;
|
---|
| 833 | ts->queue[i].size = pjob.size;
|
---|
| 834 | ts->queue[i].page_count = pjob.page_count;
|
---|
| 835 | ts->queue[i].status = pjob.status;
|
---|
| 836 | ts->queue[i].priority = 1;
|
---|
| 837 | ts->queue[i].time = pjob.starttime;
|
---|
| 838 | fstrcpy(ts->queue[i].fs_user, pjob.user);
|
---|
| 839 | fstrcpy(ts->queue[i].fs_file, pjob.jobname);
|
---|
| 840 |
|
---|
| 841 | ts->total_jobs++;
|
---|
| 842 |
|
---|
| 843 | return 0;
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 | /****************************************************************************
|
---|
| 847 | Check if the print queue has been updated recently enough.
|
---|
| 848 | ****************************************************************************/
|
---|
| 849 |
|
---|
| 850 | static void print_cache_flush(const char *sharename)
|
---|
| 851 | {
|
---|
| 852 | fstring key;
|
---|
| 853 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 854 |
|
---|
| 855 | if (!pdb)
|
---|
| 856 | return;
|
---|
| 857 | slprintf(key, sizeof(key)-1, "CACHE/%s", sharename);
|
---|
| 858 | tdb_store_int32(pdb->tdb, key, -1);
|
---|
| 859 | release_print_db(pdb);
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | /****************************************************************************
|
---|
| 863 | Check if someone already thinks they are doing the update.
|
---|
| 864 | ****************************************************************************/
|
---|
| 865 |
|
---|
| 866 | static pid_t get_updating_pid(const char *sharename)
|
---|
| 867 | {
|
---|
| 868 | fstring keystr;
|
---|
| 869 | TDB_DATA data, key;
|
---|
| 870 | pid_t updating_pid;
|
---|
| 871 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 872 |
|
---|
| 873 | if (!pdb)
|
---|
| 874 | return (pid_t)-1;
|
---|
| 875 | slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
|
---|
| 876 | key = string_tdb_data(keystr);
|
---|
| 877 |
|
---|
| 878 | data = tdb_fetch(pdb->tdb, key);
|
---|
| 879 | release_print_db(pdb);
|
---|
| 880 | if (!data.dptr || data.dsize != sizeof(pid_t)) {
|
---|
| 881 | SAFE_FREE(data.dptr);
|
---|
| 882 | return (pid_t)-1;
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | updating_pid = IVAL(data.dptr, 0);
|
---|
| 886 | SAFE_FREE(data.dptr);
|
---|
| 887 |
|
---|
| 888 | if (process_exists_by_pid(updating_pid))
|
---|
| 889 | return updating_pid;
|
---|
| 890 |
|
---|
| 891 | return (pid_t)-1;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 | /****************************************************************************
|
---|
| 895 | Set the fact that we're doing the update, or have finished doing the update
|
---|
| 896 | in the tdb.
|
---|
| 897 | ****************************************************************************/
|
---|
| 898 |
|
---|
| 899 | static void set_updating_pid(const fstring sharename, bool updating)
|
---|
| 900 | {
|
---|
| 901 | fstring keystr;
|
---|
| 902 | TDB_DATA key;
|
---|
| 903 | TDB_DATA data;
|
---|
| 904 | pid_t updating_pid = sys_getpid();
|
---|
| 905 | uint8 buffer[4];
|
---|
| 906 |
|
---|
| 907 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 908 |
|
---|
| 909 | if (!pdb)
|
---|
| 910 | return;
|
---|
| 911 |
|
---|
| 912 | slprintf(keystr, sizeof(keystr)-1, "UPDATING/%s", sharename);
|
---|
| 913 | key = string_tdb_data(keystr);
|
---|
| 914 |
|
---|
| 915 | DEBUG(5, ("set_updating_pid: %s updating lpq cache for print share %s\n",
|
---|
| 916 | updating ? "" : "not ",
|
---|
| 917 | sharename ));
|
---|
| 918 |
|
---|
| 919 | if ( !updating ) {
|
---|
| 920 | tdb_delete(pdb->tdb, key);
|
---|
| 921 | release_print_db(pdb);
|
---|
| 922 | return;
|
---|
| 923 | }
|
---|
| 924 |
|
---|
| 925 | SIVAL( buffer, 0, updating_pid);
|
---|
| 926 | data.dptr = buffer;
|
---|
| 927 | data.dsize = 4; /* we always assume this is a 4 byte value */
|
---|
| 928 |
|
---|
| 929 | tdb_store(pdb->tdb, key, data, TDB_REPLACE);
|
---|
| 930 | release_print_db(pdb);
|
---|
| 931 | }
|
---|
| 932 |
|
---|
| 933 | /****************************************************************************
|
---|
| 934 | Sort print jobs by submittal time.
|
---|
| 935 | ****************************************************************************/
|
---|
| 936 |
|
---|
| 937 | static int printjob_comp(print_queue_struct *j1, print_queue_struct *j2)
|
---|
| 938 | {
|
---|
| 939 | /* Silly cases */
|
---|
| 940 |
|
---|
| 941 | if (!j1 && !j2)
|
---|
| 942 | return 0;
|
---|
| 943 | if (!j1)
|
---|
| 944 | return -1;
|
---|
| 945 | if (!j2)
|
---|
| 946 | return 1;
|
---|
| 947 |
|
---|
| 948 | /* Sort on job start time */
|
---|
| 949 |
|
---|
| 950 | if (j1->time == j2->time)
|
---|
| 951 | return 0;
|
---|
| 952 | return (j1->time > j2->time) ? 1 : -1;
|
---|
| 953 | }
|
---|
| 954 |
|
---|
| 955 | /****************************************************************************
|
---|
| 956 | Store the sorted queue representation for later portmon retrieval.
|
---|
| 957 | Skip deleted jobs
|
---|
| 958 | ****************************************************************************/
|
---|
| 959 |
|
---|
| 960 | static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct *pts)
|
---|
| 961 | {
|
---|
| 962 | TDB_DATA data;
|
---|
| 963 | int max_reported_jobs = lp_max_reported_jobs(pts->snum);
|
---|
| 964 | print_queue_struct *queue = pts->queue;
|
---|
| 965 | size_t len;
|
---|
| 966 | size_t i;
|
---|
| 967 | unsigned int qcount;
|
---|
| 968 |
|
---|
| 969 | if (max_reported_jobs && (max_reported_jobs < pts->qcount))
|
---|
| 970 | pts->qcount = max_reported_jobs;
|
---|
| 971 | qcount = 0;
|
---|
| 972 |
|
---|
| 973 | /* Work out the size. */
|
---|
| 974 | data.dsize = 0;
|
---|
| 975 | data.dsize += tdb_pack(NULL, 0, "d", qcount);
|
---|
| 976 |
|
---|
| 977 | for (i = 0; i < pts->qcount; i++) {
|
---|
| 978 | if ( queue[i].status == LPQ_DELETED )
|
---|
| 979 | continue;
|
---|
| 980 |
|
---|
| 981 | qcount++;
|
---|
| 982 | data.dsize += tdb_pack(NULL, 0, "ddddddff",
|
---|
| 983 | (uint32)queue[i].job,
|
---|
| 984 | (uint32)queue[i].size,
|
---|
| 985 | (uint32)queue[i].page_count,
|
---|
| 986 | (uint32)queue[i].status,
|
---|
| 987 | (uint32)queue[i].priority,
|
---|
| 988 | (uint32)queue[i].time,
|
---|
| 989 | queue[i].fs_user,
|
---|
| 990 | queue[i].fs_file);
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | if ((data.dptr = (uint8 *)SMB_MALLOC(data.dsize)) == NULL)
|
---|
| 994 | return;
|
---|
| 995 |
|
---|
| 996 | len = 0;
|
---|
| 997 | len += tdb_pack(data.dptr + len, data.dsize - len, "d", qcount);
|
---|
| 998 | for (i = 0; i < pts->qcount; i++) {
|
---|
| 999 | if ( queue[i].status == LPQ_DELETED )
|
---|
| 1000 | continue;
|
---|
| 1001 |
|
---|
| 1002 | len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
|
---|
| 1003 | (uint32)queue[i].job,
|
---|
| 1004 | (uint32)queue[i].size,
|
---|
| 1005 | (uint32)queue[i].page_count,
|
---|
| 1006 | (uint32)queue[i].status,
|
---|
| 1007 | (uint32)queue[i].priority,
|
---|
| 1008 | (uint32)queue[i].time,
|
---|
| 1009 | queue[i].fs_user,
|
---|
| 1010 | queue[i].fs_file);
|
---|
| 1011 | }
|
---|
| 1012 |
|
---|
| 1013 | tdb_store(pdb->tdb, string_tdb_data("INFO/linear_queue_array"), data,
|
---|
| 1014 | TDB_REPLACE);
|
---|
| 1015 | SAFE_FREE(data.dptr);
|
---|
| 1016 | return;
|
---|
| 1017 | }
|
---|
| 1018 |
|
---|
| 1019 | static TDB_DATA get_jobs_changed_data(struct tdb_print_db *pdb)
|
---|
| 1020 | {
|
---|
| 1021 | TDB_DATA data;
|
---|
| 1022 |
|
---|
| 1023 | ZERO_STRUCT(data);
|
---|
| 1024 |
|
---|
| 1025 | data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
|
---|
| 1026 | if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0)) {
|
---|
| 1027 | SAFE_FREE(data.dptr);
|
---|
| 1028 | ZERO_STRUCT(data);
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | return data;
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | static void check_job_changed(const char *sharename, TDB_DATA data, uint32 jobid)
|
---|
| 1035 | {
|
---|
| 1036 | unsigned int i;
|
---|
| 1037 | unsigned int job_count = data.dsize / 4;
|
---|
| 1038 |
|
---|
| 1039 | for (i = 0; i < job_count; i++) {
|
---|
| 1040 | uint32 ch_jobid;
|
---|
| 1041 |
|
---|
| 1042 | ch_jobid = IVAL(data.dptr, i*4);
|
---|
| 1043 | if (ch_jobid == jobid)
|
---|
| 1044 | remove_from_jobs_changed(sharename, jobid);
|
---|
| 1045 | }
|
---|
| 1046 | }
|
---|
| 1047 |
|
---|
| 1048 | /****************************************************************************
|
---|
| 1049 | Check if the print queue has been updated recently enough.
|
---|
| 1050 | ****************************************************************************/
|
---|
| 1051 |
|
---|
| 1052 | static bool print_cache_expired(const char *sharename, bool check_pending)
|
---|
| 1053 | {
|
---|
| 1054 | fstring key;
|
---|
| 1055 | time_t last_qscan_time, time_now = time(NULL);
|
---|
| 1056 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 1057 | bool result = False;
|
---|
| 1058 |
|
---|
| 1059 | if (!pdb)
|
---|
| 1060 | return False;
|
---|
| 1061 |
|
---|
| 1062 | snprintf(key, sizeof(key), "CACHE/%s", sharename);
|
---|
| 1063 | last_qscan_time = (time_t)tdb_fetch_int32(pdb->tdb, key);
|
---|
| 1064 |
|
---|
| 1065 | /*
|
---|
| 1066 | * Invalidate the queue for 3 reasons.
|
---|
| 1067 | * (1). last queue scan time == -1.
|
---|
| 1068 | * (2). Current time - last queue scan time > allowed cache time.
|
---|
| 1069 | * (3). last queue scan time > current time + MAX_CACHE_VALID_TIME (1 hour by default).
|
---|
| 1070 | * This last test picks up machines for which the clock has been moved
|
---|
| 1071 | * forward, an lpq scan done and then the clock moved back. Otherwise
|
---|
| 1072 | * that last lpq scan would stay around for a loooong loooong time... :-). JRA.
|
---|
| 1073 | */
|
---|
| 1074 |
|
---|
| 1075 | if (last_qscan_time == ((time_t)-1)
|
---|
| 1076 | || (time_now - last_qscan_time) >= lp_lpqcachetime()
|
---|
| 1077 | || last_qscan_time > (time_now + MAX_CACHE_VALID_TIME))
|
---|
| 1078 | {
|
---|
| 1079 | uint32 u;
|
---|
| 1080 | time_t msg_pending_time;
|
---|
| 1081 |
|
---|
| 1082 | DEBUG(4, ("print_cache_expired: cache expired for queue %s "
|
---|
| 1083 | "(last_qscan_time = %d, time now = %d, qcachetime = %d)\n",
|
---|
| 1084 | sharename, (int)last_qscan_time, (int)time_now,
|
---|
| 1085 | (int)lp_lpqcachetime() ));
|
---|
| 1086 |
|
---|
| 1087 | /* check if another smbd has already sent a message to update the
|
---|
| 1088 | queue. Give the pending message one minute to clear and
|
---|
| 1089 | then send another message anyways. Make sure to check for
|
---|
| 1090 | clocks that have been run forward and then back again. */
|
---|
| 1091 |
|
---|
| 1092 | snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
|
---|
| 1093 |
|
---|
| 1094 | if ( check_pending
|
---|
| 1095 | && tdb_fetch_uint32( pdb->tdb, key, &u )
|
---|
| 1096 | && (msg_pending_time=u) > 0
|
---|
| 1097 | && msg_pending_time <= time_now
|
---|
| 1098 | && (time_now - msg_pending_time) < 60 )
|
---|
| 1099 | {
|
---|
| 1100 | DEBUG(4,("print_cache_expired: message already pending for %s. Accepting cache\n",
|
---|
| 1101 | sharename));
|
---|
| 1102 | goto done;
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
| 1105 | result = True;
|
---|
| 1106 | }
|
---|
| 1107 |
|
---|
| 1108 | done:
|
---|
| 1109 | release_print_db(pdb);
|
---|
| 1110 | return result;
|
---|
| 1111 | }
|
---|
| 1112 |
|
---|
| 1113 | /****************************************************************************
|
---|
| 1114 | main work for updating the lpq cahe for a printer queue
|
---|
| 1115 | ****************************************************************************/
|
---|
| 1116 |
|
---|
| 1117 | static void print_queue_update_internal( const char *sharename,
|
---|
| 1118 | struct printif *current_printif,
|
---|
| 1119 | char *lpq_command, char *lprm_command )
|
---|
| 1120 | {
|
---|
| 1121 | int i, qcount;
|
---|
| 1122 | print_queue_struct *queue = NULL;
|
---|
| 1123 | print_status_struct status;
|
---|
| 1124 | print_status_struct old_status;
|
---|
| 1125 | struct printjob *pjob;
|
---|
| 1126 | struct traverse_struct tstruct;
|
---|
| 1127 | TDB_DATA data, key;
|
---|
| 1128 | TDB_DATA jcdata;
|
---|
| 1129 | fstring keystr, cachestr;
|
---|
| 1130 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 1131 |
|
---|
| 1132 | if (!pdb) {
|
---|
| 1133 | return;
|
---|
| 1134 | }
|
---|
| 1135 |
|
---|
| 1136 | DEBUG(5,("print_queue_update_internal: printer = %s, type = %d, lpq command = [%s]\n",
|
---|
| 1137 | sharename, current_printif->type, lpq_command));
|
---|
| 1138 |
|
---|
| 1139 | /*
|
---|
| 1140 | * Update the cache time FIRST ! Stops others even
|
---|
| 1141 | * attempting to get the lock and doing this
|
---|
| 1142 | * if the lpq takes a long time.
|
---|
| 1143 | */
|
---|
| 1144 |
|
---|
| 1145 | slprintf(cachestr, sizeof(cachestr)-1, "CACHE/%s", sharename);
|
---|
| 1146 | tdb_store_int32(pdb->tdb, cachestr, (int)time(NULL));
|
---|
| 1147 |
|
---|
| 1148 | /* get the current queue using the appropriate interface */
|
---|
| 1149 | ZERO_STRUCT(status);
|
---|
| 1150 |
|
---|
| 1151 | qcount = (*(current_printif->queue_get))(sharename,
|
---|
| 1152 | current_printif->type,
|
---|
| 1153 | lpq_command, &queue, &status);
|
---|
| 1154 |
|
---|
| 1155 | DEBUG(3, ("print_queue_update_internal: %d job%s in queue for %s\n",
|
---|
| 1156 | qcount, (qcount != 1) ? "s" : "", sharename));
|
---|
| 1157 |
|
---|
| 1158 | /* Sort the queue by submission time otherwise they are displayed
|
---|
| 1159 | in hash order. */
|
---|
| 1160 |
|
---|
| 1161 | qsort(queue, qcount, sizeof(print_queue_struct),
|
---|
| 1162 | QSORT_CAST(printjob_comp));
|
---|
| 1163 |
|
---|
| 1164 | /*
|
---|
| 1165 | any job in the internal database that is marked as spooled
|
---|
| 1166 | and doesn't exist in the system queue is considered finished
|
---|
| 1167 | and removed from the database
|
---|
| 1168 |
|
---|
| 1169 | any job in the system database but not in the internal database
|
---|
| 1170 | is added as a unix job
|
---|
| 1171 |
|
---|
| 1172 | fill in any system job numbers as we go
|
---|
| 1173 | */
|
---|
| 1174 |
|
---|
| 1175 | jcdata = get_jobs_changed_data(pdb);
|
---|
| 1176 |
|
---|
| 1177 | for (i=0; i<qcount; i++) {
|
---|
| 1178 | uint32 jobid = print_parse_jobid(queue[i].fs_file);
|
---|
| 1179 |
|
---|
| 1180 | if (jobid == (uint32)-1) {
|
---|
| 1181 | /* assume its a unix print job */
|
---|
| 1182 | print_unix_job(sharename, &queue[i], jobid);
|
---|
| 1183 | continue;
|
---|
| 1184 | }
|
---|
| 1185 |
|
---|
| 1186 | /* we have an active SMB print job - update its status */
|
---|
| 1187 | pjob = print_job_find(sharename, jobid);
|
---|
| 1188 | if (!pjob) {
|
---|
| 1189 | /* err, somethings wrong. Probably smbd was restarted
|
---|
| 1190 | with jobs in the queue. All we can do is treat them
|
---|
| 1191 | like unix jobs. Pity. */
|
---|
| 1192 | print_unix_job(sharename, &queue[i], jobid);
|
---|
| 1193 | continue;
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
| 1196 | pjob->sysjob = queue[i].job;
|
---|
| 1197 |
|
---|
| 1198 | /* don't reset the status on jobs to be deleted */
|
---|
| 1199 |
|
---|
| 1200 | if ( pjob->status != LPQ_DELETING )
|
---|
| 1201 | pjob->status = queue[i].status;
|
---|
| 1202 |
|
---|
| 1203 | pjob_store(sharename, jobid, pjob);
|
---|
| 1204 |
|
---|
| 1205 | check_job_changed(sharename, jcdata, jobid);
|
---|
| 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | SAFE_FREE(jcdata.dptr);
|
---|
| 1209 |
|
---|
| 1210 | /* now delete any queued entries that don't appear in the
|
---|
| 1211 | system queue */
|
---|
| 1212 | tstruct.queue = queue;
|
---|
| 1213 | tstruct.qcount = qcount;
|
---|
| 1214 | tstruct.snum = -1;
|
---|
| 1215 | tstruct.total_jobs = 0;
|
---|
| 1216 | tstruct.lpq_time = time(NULL);
|
---|
| 1217 | tstruct.sharename = sharename;
|
---|
| 1218 | tstruct.lprm_command = lprm_command;
|
---|
| 1219 | tstruct.print_if = current_printif;
|
---|
| 1220 |
|
---|
| 1221 | tdb_traverse(pdb->tdb, traverse_fn_delete, (void *)&tstruct);
|
---|
| 1222 |
|
---|
| 1223 | /* Store the linearised queue, max jobs only. */
|
---|
| 1224 | store_queue_struct(pdb, &tstruct);
|
---|
| 1225 |
|
---|
| 1226 | SAFE_FREE(tstruct.queue);
|
---|
| 1227 |
|
---|
| 1228 | DEBUG(10,("print_queue_update_internal: printer %s INFO/total_jobs = %d\n",
|
---|
| 1229 | sharename, tstruct.total_jobs ));
|
---|
| 1230 |
|
---|
| 1231 | tdb_store_int32(pdb->tdb, "INFO/total_jobs", tstruct.total_jobs);
|
---|
| 1232 |
|
---|
| 1233 | get_queue_status(sharename, &old_status);
|
---|
| 1234 | if (old_status.qcount != qcount)
|
---|
| 1235 | DEBUG(10,("print_queue_update_internal: queue status change %d jobs -> %d jobs for printer %s\n",
|
---|
| 1236 | old_status.qcount, qcount, sharename));
|
---|
| 1237 |
|
---|
| 1238 | /* store the new queue status structure */
|
---|
| 1239 | slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
|
---|
| 1240 | key = string_tdb_data(keystr);
|
---|
| 1241 |
|
---|
| 1242 | status.qcount = qcount;
|
---|
| 1243 | data.dptr = (uint8 *)&status;
|
---|
| 1244 | data.dsize = sizeof(status);
|
---|
| 1245 | tdb_store(pdb->tdb, key, data, TDB_REPLACE);
|
---|
| 1246 |
|
---|
| 1247 | /*
|
---|
| 1248 | * Update the cache time again. We want to do this call
|
---|
| 1249 | * as little as possible...
|
---|
| 1250 | */
|
---|
| 1251 |
|
---|
| 1252 | slprintf(keystr, sizeof(keystr)-1, "CACHE/%s", sharename);
|
---|
| 1253 | tdb_store_int32(pdb->tdb, keystr, (int32)time(NULL));
|
---|
| 1254 |
|
---|
| 1255 | /* clear the msg pending record for this queue */
|
---|
| 1256 |
|
---|
| 1257 | snprintf(keystr, sizeof(keystr), "MSG_PENDING/%s", sharename);
|
---|
| 1258 |
|
---|
| 1259 | if ( !tdb_store_uint32( pdb->tdb, keystr, 0 ) ) {
|
---|
| 1260 | /* log a message but continue on */
|
---|
| 1261 |
|
---|
| 1262 | DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
|
---|
| 1263 | sharename));
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | release_print_db( pdb );
|
---|
| 1267 |
|
---|
| 1268 | return;
|
---|
| 1269 | }
|
---|
| 1270 |
|
---|
| 1271 | /****************************************************************************
|
---|
| 1272 | Update the internal database from the system print queue for a queue.
|
---|
| 1273 | obtain a lock on the print queue before proceeding (needed when mutiple
|
---|
| 1274 | smbd processes maytry to update the lpq cache concurrently).
|
---|
| 1275 | ****************************************************************************/
|
---|
| 1276 |
|
---|
| 1277 | static void print_queue_update_with_lock( const char *sharename,
|
---|
| 1278 | struct printif *current_printif,
|
---|
| 1279 | char *lpq_command, char *lprm_command )
|
---|
| 1280 | {
|
---|
| 1281 | fstring keystr;
|
---|
| 1282 | struct tdb_print_db *pdb;
|
---|
| 1283 |
|
---|
| 1284 | DEBUG(5,("print_queue_update_with_lock: printer share = %s\n", sharename));
|
---|
| 1285 | pdb = get_print_db_byname(sharename);
|
---|
| 1286 | if (!pdb)
|
---|
| 1287 | return;
|
---|
| 1288 |
|
---|
| 1289 | if ( !print_cache_expired(sharename, False) ) {
|
---|
| 1290 | DEBUG(5,("print_queue_update_with_lock: print cache for %s is still ok\n", sharename));
|
---|
| 1291 | release_print_db(pdb);
|
---|
| 1292 | return;
|
---|
| 1293 | }
|
---|
| 1294 |
|
---|
| 1295 | /*
|
---|
| 1296 | * Check to see if someone else is doing this update.
|
---|
| 1297 | * This is essentially a mutex on the update.
|
---|
| 1298 | */
|
---|
| 1299 |
|
---|
| 1300 | if (get_updating_pid(sharename) != -1) {
|
---|
| 1301 | release_print_db(pdb);
|
---|
| 1302 | return;
|
---|
| 1303 | }
|
---|
| 1304 |
|
---|
| 1305 | /* Lock the queue for the database update */
|
---|
| 1306 |
|
---|
| 1307 | slprintf(keystr, sizeof(keystr) - 1, "LOCK/%s", sharename);
|
---|
| 1308 | /* Only wait 10 seconds for this. */
|
---|
| 1309 | if (tdb_lock_bystring_with_timeout(pdb->tdb, keystr, 10) == -1) {
|
---|
| 1310 | DEBUG(0,("print_queue_update_with_lock: Failed to lock printer %s database\n", sharename));
|
---|
| 1311 | release_print_db(pdb);
|
---|
| 1312 | return;
|
---|
| 1313 | }
|
---|
| 1314 |
|
---|
| 1315 | /*
|
---|
| 1316 | * Ensure that no one else got in here.
|
---|
| 1317 | * If the updating pid is still -1 then we are
|
---|
| 1318 | * the winner.
|
---|
| 1319 | */
|
---|
| 1320 |
|
---|
| 1321 | if (get_updating_pid(sharename) != -1) {
|
---|
| 1322 | /*
|
---|
| 1323 | * Someone else is doing the update, exit.
|
---|
| 1324 | */
|
---|
| 1325 | tdb_unlock_bystring(pdb->tdb, keystr);
|
---|
| 1326 | release_print_db(pdb);
|
---|
| 1327 | return;
|
---|
| 1328 | }
|
---|
| 1329 |
|
---|
| 1330 | /*
|
---|
| 1331 | * We're going to do the update ourselves.
|
---|
| 1332 | */
|
---|
| 1333 |
|
---|
| 1334 | /* Tell others we're doing the update. */
|
---|
| 1335 | set_updating_pid(sharename, True);
|
---|
| 1336 |
|
---|
| 1337 | /*
|
---|
| 1338 | * Allow others to enter and notice we're doing
|
---|
| 1339 | * the update.
|
---|
| 1340 | */
|
---|
| 1341 |
|
---|
| 1342 | tdb_unlock_bystring(pdb->tdb, keystr);
|
---|
| 1343 |
|
---|
| 1344 | /* do the main work now */
|
---|
| 1345 |
|
---|
| 1346 | print_queue_update_internal( sharename, current_printif,
|
---|
| 1347 | lpq_command, lprm_command );
|
---|
| 1348 |
|
---|
| 1349 | /* Delete our pid from the db. */
|
---|
| 1350 | set_updating_pid(sharename, False);
|
---|
| 1351 | release_print_db(pdb);
|
---|
| 1352 | }
|
---|
| 1353 |
|
---|
| 1354 | /****************************************************************************
|
---|
| 1355 | this is the receive function of the background lpq updater
|
---|
| 1356 | ****************************************************************************/
|
---|
| 1357 | static void print_queue_receive(struct messaging_context *msg,
|
---|
| 1358 | void *private_data,
|
---|
| 1359 | uint32_t msg_type,
|
---|
| 1360 | struct server_id server_id,
|
---|
| 1361 | DATA_BLOB *data)
|
---|
| 1362 | {
|
---|
| 1363 | fstring sharename;
|
---|
| 1364 | char *lpqcommand = NULL, *lprmcommand = NULL;
|
---|
| 1365 | int printing_type;
|
---|
| 1366 | size_t len;
|
---|
| 1367 |
|
---|
| 1368 | len = tdb_unpack( (uint8 *)data->data, data->length, "fdPP",
|
---|
| 1369 | sharename,
|
---|
| 1370 | &printing_type,
|
---|
| 1371 | &lpqcommand,
|
---|
| 1372 | &lprmcommand );
|
---|
| 1373 |
|
---|
| 1374 | if ( len == -1 ) {
|
---|
| 1375 | SAFE_FREE(lpqcommand);
|
---|
| 1376 | SAFE_FREE(lprmcommand);
|
---|
| 1377 | DEBUG(0,("print_queue_receive: Got invalid print queue update message\n"));
|
---|
| 1378 | return;
|
---|
| 1379 | }
|
---|
| 1380 |
|
---|
| 1381 | print_queue_update_with_lock(sharename,
|
---|
| 1382 | get_printer_fns_from_type((enum printing_types)printing_type),
|
---|
| 1383 | lpqcommand, lprmcommand );
|
---|
| 1384 |
|
---|
| 1385 | SAFE_FREE(lpqcommand);
|
---|
| 1386 | SAFE_FREE(lprmcommand);
|
---|
| 1387 | return;
|
---|
| 1388 | }
|
---|
| 1389 |
|
---|
| 1390 | static pid_t background_lpq_updater_pid = -1;
|
---|
| 1391 |
|
---|
| 1392 | /****************************************************************************
|
---|
| 1393 | main thread of the background lpq updater
|
---|
| 1394 | ****************************************************************************/
|
---|
| 1395 | void start_background_queue(void)
|
---|
| 1396 | {
|
---|
[228] | 1397 | /* Use local variables for this as we don't
|
---|
| 1398 | * need to save the parent side of this, just
|
---|
| 1399 | * ensure it closes when the process exits.
|
---|
| 1400 | */
|
---|
| 1401 | int pause_pipe[2];
|
---|
| 1402 |
|
---|
[141] | 1403 | DEBUG(3,("start_background_queue: Starting background LPQ thread\n"));
|
---|
[228] | 1404 |
|
---|
[257] | 1405 | #ifndef __OS2__
|
---|
[228] | 1406 | if (pipe(pause_pipe) == -1) {
|
---|
[257] | 1407 | #else
|
---|
| 1408 | // on OS2 we have no select() on pipes, so we use socketpair() instead
|
---|
| 1409 | if (socketpair(AF_UNIX, SOCK_STREAM,0, pause_pipe) < 0) {
|
---|
| 1410 | #endif
|
---|
[228] | 1411 | DEBUG(5,("start_background_queue: cannot create pipe. %s\n", strerror(errno) ));
|
---|
| 1412 | exit(1);
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
[141] | 1415 | background_lpq_updater_pid = sys_fork();
|
---|
| 1416 |
|
---|
| 1417 | if (background_lpq_updater_pid == -1) {
|
---|
| 1418 | DEBUG(5,("start_background_queue: background LPQ thread failed to start. %s\n", strerror(errno) ));
|
---|
| 1419 | exit(1);
|
---|
| 1420 | }
|
---|
| 1421 |
|
---|
| 1422 | if(background_lpq_updater_pid == 0) {
|
---|
| 1423 | /* Child. */
|
---|
| 1424 | DEBUG(5,("start_background_queue: background LPQ thread started\n"));
|
---|
| 1425 |
|
---|
[228] | 1426 | close(pause_pipe[0]);
|
---|
| 1427 | pause_pipe[0] = -1;
|
---|
| 1428 |
|
---|
[232] | 1429 | if (!reinit_after_fork(smbd_messaging_context(),
|
---|
| 1430 | smbd_event_context(), true)) {
|
---|
[141] | 1431 | DEBUG(0,("reinit_after_fork() failed\n"));
|
---|
| 1432 | smb_panic("reinit_after_fork() failed");
|
---|
| 1433 | }
|
---|
| 1434 |
|
---|
| 1435 | claim_connection( NULL, "smbd lpq backend",
|
---|
| 1436 | FLAG_MSG_GENERAL|FLAG_MSG_SMBD|FLAG_MSG_PRINT_GENERAL);
|
---|
| 1437 |
|
---|
| 1438 | if (!locking_init()) {
|
---|
| 1439 | exit(1);
|
---|
| 1440 | }
|
---|
| 1441 |
|
---|
| 1442 | messaging_register(smbd_messaging_context(), NULL,
|
---|
| 1443 | MSG_PRINTER_UPDATE, print_queue_receive);
|
---|
[228] | 1444 |
|
---|
[141] | 1445 | DEBUG(5,("start_background_queue: background LPQ thread waiting for messages\n"));
|
---|
| 1446 | while (1) {
|
---|
[228] | 1447 | fd_set pause_fds;
|
---|
| 1448 | int pause_select;
|
---|
| 1449 |
|
---|
| 1450 | FD_ZERO(&pause_fds);
|
---|
| 1451 | FD_SET(pause_pipe[1], &pause_fds);
|
---|
| 1452 | pause_select = sys_select(pause_pipe[1]+1, &pause_fds, NULL, NULL, NULL);
|
---|
| 1453 | /* If pause_pipe[0] is closed it means the parent smbd
|
---|
| 1454 | * and children exited or aborted. */
|
---|
| 1455 | if (pause_select == 1) {
|
---|
| 1456 | exit_server_cleanly(NULL);
|
---|
| 1457 | }
|
---|
| 1458 |
|
---|
[141] | 1459 | /* check for some essential signals first */
|
---|
[228] | 1460 |
|
---|
[141] | 1461 | if (got_sig_term) {
|
---|
| 1462 | exit_server_cleanly(NULL);
|
---|
| 1463 | }
|
---|
| 1464 |
|
---|
| 1465 | if (reload_after_sighup) {
|
---|
| 1466 | change_to_root_user();
|
---|
| 1467 | DEBUG(1,("Reloading services after SIGHUP\n"));
|
---|
| 1468 | reload_services(False);
|
---|
| 1469 | reload_after_sighup = 0;
|
---|
| 1470 | }
|
---|
[228] | 1471 |
|
---|
[141] | 1472 | /* now check for messages */
|
---|
[228] | 1473 |
|
---|
[141] | 1474 | DEBUG(10,("start_background_queue: background LPQ thread got a message\n"));
|
---|
| 1475 | message_dispatch(smbd_messaging_context());
|
---|
| 1476 |
|
---|
| 1477 | /* process any pending print change notify messages */
|
---|
| 1478 |
|
---|
| 1479 | print_notify_send_messages(smbd_messaging_context(),
|
---|
| 1480 | 0);
|
---|
| 1481 | }
|
---|
| 1482 | }
|
---|
[228] | 1483 |
|
---|
| 1484 | close(pause_pipe[1]);
|
---|
[141] | 1485 | }
|
---|
| 1486 |
|
---|
| 1487 | /****************************************************************************
|
---|
| 1488 | update the internal database from the system print queue for a queue
|
---|
| 1489 | ****************************************************************************/
|
---|
| 1490 |
|
---|
| 1491 | static void print_queue_update(int snum, bool force)
|
---|
| 1492 | {
|
---|
| 1493 | fstring key;
|
---|
| 1494 | fstring sharename;
|
---|
| 1495 | char *lpqcommand = NULL;
|
---|
| 1496 | char *lprmcommand = NULL;
|
---|
| 1497 | uint8 *buffer = NULL;
|
---|
| 1498 | size_t len = 0;
|
---|
| 1499 | size_t newlen;
|
---|
| 1500 | struct tdb_print_db *pdb;
|
---|
| 1501 | int type;
|
---|
| 1502 | struct printif *current_printif;
|
---|
| 1503 | TALLOC_CTX *ctx = talloc_tos();
|
---|
| 1504 |
|
---|
| 1505 | fstrcpy( sharename, lp_const_servicename(snum));
|
---|
| 1506 |
|
---|
| 1507 | /* don't strip out characters like '$' from the printername */
|
---|
| 1508 |
|
---|
| 1509 | lpqcommand = talloc_string_sub2(ctx,
|
---|
| 1510 | lp_lpqcommand(snum),
|
---|
| 1511 | "%p",
|
---|
| 1512 | PRINTERNAME(snum),
|
---|
| 1513 | false, false, false);
|
---|
| 1514 | if (!lpqcommand) {
|
---|
| 1515 | return;
|
---|
| 1516 | }
|
---|
| 1517 | lpqcommand = talloc_sub_advanced(ctx,
|
---|
| 1518 | lp_servicename(snum),
|
---|
| 1519 | current_user_info.unix_name,
|
---|
| 1520 | "",
|
---|
| 1521 | current_user.ut.gid,
|
---|
| 1522 | get_current_username(),
|
---|
| 1523 | current_user_info.domain,
|
---|
| 1524 | lpqcommand);
|
---|
| 1525 | if (!lpqcommand) {
|
---|
| 1526 | return;
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
| 1529 | lprmcommand = talloc_string_sub2(ctx,
|
---|
| 1530 | lp_lprmcommand(snum),
|
---|
| 1531 | "%p",
|
---|
| 1532 | PRINTERNAME(snum),
|
---|
| 1533 | false, false, false);
|
---|
| 1534 | if (!lprmcommand) {
|
---|
| 1535 | return;
|
---|
| 1536 | }
|
---|
| 1537 | lprmcommand = talloc_sub_advanced(ctx,
|
---|
| 1538 | lp_servicename(snum),
|
---|
| 1539 | current_user_info.unix_name,
|
---|
| 1540 | "",
|
---|
| 1541 | current_user.ut.gid,
|
---|
| 1542 | get_current_username(),
|
---|
| 1543 | current_user_info.domain,
|
---|
| 1544 | lprmcommand);
|
---|
| 1545 | if (!lprmcommand) {
|
---|
| 1546 | return;
|
---|
| 1547 | }
|
---|
| 1548 |
|
---|
| 1549 | /*
|
---|
| 1550 | * Make sure that the background queue process exists.
|
---|
| 1551 | * Otherwise just do the update ourselves
|
---|
| 1552 | */
|
---|
| 1553 |
|
---|
| 1554 | if ( force || background_lpq_updater_pid == -1 ) {
|
---|
| 1555 | DEBUG(4,("print_queue_update: updating queue [%s] myself\n", sharename));
|
---|
| 1556 | current_printif = get_printer_fns( snum );
|
---|
| 1557 | print_queue_update_with_lock( sharename, current_printif, lpqcommand, lprmcommand );
|
---|
| 1558 |
|
---|
| 1559 | return;
|
---|
| 1560 | }
|
---|
| 1561 |
|
---|
| 1562 | type = lp_printing(snum);
|
---|
| 1563 |
|
---|
| 1564 | /* get the length */
|
---|
| 1565 |
|
---|
| 1566 | len = tdb_pack( NULL, 0, "fdPP",
|
---|
| 1567 | sharename,
|
---|
| 1568 | type,
|
---|
| 1569 | lpqcommand,
|
---|
| 1570 | lprmcommand );
|
---|
| 1571 |
|
---|
| 1572 | buffer = SMB_XMALLOC_ARRAY( uint8, len );
|
---|
| 1573 |
|
---|
| 1574 | /* now pack the buffer */
|
---|
| 1575 | newlen = tdb_pack( buffer, len, "fdPP",
|
---|
| 1576 | sharename,
|
---|
| 1577 | type,
|
---|
| 1578 | lpqcommand,
|
---|
| 1579 | lprmcommand );
|
---|
| 1580 |
|
---|
| 1581 | SMB_ASSERT( newlen == len );
|
---|
| 1582 |
|
---|
| 1583 | DEBUG(10,("print_queue_update: Sending message -> printer = %s, "
|
---|
| 1584 | "type = %d, lpq command = [%s] lprm command = [%s]\n",
|
---|
| 1585 | sharename, type, lpqcommand, lprmcommand ));
|
---|
| 1586 |
|
---|
| 1587 | /* here we set a msg pending record for other smbd processes
|
---|
| 1588 | to throttle the number of duplicate print_queue_update msgs
|
---|
| 1589 | sent. */
|
---|
| 1590 |
|
---|
| 1591 | pdb = get_print_db_byname(sharename);
|
---|
| 1592 | if (!pdb) {
|
---|
| 1593 | SAFE_FREE(buffer);
|
---|
| 1594 | return;
|
---|
| 1595 | }
|
---|
| 1596 |
|
---|
| 1597 | snprintf(key, sizeof(key), "MSG_PENDING/%s", sharename);
|
---|
| 1598 |
|
---|
| 1599 | if ( !tdb_store_uint32( pdb->tdb, key, time(NULL) ) ) {
|
---|
| 1600 | /* log a message but continue on */
|
---|
| 1601 |
|
---|
| 1602 | DEBUG(0,("print_queue_update: failed to store MSG_PENDING flag for [%s]!\n",
|
---|
| 1603 | sharename));
|
---|
| 1604 | }
|
---|
| 1605 |
|
---|
| 1606 | release_print_db( pdb );
|
---|
| 1607 |
|
---|
| 1608 | /* finally send the message */
|
---|
| 1609 |
|
---|
| 1610 | messaging_send_buf(smbd_messaging_context(),
|
---|
| 1611 | pid_to_procid(background_lpq_updater_pid),
|
---|
| 1612 | MSG_PRINTER_UPDATE, (uint8 *)buffer, len);
|
---|
| 1613 |
|
---|
| 1614 | SAFE_FREE( buffer );
|
---|
| 1615 |
|
---|
| 1616 | return;
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
| 1619 | /****************************************************************************
|
---|
| 1620 | Create/Update an entry in the print tdb that will allow us to send notify
|
---|
| 1621 | updates only to interested smbd's.
|
---|
| 1622 | ****************************************************************************/
|
---|
| 1623 |
|
---|
| 1624 | bool print_notify_register_pid(int snum)
|
---|
| 1625 | {
|
---|
| 1626 | TDB_DATA data;
|
---|
| 1627 | struct tdb_print_db *pdb = NULL;
|
---|
| 1628 | TDB_CONTEXT *tdb = NULL;
|
---|
| 1629 | const char *printername;
|
---|
| 1630 | uint32 mypid = (uint32)sys_getpid();
|
---|
| 1631 | bool ret = False;
|
---|
| 1632 | size_t i;
|
---|
| 1633 |
|
---|
| 1634 | /* if (snum == -1), then the change notify request was
|
---|
| 1635 | on a print server handle and we need to register on
|
---|
| 1636 | all print queus */
|
---|
| 1637 |
|
---|
| 1638 | if (snum == -1)
|
---|
| 1639 | {
|
---|
| 1640 | int num_services = lp_numservices();
|
---|
| 1641 | int idx;
|
---|
| 1642 |
|
---|
| 1643 | for ( idx=0; idx<num_services; idx++ ) {
|
---|
| 1644 | if (lp_snum_ok(idx) && lp_print_ok(idx) )
|
---|
| 1645 | print_notify_register_pid(idx);
|
---|
| 1646 | }
|
---|
| 1647 |
|
---|
| 1648 | return True;
|
---|
| 1649 | }
|
---|
| 1650 | else /* register for a specific printer */
|
---|
| 1651 | {
|
---|
| 1652 | printername = lp_const_servicename(snum);
|
---|
| 1653 | pdb = get_print_db_byname(printername);
|
---|
| 1654 | if (!pdb)
|
---|
| 1655 | return False;
|
---|
| 1656 | tdb = pdb->tdb;
|
---|
| 1657 | }
|
---|
| 1658 |
|
---|
| 1659 | if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
|
---|
| 1660 | DEBUG(0,("print_notify_register_pid: Failed to lock printer %s\n",
|
---|
| 1661 | printername));
|
---|
| 1662 | if (pdb)
|
---|
| 1663 | release_print_db(pdb);
|
---|
| 1664 | return False;
|
---|
| 1665 | }
|
---|
| 1666 |
|
---|
| 1667 | data = get_printer_notify_pid_list( tdb, printername, True );
|
---|
| 1668 |
|
---|
| 1669 | /* Add ourselves and increase the refcount. */
|
---|
| 1670 |
|
---|
| 1671 | for (i = 0; i < data.dsize; i += 8) {
|
---|
| 1672 | if (IVAL(data.dptr,i) == mypid) {
|
---|
| 1673 | uint32 new_refcount = IVAL(data.dptr, i+4) + 1;
|
---|
| 1674 | SIVAL(data.dptr, i+4, new_refcount);
|
---|
| 1675 | break;
|
---|
| 1676 | }
|
---|
| 1677 | }
|
---|
| 1678 |
|
---|
| 1679 | if (i == data.dsize) {
|
---|
| 1680 | /* We weren't in the list. Realloc. */
|
---|
| 1681 | data.dptr = (uint8 *)SMB_REALLOC(data.dptr, data.dsize + 8);
|
---|
| 1682 | if (!data.dptr) {
|
---|
| 1683 | DEBUG(0,("print_notify_register_pid: Relloc fail for printer %s\n",
|
---|
| 1684 | printername));
|
---|
| 1685 | goto done;
|
---|
| 1686 | }
|
---|
| 1687 | data.dsize += 8;
|
---|
| 1688 | SIVAL(data.dptr,data.dsize - 8,mypid);
|
---|
| 1689 | SIVAL(data.dptr,data.dsize - 4,1); /* Refcount. */
|
---|
| 1690 | }
|
---|
| 1691 |
|
---|
| 1692 | /* Store back the record. */
|
---|
| 1693 | if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
|
---|
| 1694 | DEBUG(0,("print_notify_register_pid: Failed to update pid \
|
---|
| 1695 | list for printer %s\n", printername));
|
---|
| 1696 | goto done;
|
---|
| 1697 | }
|
---|
| 1698 |
|
---|
| 1699 | ret = True;
|
---|
| 1700 |
|
---|
| 1701 | done:
|
---|
| 1702 |
|
---|
| 1703 | tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
|
---|
| 1704 | if (pdb)
|
---|
| 1705 | release_print_db(pdb);
|
---|
| 1706 | SAFE_FREE(data.dptr);
|
---|
| 1707 | return ret;
|
---|
| 1708 | }
|
---|
| 1709 |
|
---|
| 1710 | /****************************************************************************
|
---|
| 1711 | Update an entry in the print tdb that will allow us to send notify
|
---|
| 1712 | updates only to interested smbd's.
|
---|
| 1713 | ****************************************************************************/
|
---|
| 1714 |
|
---|
| 1715 | bool print_notify_deregister_pid(int snum)
|
---|
| 1716 | {
|
---|
| 1717 | TDB_DATA data;
|
---|
| 1718 | struct tdb_print_db *pdb = NULL;
|
---|
| 1719 | TDB_CONTEXT *tdb = NULL;
|
---|
| 1720 | const char *printername;
|
---|
| 1721 | uint32 mypid = (uint32)sys_getpid();
|
---|
| 1722 | size_t i;
|
---|
| 1723 | bool ret = False;
|
---|
| 1724 |
|
---|
| 1725 | /* if ( snum == -1 ), we are deregister a print server handle
|
---|
| 1726 | which means to deregister on all print queues */
|
---|
| 1727 |
|
---|
| 1728 | if (snum == -1)
|
---|
| 1729 | {
|
---|
| 1730 | int num_services = lp_numservices();
|
---|
| 1731 | int idx;
|
---|
| 1732 |
|
---|
| 1733 | for ( idx=0; idx<num_services; idx++ ) {
|
---|
| 1734 | if ( lp_snum_ok(idx) && lp_print_ok(idx) )
|
---|
| 1735 | print_notify_deregister_pid(idx);
|
---|
| 1736 | }
|
---|
| 1737 |
|
---|
| 1738 | return True;
|
---|
| 1739 | }
|
---|
| 1740 | else /* deregister a specific printer */
|
---|
| 1741 | {
|
---|
| 1742 | printername = lp_const_servicename(snum);
|
---|
| 1743 | pdb = get_print_db_byname(printername);
|
---|
| 1744 | if (!pdb)
|
---|
| 1745 | return False;
|
---|
| 1746 | tdb = pdb->tdb;
|
---|
| 1747 | }
|
---|
| 1748 |
|
---|
| 1749 | if (tdb_lock_bystring_with_timeout(tdb, NOTIFY_PID_LIST_KEY, 10) == -1) {
|
---|
| 1750 | DEBUG(0,("print_notify_register_pid: Failed to lock \
|
---|
| 1751 | printer %s database\n", printername));
|
---|
| 1752 | if (pdb)
|
---|
| 1753 | release_print_db(pdb);
|
---|
| 1754 | return False;
|
---|
| 1755 | }
|
---|
| 1756 |
|
---|
| 1757 | data = get_printer_notify_pid_list( tdb, printername, True );
|
---|
| 1758 |
|
---|
| 1759 | /* Reduce refcount. Remove ourselves if zero. */
|
---|
| 1760 |
|
---|
| 1761 | for (i = 0; i < data.dsize; ) {
|
---|
| 1762 | if (IVAL(data.dptr,i) == mypid) {
|
---|
| 1763 | uint32 refcount = IVAL(data.dptr, i+4);
|
---|
| 1764 |
|
---|
| 1765 | refcount--;
|
---|
| 1766 |
|
---|
| 1767 | if (refcount == 0) {
|
---|
| 1768 | if (data.dsize - i > 8)
|
---|
| 1769 | memmove( &data.dptr[i], &data.dptr[i+8], data.dsize - i - 8);
|
---|
| 1770 | data.dsize -= 8;
|
---|
| 1771 | continue;
|
---|
| 1772 | }
|
---|
| 1773 | SIVAL(data.dptr, i+4, refcount);
|
---|
| 1774 | }
|
---|
| 1775 |
|
---|
| 1776 | i += 8;
|
---|
| 1777 | }
|
---|
| 1778 |
|
---|
| 1779 | if (data.dsize == 0)
|
---|
| 1780 | SAFE_FREE(data.dptr);
|
---|
| 1781 |
|
---|
| 1782 | /* Store back the record. */
|
---|
| 1783 | if (tdb_store_bystring(tdb, NOTIFY_PID_LIST_KEY, data, TDB_REPLACE) == -1) {
|
---|
| 1784 | DEBUG(0,("print_notify_register_pid: Failed to update pid \
|
---|
| 1785 | list for printer %s\n", printername));
|
---|
| 1786 | goto done;
|
---|
| 1787 | }
|
---|
| 1788 |
|
---|
| 1789 | ret = True;
|
---|
| 1790 |
|
---|
| 1791 | done:
|
---|
| 1792 |
|
---|
| 1793 | tdb_unlock_bystring(tdb, NOTIFY_PID_LIST_KEY);
|
---|
| 1794 | if (pdb)
|
---|
| 1795 | release_print_db(pdb);
|
---|
| 1796 | SAFE_FREE(data.dptr);
|
---|
| 1797 | return ret;
|
---|
| 1798 | }
|
---|
| 1799 |
|
---|
| 1800 | /****************************************************************************
|
---|
| 1801 | Check if a jobid is valid. It is valid if it exists in the database.
|
---|
| 1802 | ****************************************************************************/
|
---|
| 1803 |
|
---|
| 1804 | bool print_job_exists(const char* sharename, uint32 jobid)
|
---|
| 1805 | {
|
---|
| 1806 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 1807 | bool ret;
|
---|
| 1808 | uint32_t tmp;
|
---|
| 1809 |
|
---|
| 1810 | if (!pdb)
|
---|
| 1811 | return False;
|
---|
| 1812 | ret = tdb_exists(pdb->tdb, print_key(jobid, &tmp));
|
---|
| 1813 | release_print_db(pdb);
|
---|
| 1814 | return ret;
|
---|
| 1815 | }
|
---|
| 1816 |
|
---|
| 1817 | /****************************************************************************
|
---|
| 1818 | Give the fd used for a jobid.
|
---|
| 1819 | ****************************************************************************/
|
---|
| 1820 |
|
---|
| 1821 | int print_job_fd(const char* sharename, uint32 jobid)
|
---|
| 1822 | {
|
---|
| 1823 | struct printjob *pjob = print_job_find(sharename, jobid);
|
---|
| 1824 | if (!pjob)
|
---|
| 1825 | return -1;
|
---|
| 1826 | /* don't allow another process to get this info - it is meaningless */
|
---|
| 1827 | if (pjob->pid != sys_getpid())
|
---|
| 1828 | return -1;
|
---|
| 1829 | return pjob->fd;
|
---|
| 1830 | }
|
---|
| 1831 |
|
---|
| 1832 | /****************************************************************************
|
---|
| 1833 | Give the filename used for a jobid.
|
---|
| 1834 | Only valid for the process doing the spooling and when the job
|
---|
| 1835 | has not been spooled.
|
---|
| 1836 | ****************************************************************************/
|
---|
| 1837 |
|
---|
| 1838 | char *print_job_fname(const char* sharename, uint32 jobid)
|
---|
| 1839 | {
|
---|
| 1840 | struct printjob *pjob = print_job_find(sharename, jobid);
|
---|
| 1841 | if (!pjob || pjob->spooled || pjob->pid != sys_getpid())
|
---|
| 1842 | return NULL;
|
---|
| 1843 | return pjob->filename;
|
---|
| 1844 | }
|
---|
| 1845 |
|
---|
| 1846 |
|
---|
| 1847 | /****************************************************************************
|
---|
| 1848 | Give the filename used for a jobid.
|
---|
| 1849 | Only valid for the process doing the spooling and when the job
|
---|
| 1850 | has not been spooled.
|
---|
| 1851 | ****************************************************************************/
|
---|
| 1852 |
|
---|
| 1853 | NT_DEVICEMODE *print_job_devmode(const char* sharename, uint32 jobid)
|
---|
| 1854 | {
|
---|
| 1855 | struct printjob *pjob = print_job_find(sharename, jobid);
|
---|
| 1856 |
|
---|
| 1857 | if ( !pjob )
|
---|
| 1858 | return NULL;
|
---|
| 1859 |
|
---|
| 1860 | return pjob->nt_devmode;
|
---|
| 1861 | }
|
---|
| 1862 |
|
---|
| 1863 | /****************************************************************************
|
---|
| 1864 | Set the place in the queue for a job.
|
---|
| 1865 | ****************************************************************************/
|
---|
| 1866 |
|
---|
| 1867 | bool print_job_set_place(const char *sharename, uint32 jobid, int place)
|
---|
| 1868 | {
|
---|
| 1869 | DEBUG(2,("print_job_set_place not implemented yet\n"));
|
---|
| 1870 | return False;
|
---|
| 1871 | }
|
---|
| 1872 |
|
---|
| 1873 | /****************************************************************************
|
---|
| 1874 | Set the name of a job. Only possible for owner.
|
---|
| 1875 | ****************************************************************************/
|
---|
| 1876 |
|
---|
| 1877 | bool print_job_set_name(const char *sharename, uint32 jobid, char *name)
|
---|
| 1878 | {
|
---|
| 1879 | struct printjob *pjob;
|
---|
| 1880 |
|
---|
| 1881 | pjob = print_job_find(sharename, jobid);
|
---|
| 1882 | if (!pjob || pjob->pid != sys_getpid())
|
---|
| 1883 | return False;
|
---|
| 1884 |
|
---|
| 1885 | fstrcpy(pjob->jobname, name);
|
---|
| 1886 | return pjob_store(sharename, jobid, pjob);
|
---|
| 1887 | }
|
---|
| 1888 |
|
---|
| 1889 | /***************************************************************************
|
---|
| 1890 | Remove a jobid from the 'jobs changed' list.
|
---|
| 1891 | ***************************************************************************/
|
---|
| 1892 |
|
---|
| 1893 | static bool remove_from_jobs_changed(const char* sharename, uint32 jobid)
|
---|
| 1894 | {
|
---|
| 1895 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 1896 | TDB_DATA data, key;
|
---|
| 1897 | size_t job_count, i;
|
---|
| 1898 | bool ret = False;
|
---|
| 1899 | bool gotlock = False;
|
---|
| 1900 |
|
---|
| 1901 | if (!pdb) {
|
---|
| 1902 | return False;
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
| 1905 | ZERO_STRUCT(data);
|
---|
| 1906 |
|
---|
| 1907 | key = string_tdb_data("INFO/jobs_changed");
|
---|
| 1908 |
|
---|
| 1909 | if (tdb_chainlock_with_timeout(pdb->tdb, key, 5) == -1)
|
---|
| 1910 | goto out;
|
---|
| 1911 |
|
---|
| 1912 | gotlock = True;
|
---|
| 1913 |
|
---|
| 1914 | data = tdb_fetch(pdb->tdb, key);
|
---|
| 1915 |
|
---|
| 1916 | if (data.dptr == NULL || data.dsize == 0 || (data.dsize % 4 != 0))
|
---|
| 1917 | goto out;
|
---|
| 1918 |
|
---|
| 1919 | job_count = data.dsize / 4;
|
---|
| 1920 | for (i = 0; i < job_count; i++) {
|
---|
| 1921 | uint32 ch_jobid;
|
---|
| 1922 |
|
---|
| 1923 | ch_jobid = IVAL(data.dptr, i*4);
|
---|
| 1924 | if (ch_jobid == jobid) {
|
---|
| 1925 | if (i < job_count -1 )
|
---|
| 1926 | memmove(data.dptr + (i*4), data.dptr + (i*4) + 4, (job_count - i - 1)*4 );
|
---|
| 1927 | data.dsize -= 4;
|
---|
| 1928 | if (tdb_store(pdb->tdb, key, data, TDB_REPLACE) == -1)
|
---|
| 1929 | goto out;
|
---|
| 1930 | break;
|
---|
| 1931 | }
|
---|
| 1932 | }
|
---|
| 1933 |
|
---|
| 1934 | ret = True;
|
---|
| 1935 | out:
|
---|
| 1936 |
|
---|
| 1937 | if (gotlock)
|
---|
| 1938 | tdb_chainunlock(pdb->tdb, key);
|
---|
| 1939 | SAFE_FREE(data.dptr);
|
---|
| 1940 | release_print_db(pdb);
|
---|
| 1941 | if (ret)
|
---|
| 1942 | DEBUG(10,("remove_from_jobs_changed: removed jobid %u\n", (unsigned int)jobid ));
|
---|
| 1943 | else
|
---|
| 1944 | DEBUG(10,("remove_from_jobs_changed: Failed to remove jobid %u\n", (unsigned int)jobid ));
|
---|
| 1945 | return ret;
|
---|
| 1946 | }
|
---|
| 1947 |
|
---|
| 1948 | /****************************************************************************
|
---|
| 1949 | Delete a print job - don't update queue.
|
---|
| 1950 | ****************************************************************************/
|
---|
| 1951 |
|
---|
| 1952 | static bool print_job_delete1(int snum, uint32 jobid)
|
---|
| 1953 | {
|
---|
| 1954 | const char* sharename = lp_const_servicename(snum);
|
---|
| 1955 | struct printjob *pjob = print_job_find(sharename, jobid);
|
---|
| 1956 | int result = 0;
|
---|
| 1957 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 1958 |
|
---|
| 1959 | if (!pjob)
|
---|
| 1960 | return False;
|
---|
| 1961 |
|
---|
| 1962 | /*
|
---|
| 1963 | * If already deleting just return.
|
---|
| 1964 | */
|
---|
| 1965 |
|
---|
| 1966 | if (pjob->status == LPQ_DELETING)
|
---|
| 1967 | return True;
|
---|
| 1968 |
|
---|
| 1969 | /* Hrm - we need to be able to cope with deleting a job before it
|
---|
| 1970 | has reached the spooler. Just mark it as LPQ_DELETING and
|
---|
| 1971 | let the print_queue_update() code rmeove the record */
|
---|
| 1972 |
|
---|
| 1973 |
|
---|
| 1974 | if (pjob->sysjob == -1) {
|
---|
| 1975 | DEBUG(5, ("attempt to delete job %u not seen by lpr\n", (unsigned int)jobid));
|
---|
| 1976 | }
|
---|
| 1977 |
|
---|
| 1978 | /* Set the tdb entry to be deleting. */
|
---|
| 1979 |
|
---|
| 1980 | pjob->status = LPQ_DELETING;
|
---|
| 1981 | pjob_store(sharename, jobid, pjob);
|
---|
| 1982 |
|
---|
| 1983 | if (pjob->spooled && pjob->sysjob != -1)
|
---|
| 1984 | {
|
---|
| 1985 | result = (*(current_printif->job_delete))(
|
---|
| 1986 | PRINTERNAME(snum),
|
---|
| 1987 | lp_lprmcommand(snum),
|
---|
| 1988 | pjob);
|
---|
| 1989 |
|
---|
| 1990 | /* Delete the tdb entry if the delete succeeded or the job hasn't
|
---|
| 1991 | been spooled. */
|
---|
| 1992 |
|
---|
| 1993 | if (result == 0) {
|
---|
| 1994 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 1995 | int njobs = 1;
|
---|
| 1996 |
|
---|
| 1997 | if (!pdb)
|
---|
| 1998 | return False;
|
---|
| 1999 | pjob_delete(sharename, jobid);
|
---|
| 2000 | /* Ensure we keep a rough count of the number of total jobs... */
|
---|
| 2001 | tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, -1);
|
---|
| 2002 | release_print_db(pdb);
|
---|
| 2003 | }
|
---|
| 2004 | }
|
---|
| 2005 |
|
---|
| 2006 | remove_from_jobs_changed( sharename, jobid );
|
---|
| 2007 |
|
---|
| 2008 | return (result == 0);
|
---|
| 2009 | }
|
---|
| 2010 |
|
---|
| 2011 | /****************************************************************************
|
---|
| 2012 | Return true if the current user owns the print job.
|
---|
| 2013 | ****************************************************************************/
|
---|
| 2014 |
|
---|
| 2015 | static bool is_owner(struct current_user *user, const char *servicename,
|
---|
| 2016 | uint32 jobid)
|
---|
| 2017 | {
|
---|
| 2018 | struct printjob *pjob = print_job_find(servicename, jobid);
|
---|
| 2019 | user_struct *vuser;
|
---|
| 2020 |
|
---|
| 2021 | if (!pjob || !user)
|
---|
| 2022 | return False;
|
---|
| 2023 |
|
---|
| 2024 | if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
|
---|
| 2025 | return strequal(pjob->user, vuser->user.smb_name);
|
---|
| 2026 | } else {
|
---|
| 2027 | return strequal(pjob->user, uidtoname(user->ut.uid));
|
---|
| 2028 | }
|
---|
| 2029 | }
|
---|
| 2030 |
|
---|
| 2031 | /****************************************************************************
|
---|
| 2032 | Delete a print job.
|
---|
| 2033 | ****************************************************************************/
|
---|
| 2034 |
|
---|
| 2035 | bool print_job_delete(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
|
---|
| 2036 | {
|
---|
| 2037 | const char* sharename = lp_const_servicename( snum );
|
---|
| 2038 | struct printjob *pjob;
|
---|
| 2039 | bool owner;
|
---|
| 2040 | char *fname;
|
---|
| 2041 |
|
---|
| 2042 | *errcode = WERR_OK;
|
---|
| 2043 |
|
---|
| 2044 | owner = is_owner(user, lp_const_servicename(snum), jobid);
|
---|
| 2045 |
|
---|
| 2046 | /* Check access against security descriptor or whether the user
|
---|
| 2047 | owns their job. */
|
---|
| 2048 |
|
---|
| 2049 | if (!owner &&
|
---|
| 2050 | !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
|
---|
| 2051 | DEBUG(3, ("delete denied by security descriptor\n"));
|
---|
| 2052 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2053 |
|
---|
| 2054 | /* BEGIN_ADMIN_LOG */
|
---|
| 2055 | sys_adminlog( LOG_ERR,
|
---|
| 2056 | "Permission denied-- user not allowed to delete, \
|
---|
| 2057 | pause, or resume print job. User name: %s. Printer name: %s.",
|
---|
| 2058 | uidtoname(user->ut.uid), PRINTERNAME(snum) );
|
---|
| 2059 | /* END_ADMIN_LOG */
|
---|
| 2060 |
|
---|
| 2061 | return False;
|
---|
| 2062 | }
|
---|
| 2063 |
|
---|
| 2064 | /*
|
---|
| 2065 | * get the spooled filename of the print job
|
---|
| 2066 | * if this works, then the file has not been spooled
|
---|
| 2067 | * to the underlying print system. Just delete the
|
---|
| 2068 | * spool file & return.
|
---|
| 2069 | */
|
---|
| 2070 |
|
---|
| 2071 | if ( (fname = print_job_fname( sharename, jobid )) != NULL )
|
---|
| 2072 | {
|
---|
| 2073 | /* remove the spool file */
|
---|
| 2074 | DEBUG(10,("print_job_delete: Removing spool file [%s]\n", fname ));
|
---|
| 2075 | if ( unlink( fname ) == -1 ) {
|
---|
| 2076 | *errcode = map_werror_from_unix(errno);
|
---|
| 2077 | return False;
|
---|
| 2078 | }
|
---|
| 2079 | }
|
---|
| 2080 |
|
---|
| 2081 | if (!print_job_delete1(snum, jobid)) {
|
---|
| 2082 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2083 | return False;
|
---|
| 2084 | }
|
---|
| 2085 |
|
---|
| 2086 | /* force update the database and say the delete failed if the
|
---|
| 2087 | job still exists */
|
---|
| 2088 |
|
---|
| 2089 | print_queue_update(snum, True);
|
---|
| 2090 |
|
---|
| 2091 | pjob = print_job_find(sharename, jobid);
|
---|
| 2092 | if ( pjob && (pjob->status != LPQ_DELETING) )
|
---|
| 2093 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2094 |
|
---|
| 2095 | return (pjob == NULL );
|
---|
| 2096 | }
|
---|
| 2097 |
|
---|
| 2098 | /****************************************************************************
|
---|
| 2099 | Pause a job.
|
---|
| 2100 | ****************************************************************************/
|
---|
| 2101 |
|
---|
| 2102 | bool print_job_pause(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
|
---|
| 2103 | {
|
---|
| 2104 | const char* sharename = lp_const_servicename(snum);
|
---|
| 2105 | struct printjob *pjob;
|
---|
| 2106 | int ret = -1;
|
---|
| 2107 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 2108 |
|
---|
| 2109 | pjob = print_job_find(sharename, jobid);
|
---|
| 2110 |
|
---|
| 2111 | if (!pjob || !user) {
|
---|
| 2112 | DEBUG(10, ("print_job_pause: no pjob or user for jobid %u\n",
|
---|
| 2113 | (unsigned int)jobid ));
|
---|
| 2114 | return False;
|
---|
| 2115 | }
|
---|
| 2116 |
|
---|
| 2117 | if (!pjob->spooled || pjob->sysjob == -1) {
|
---|
| 2118 | DEBUG(10, ("print_job_pause: not spooled or bad sysjob = %d for jobid %u\n",
|
---|
| 2119 | (int)pjob->sysjob, (unsigned int)jobid ));
|
---|
| 2120 | return False;
|
---|
| 2121 | }
|
---|
| 2122 |
|
---|
| 2123 | if (!is_owner(user, lp_const_servicename(snum), jobid) &&
|
---|
| 2124 | !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
|
---|
| 2125 | DEBUG(3, ("pause denied by security descriptor\n"));
|
---|
| 2126 |
|
---|
| 2127 | /* BEGIN_ADMIN_LOG */
|
---|
| 2128 | sys_adminlog( LOG_ERR,
|
---|
| 2129 | "Permission denied-- user not allowed to delete, \
|
---|
| 2130 | pause, or resume print job. User name: %s. Printer name: %s.",
|
---|
| 2131 | uidtoname(user->ut.uid), PRINTERNAME(snum) );
|
---|
| 2132 | /* END_ADMIN_LOG */
|
---|
| 2133 |
|
---|
| 2134 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2135 | return False;
|
---|
| 2136 | }
|
---|
| 2137 |
|
---|
| 2138 | /* need to pause the spooled entry */
|
---|
| 2139 | ret = (*(current_printif->job_pause))(snum, pjob);
|
---|
| 2140 |
|
---|
| 2141 | if (ret != 0) {
|
---|
| 2142 | *errcode = WERR_INVALID_PARAM;
|
---|
| 2143 | return False;
|
---|
| 2144 | }
|
---|
| 2145 |
|
---|
| 2146 | /* force update the database */
|
---|
| 2147 | print_cache_flush(lp_const_servicename(snum));
|
---|
| 2148 |
|
---|
| 2149 | /* Send a printer notify message */
|
---|
| 2150 |
|
---|
| 2151 | notify_job_status(sharename, jobid, JOB_STATUS_PAUSED);
|
---|
| 2152 |
|
---|
| 2153 | /* how do we tell if this succeeded? */
|
---|
| 2154 |
|
---|
| 2155 | return True;
|
---|
| 2156 | }
|
---|
| 2157 |
|
---|
| 2158 | /****************************************************************************
|
---|
| 2159 | Resume a job.
|
---|
| 2160 | ****************************************************************************/
|
---|
| 2161 |
|
---|
| 2162 | bool print_job_resume(struct current_user *user, int snum, uint32 jobid, WERROR *errcode)
|
---|
| 2163 | {
|
---|
| 2164 | const char *sharename = lp_const_servicename(snum);
|
---|
| 2165 | struct printjob *pjob;
|
---|
| 2166 | int ret;
|
---|
| 2167 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 2168 |
|
---|
| 2169 | pjob = print_job_find(sharename, jobid);
|
---|
| 2170 |
|
---|
| 2171 | if (!pjob || !user) {
|
---|
| 2172 | DEBUG(10, ("print_job_resume: no pjob or user for jobid %u\n",
|
---|
| 2173 | (unsigned int)jobid ));
|
---|
| 2174 | return False;
|
---|
| 2175 | }
|
---|
| 2176 |
|
---|
| 2177 | if (!pjob->spooled || pjob->sysjob == -1) {
|
---|
| 2178 | DEBUG(10, ("print_job_resume: not spooled or bad sysjob = %d for jobid %u\n",
|
---|
| 2179 | (int)pjob->sysjob, (unsigned int)jobid ));
|
---|
| 2180 | return False;
|
---|
| 2181 | }
|
---|
| 2182 |
|
---|
| 2183 | if (!is_owner(user, lp_const_servicename(snum), jobid) &&
|
---|
| 2184 | !print_access_check(user, snum, JOB_ACCESS_ADMINISTER)) {
|
---|
| 2185 | DEBUG(3, ("resume denied by security descriptor\n"));
|
---|
| 2186 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2187 |
|
---|
| 2188 | /* BEGIN_ADMIN_LOG */
|
---|
| 2189 | sys_adminlog( LOG_ERR,
|
---|
| 2190 | "Permission denied-- user not allowed to delete, \
|
---|
| 2191 | pause, or resume print job. User name: %s. Printer name: %s.",
|
---|
| 2192 | uidtoname(user->ut.uid), PRINTERNAME(snum) );
|
---|
| 2193 | /* END_ADMIN_LOG */
|
---|
| 2194 | return False;
|
---|
| 2195 | }
|
---|
| 2196 |
|
---|
| 2197 | ret = (*(current_printif->job_resume))(snum, pjob);
|
---|
| 2198 |
|
---|
| 2199 | if (ret != 0) {
|
---|
| 2200 | *errcode = WERR_INVALID_PARAM;
|
---|
| 2201 | return False;
|
---|
| 2202 | }
|
---|
| 2203 |
|
---|
| 2204 | /* force update the database */
|
---|
| 2205 | print_cache_flush(lp_const_servicename(snum));
|
---|
| 2206 |
|
---|
| 2207 | /* Send a printer notify message */
|
---|
| 2208 |
|
---|
| 2209 | notify_job_status(sharename, jobid, JOB_STATUS_QUEUED);
|
---|
| 2210 |
|
---|
| 2211 | return True;
|
---|
| 2212 | }
|
---|
| 2213 |
|
---|
| 2214 | /****************************************************************************
|
---|
| 2215 | Write to a print file.
|
---|
| 2216 | ****************************************************************************/
|
---|
| 2217 |
|
---|
| 2218 | ssize_t print_job_write(int snum, uint32 jobid, const char *buf, SMB_OFF_T pos, size_t size)
|
---|
| 2219 | {
|
---|
| 2220 | const char* sharename = lp_const_servicename(snum);
|
---|
| 2221 | int return_code;
|
---|
| 2222 | struct printjob *pjob;
|
---|
| 2223 |
|
---|
| 2224 | pjob = print_job_find(sharename, jobid);
|
---|
| 2225 |
|
---|
| 2226 | if (!pjob)
|
---|
| 2227 | return -1;
|
---|
| 2228 | /* don't allow another process to get this info - it is meaningless */
|
---|
| 2229 | if (pjob->pid != sys_getpid())
|
---|
| 2230 | return -1;
|
---|
| 2231 |
|
---|
| 2232 | return_code = write_data_at_offset(pjob->fd, buf, size, pos);
|
---|
| 2233 |
|
---|
| 2234 | if (return_code>0) {
|
---|
| 2235 | pjob->size += size;
|
---|
| 2236 | pjob_store(sharename, jobid, pjob);
|
---|
| 2237 | }
|
---|
| 2238 | return return_code;
|
---|
| 2239 | }
|
---|
| 2240 |
|
---|
| 2241 | /****************************************************************************
|
---|
| 2242 | Get the queue status - do not update if db is out of date.
|
---|
| 2243 | ****************************************************************************/
|
---|
| 2244 |
|
---|
| 2245 | static int get_queue_status(const char* sharename, print_status_struct *status)
|
---|
| 2246 | {
|
---|
| 2247 | fstring keystr;
|
---|
| 2248 | TDB_DATA data;
|
---|
| 2249 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 2250 | int len;
|
---|
| 2251 |
|
---|
| 2252 | if (status) {
|
---|
| 2253 | ZERO_STRUCTP(status);
|
---|
| 2254 | }
|
---|
| 2255 |
|
---|
| 2256 | if (!pdb)
|
---|
| 2257 | return 0;
|
---|
| 2258 |
|
---|
| 2259 | if (status) {
|
---|
| 2260 | fstr_sprintf(keystr, "STATUS/%s", sharename);
|
---|
| 2261 | data = tdb_fetch(pdb->tdb, string_tdb_data(keystr));
|
---|
| 2262 | if (data.dptr) {
|
---|
| 2263 | if (data.dsize == sizeof(print_status_struct))
|
---|
| 2264 | /* this memcpy is ok since the status struct was
|
---|
| 2265 | not packed before storing it in the tdb */
|
---|
| 2266 | memcpy(status, data.dptr, sizeof(print_status_struct));
|
---|
| 2267 | SAFE_FREE(data.dptr);
|
---|
| 2268 | }
|
---|
| 2269 | }
|
---|
| 2270 | len = tdb_fetch_int32(pdb->tdb, "INFO/total_jobs");
|
---|
| 2271 | release_print_db(pdb);
|
---|
| 2272 | return (len == -1 ? 0 : len);
|
---|
| 2273 | }
|
---|
| 2274 |
|
---|
| 2275 | /****************************************************************************
|
---|
| 2276 | Determine the number of jobs in a queue.
|
---|
| 2277 | ****************************************************************************/
|
---|
| 2278 |
|
---|
| 2279 | int print_queue_length(int snum, print_status_struct *pstatus)
|
---|
| 2280 | {
|
---|
| 2281 | const char* sharename = lp_const_servicename( snum );
|
---|
| 2282 | print_status_struct status;
|
---|
| 2283 | int len;
|
---|
| 2284 |
|
---|
| 2285 | ZERO_STRUCT( status );
|
---|
| 2286 |
|
---|
| 2287 | /* make sure the database is up to date */
|
---|
| 2288 | if (print_cache_expired(lp_const_servicename(snum), True))
|
---|
| 2289 | print_queue_update(snum, False);
|
---|
| 2290 |
|
---|
| 2291 | /* also fetch the queue status */
|
---|
| 2292 | memset(&status, 0, sizeof(status));
|
---|
| 2293 | len = get_queue_status(sharename, &status);
|
---|
| 2294 |
|
---|
| 2295 | if (pstatus)
|
---|
| 2296 | *pstatus = status;
|
---|
| 2297 |
|
---|
| 2298 | return len;
|
---|
| 2299 | }
|
---|
| 2300 |
|
---|
| 2301 | /***************************************************************************
|
---|
| 2302 | Allocate a jobid. Hold the lock for as short a time as possible.
|
---|
| 2303 | ***************************************************************************/
|
---|
| 2304 |
|
---|
| 2305 | static bool allocate_print_jobid(struct tdb_print_db *pdb, int snum, const char *sharename, uint32 *pjobid)
|
---|
| 2306 | {
|
---|
| 2307 | int i;
|
---|
| 2308 | uint32 jobid;
|
---|
| 2309 |
|
---|
| 2310 | *pjobid = (uint32)-1;
|
---|
| 2311 |
|
---|
| 2312 | for (i = 0; i < 3; i++) {
|
---|
| 2313 | /* Lock the database - only wait 20 seconds. */
|
---|
| 2314 | if (tdb_lock_bystring_with_timeout(pdb->tdb, "INFO/nextjob", 20) == -1) {
|
---|
| 2315 | DEBUG(0,("allocate_print_jobid: failed to lock printing database %s\n", sharename));
|
---|
| 2316 | return False;
|
---|
| 2317 | }
|
---|
| 2318 |
|
---|
| 2319 | if (!tdb_fetch_uint32(pdb->tdb, "INFO/nextjob", &jobid)) {
|
---|
| 2320 | if (tdb_error(pdb->tdb) != TDB_ERR_NOEXIST) {
|
---|
| 2321 | DEBUG(0, ("allocate_print_jobid: failed to fetch INFO/nextjob for print queue %s\n",
|
---|
| 2322 | sharename));
|
---|
| 2323 | return False;
|
---|
| 2324 | }
|
---|
| 2325 | jobid = 0;
|
---|
| 2326 | }
|
---|
| 2327 |
|
---|
| 2328 | jobid = NEXT_JOBID(jobid);
|
---|
| 2329 |
|
---|
| 2330 | if (tdb_store_int32(pdb->tdb, "INFO/nextjob", jobid)==-1) {
|
---|
| 2331 | DEBUG(3, ("allocate_print_jobid: failed to store INFO/nextjob.\n"));
|
---|
| 2332 | tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
|
---|
| 2333 | return False;
|
---|
| 2334 | }
|
---|
| 2335 |
|
---|
| 2336 | /* We've finished with the INFO/nextjob lock. */
|
---|
| 2337 | tdb_unlock_bystring(pdb->tdb, "INFO/nextjob");
|
---|
| 2338 |
|
---|
| 2339 | if (!print_job_exists(sharename, jobid))
|
---|
| 2340 | break;
|
---|
| 2341 | }
|
---|
| 2342 |
|
---|
| 2343 | if (i > 2) {
|
---|
| 2344 | DEBUG(0, ("allocate_print_jobid: failed to allocate a print job for queue %s\n",
|
---|
| 2345 | sharename));
|
---|
| 2346 | /* Probably full... */
|
---|
| 2347 | errno = ENOSPC;
|
---|
| 2348 | return False;
|
---|
| 2349 | }
|
---|
| 2350 |
|
---|
| 2351 | /* Store a dummy placeholder. */
|
---|
| 2352 | {
|
---|
| 2353 | uint32_t tmp;
|
---|
| 2354 | TDB_DATA dum;
|
---|
| 2355 | dum.dptr = NULL;
|
---|
| 2356 | dum.dsize = 0;
|
---|
| 2357 | if (tdb_store(pdb->tdb, print_key(jobid, &tmp), dum,
|
---|
| 2358 | TDB_INSERT) == -1) {
|
---|
| 2359 | DEBUG(3, ("allocate_print_jobid: jobid (%d) failed to store placeholder.\n",
|
---|
| 2360 | jobid ));
|
---|
| 2361 | return False;
|
---|
| 2362 | }
|
---|
| 2363 | }
|
---|
| 2364 |
|
---|
| 2365 | *pjobid = jobid;
|
---|
| 2366 | return True;
|
---|
| 2367 | }
|
---|
| 2368 |
|
---|
| 2369 | /***************************************************************************
|
---|
| 2370 | Append a jobid to the 'jobs changed' list.
|
---|
| 2371 | ***************************************************************************/
|
---|
| 2372 |
|
---|
| 2373 | static bool add_to_jobs_changed(struct tdb_print_db *pdb, uint32 jobid)
|
---|
| 2374 | {
|
---|
| 2375 | TDB_DATA data;
|
---|
| 2376 | uint32 store_jobid;
|
---|
| 2377 |
|
---|
| 2378 | SIVAL(&store_jobid, 0, jobid);
|
---|
| 2379 | data.dptr = (uint8 *)&store_jobid;
|
---|
| 2380 | data.dsize = 4;
|
---|
| 2381 |
|
---|
| 2382 | DEBUG(10,("add_to_jobs_changed: Added jobid %u\n", (unsigned int)jobid ));
|
---|
| 2383 |
|
---|
| 2384 | return (tdb_append(pdb->tdb, string_tdb_data("INFO/jobs_changed"),
|
---|
| 2385 | data) == 0);
|
---|
| 2386 | }
|
---|
| 2387 |
|
---|
| 2388 | /***************************************************************************
|
---|
| 2389 | Start spooling a job - return the jobid.
|
---|
| 2390 | ***************************************************************************/
|
---|
| 2391 |
|
---|
| 2392 | uint32 print_job_start(struct current_user *user, int snum, char *jobname, NT_DEVICEMODE *nt_devmode )
|
---|
| 2393 | {
|
---|
| 2394 | uint32 jobid;
|
---|
| 2395 | char *path;
|
---|
| 2396 | struct printjob pjob;
|
---|
| 2397 | user_struct *vuser;
|
---|
| 2398 | const char *sharename = lp_const_servicename(snum);
|
---|
| 2399 | struct tdb_print_db *pdb = get_print_db_byname(sharename);
|
---|
| 2400 | int njobs;
|
---|
| 2401 |
|
---|
| 2402 | errno = 0;
|
---|
| 2403 |
|
---|
| 2404 | if (!pdb)
|
---|
| 2405 | return (uint32)-1;
|
---|
| 2406 |
|
---|
| 2407 | if (!print_access_check(user, snum, PRINTER_ACCESS_USE)) {
|
---|
| 2408 | DEBUG(3, ("print_job_start: job start denied by security descriptor\n"));
|
---|
| 2409 | release_print_db(pdb);
|
---|
| 2410 | return (uint32)-1;
|
---|
| 2411 | }
|
---|
| 2412 |
|
---|
| 2413 | if (!print_time_access_check(lp_servicename(snum))) {
|
---|
| 2414 | DEBUG(3, ("print_job_start: job start denied by time check\n"));
|
---|
| 2415 | release_print_db(pdb);
|
---|
| 2416 | return (uint32)-1;
|
---|
| 2417 | }
|
---|
| 2418 |
|
---|
| 2419 | path = lp_pathname(snum);
|
---|
| 2420 |
|
---|
| 2421 | /* see if we have sufficient disk space */
|
---|
| 2422 | if (lp_minprintspace(snum)) {
|
---|
| 2423 | SMB_BIG_UINT dspace, dsize;
|
---|
| 2424 | if (sys_fsusage(path, &dspace, &dsize) == 0 &&
|
---|
| 2425 | dspace < 2*(SMB_BIG_UINT)lp_minprintspace(snum)) {
|
---|
| 2426 | DEBUG(3, ("print_job_start: disk space check failed.\n"));
|
---|
| 2427 | release_print_db(pdb);
|
---|
| 2428 | errno = ENOSPC;
|
---|
| 2429 | return (uint32)-1;
|
---|
| 2430 | }
|
---|
| 2431 | }
|
---|
| 2432 |
|
---|
| 2433 | /* for autoloaded printers, check that the printcap entry still exists */
|
---|
| 2434 | if (lp_autoloaded(snum) && !pcap_printername_ok(lp_const_servicename(snum))) {
|
---|
| 2435 | DEBUG(3, ("print_job_start: printer name %s check failed.\n", lp_const_servicename(snum) ));
|
---|
| 2436 | release_print_db(pdb);
|
---|
| 2437 | errno = ENOENT;
|
---|
| 2438 | return (uint32)-1;
|
---|
| 2439 | }
|
---|
| 2440 |
|
---|
| 2441 | /* Insure the maximum queue size is not violated */
|
---|
| 2442 | if ((njobs = print_queue_length(snum,NULL)) > lp_maxprintjobs(snum)) {
|
---|
| 2443 | DEBUG(3, ("print_job_start: Queue %s number of jobs (%d) larger than max printjobs per queue (%d).\n",
|
---|
| 2444 | sharename, njobs, lp_maxprintjobs(snum) ));
|
---|
| 2445 | release_print_db(pdb);
|
---|
| 2446 | errno = ENOSPC;
|
---|
| 2447 | return (uint32)-1;
|
---|
| 2448 | }
|
---|
| 2449 |
|
---|
| 2450 | DEBUG(10,("print_job_start: Queue %s number of jobs (%d), max printjobs = %d\n",
|
---|
| 2451 | sharename, njobs, lp_maxprintjobs(snum) ));
|
---|
| 2452 |
|
---|
| 2453 | if (!allocate_print_jobid(pdb, snum, sharename, &jobid))
|
---|
| 2454 | goto fail;
|
---|
| 2455 |
|
---|
| 2456 | /* create the database entry */
|
---|
| 2457 |
|
---|
| 2458 | ZERO_STRUCT(pjob);
|
---|
| 2459 |
|
---|
| 2460 | pjob.pid = sys_getpid();
|
---|
| 2461 | pjob.sysjob = -1;
|
---|
| 2462 | pjob.fd = -1;
|
---|
| 2463 | pjob.starttime = time(NULL);
|
---|
| 2464 | pjob.status = LPQ_SPOOLING;
|
---|
| 2465 | pjob.size = 0;
|
---|
| 2466 | pjob.spooled = False;
|
---|
| 2467 | pjob.smbjob = True;
|
---|
| 2468 | pjob.nt_devmode = nt_devmode;
|
---|
| 2469 |
|
---|
| 2470 | fstrcpy(pjob.jobname, jobname);
|
---|
| 2471 |
|
---|
| 2472 | if ((vuser = get_valid_user_struct(user->vuid)) != NULL) {
|
---|
| 2473 | fstrcpy(pjob.user, lp_printjob_username(snum));
|
---|
| 2474 | standard_sub_advanced(sharename, vuser->user.smb_name, path,
|
---|
| 2475 | vuser->gid, vuser->user.smb_name,
|
---|
| 2476 | vuser->user.domain, pjob.user,
|
---|
| 2477 | sizeof(pjob.user) - 1);
|
---|
| 2478 | /* ensure NULL termination */
|
---|
| 2479 | pjob.user[sizeof(pjob.user)-1] = '\0';
|
---|
| 2480 | } else {
|
---|
| 2481 | fstrcpy(pjob.user, uidtoname(user->ut.uid));
|
---|
| 2482 | }
|
---|
| 2483 |
|
---|
| 2484 | fstrcpy(pjob.queuename, lp_const_servicename(snum));
|
---|
| 2485 |
|
---|
| 2486 | /* we have a job entry - now create the spool file */
|
---|
| 2487 | slprintf(pjob.filename, sizeof(pjob.filename)-1, "%s/%s%.8u.XXXXXX",
|
---|
| 2488 | path, PRINT_SPOOL_PREFIX, (unsigned int)jobid);
|
---|
| 2489 | pjob.fd = smb_mkstemp(pjob.filename);
|
---|
| 2490 |
|
---|
| 2491 | if (pjob.fd == -1) {
|
---|
| 2492 | if (errno == EACCES) {
|
---|
| 2493 | /* Common setup error, force a report. */
|
---|
| 2494 | DEBUG(0, ("print_job_start: insufficient permissions \
|
---|
| 2495 | to open spool file %s.\n", pjob.filename));
|
---|
| 2496 | } else {
|
---|
| 2497 | /* Normal case, report at level 3 and above. */
|
---|
| 2498 | DEBUG(3, ("print_job_start: can't open spool file %s,\n", pjob.filename));
|
---|
| 2499 | DEBUGADD(3, ("errno = %d (%s).\n", errno, strerror(errno)));
|
---|
| 2500 | }
|
---|
| 2501 | goto fail;
|
---|
| 2502 | }
|
---|
| 2503 |
|
---|
| 2504 | pjob_store(sharename, jobid, &pjob);
|
---|
| 2505 |
|
---|
| 2506 | /* Update the 'jobs changed' entry used by print_queue_status. */
|
---|
| 2507 | add_to_jobs_changed(pdb, jobid);
|
---|
| 2508 |
|
---|
| 2509 | /* Ensure we keep a rough count of the number of total jobs... */
|
---|
| 2510 | tdb_change_int32_atomic(pdb->tdb, "INFO/total_jobs", &njobs, 1);
|
---|
| 2511 |
|
---|
| 2512 | release_print_db(pdb);
|
---|
| 2513 |
|
---|
| 2514 | return jobid;
|
---|
| 2515 |
|
---|
| 2516 | fail:
|
---|
| 2517 | if (jobid != -1)
|
---|
| 2518 | pjob_delete(sharename, jobid);
|
---|
| 2519 |
|
---|
| 2520 | release_print_db(pdb);
|
---|
| 2521 |
|
---|
| 2522 | DEBUG(3, ("print_job_start: returning fail. Error = %s\n", strerror(errno) ));
|
---|
| 2523 | return (uint32)-1;
|
---|
| 2524 | }
|
---|
| 2525 |
|
---|
| 2526 | /****************************************************************************
|
---|
| 2527 | Update the number of pages spooled to jobid
|
---|
| 2528 | ****************************************************************************/
|
---|
| 2529 |
|
---|
| 2530 | void print_job_endpage(int snum, uint32 jobid)
|
---|
| 2531 | {
|
---|
| 2532 | const char* sharename = lp_const_servicename(snum);
|
---|
| 2533 | struct printjob *pjob;
|
---|
| 2534 |
|
---|
| 2535 | pjob = print_job_find(sharename, jobid);
|
---|
| 2536 | if (!pjob)
|
---|
| 2537 | return;
|
---|
| 2538 | /* don't allow another process to get this info - it is meaningless */
|
---|
| 2539 | if (pjob->pid != sys_getpid())
|
---|
| 2540 | return;
|
---|
| 2541 |
|
---|
| 2542 | pjob->page_count++;
|
---|
| 2543 | pjob_store(sharename, jobid, pjob);
|
---|
| 2544 | }
|
---|
| 2545 |
|
---|
| 2546 | /****************************************************************************
|
---|
| 2547 | Print a file - called on closing the file. This spools the job.
|
---|
| 2548 | If normal close is false then we're tearing down the jobs - treat as an
|
---|
| 2549 | error.
|
---|
| 2550 | ****************************************************************************/
|
---|
| 2551 |
|
---|
| 2552 | bool print_job_end(int snum, uint32 jobid, enum file_close_type close_type)
|
---|
| 2553 | {
|
---|
| 2554 | const char* sharename = lp_const_servicename(snum);
|
---|
| 2555 | struct printjob *pjob;
|
---|
| 2556 | int ret;
|
---|
| 2557 | SMB_STRUCT_STAT sbuf;
|
---|
| 2558 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 2559 |
|
---|
| 2560 | pjob = print_job_find(sharename, jobid);
|
---|
| 2561 |
|
---|
| 2562 | if (!pjob)
|
---|
| 2563 | return False;
|
---|
| 2564 |
|
---|
| 2565 | if (pjob->spooled || pjob->pid != sys_getpid())
|
---|
| 2566 | return False;
|
---|
| 2567 |
|
---|
| 2568 | if ((close_type == NORMAL_CLOSE || close_type == SHUTDOWN_CLOSE) &&
|
---|
| 2569 | (sys_fstat(pjob->fd, &sbuf) == 0)) {
|
---|
| 2570 | pjob->size = sbuf.st_size;
|
---|
| 2571 | close(pjob->fd);
|
---|
| 2572 | pjob->fd = -1;
|
---|
| 2573 | } else {
|
---|
| 2574 |
|
---|
| 2575 | /*
|
---|
| 2576 | * Not a normal close or we couldn't stat the job file,
|
---|
| 2577 | * so something has gone wrong. Cleanup.
|
---|
| 2578 | */
|
---|
| 2579 | close(pjob->fd);
|
---|
| 2580 | pjob->fd = -1;
|
---|
| 2581 | DEBUG(3,("print_job_end: failed to stat file for jobid %d\n", jobid ));
|
---|
| 2582 | goto fail;
|
---|
| 2583 | }
|
---|
| 2584 |
|
---|
| 2585 | /* Technically, this is not quite right. If the printer has a separator
|
---|
| 2586 | * page turned on, the NT spooler prints the separator page even if the
|
---|
| 2587 | * print job is 0 bytes. 010215 JRR */
|
---|
| 2588 | if (pjob->size == 0 || pjob->status == LPQ_DELETING) {
|
---|
| 2589 | /* don't bother spooling empty files or something being deleted. */
|
---|
| 2590 | DEBUG(5,("print_job_end: canceling spool of %s (%s)\n",
|
---|
| 2591 | pjob->filename, pjob->size ? "deleted" : "zero length" ));
|
---|
| 2592 | unlink(pjob->filename);
|
---|
| 2593 | pjob_delete(sharename, jobid);
|
---|
| 2594 | return True;
|
---|
| 2595 | }
|
---|
| 2596 |
|
---|
| 2597 | pjob->smbjob = jobid;
|
---|
| 2598 |
|
---|
| 2599 | ret = (*(current_printif->job_submit))(snum, pjob);
|
---|
| 2600 |
|
---|
| 2601 | if (ret)
|
---|
| 2602 | goto fail;
|
---|
| 2603 |
|
---|
| 2604 | /* The print job has been successfully handed over to the back-end */
|
---|
| 2605 |
|
---|
| 2606 | pjob->spooled = True;
|
---|
| 2607 | pjob->status = LPQ_QUEUED;
|
---|
| 2608 | pjob_store(sharename, jobid, pjob);
|
---|
| 2609 |
|
---|
| 2610 | /* make sure the database is up to date */
|
---|
| 2611 | if (print_cache_expired(lp_const_servicename(snum), True))
|
---|
| 2612 | print_queue_update(snum, False);
|
---|
| 2613 |
|
---|
| 2614 | return True;
|
---|
| 2615 |
|
---|
| 2616 | fail:
|
---|
| 2617 |
|
---|
| 2618 | /* The print job was not successfully started. Cleanup */
|
---|
| 2619 | /* Still need to add proper error return propagation! 010122:JRR */
|
---|
| 2620 | unlink(pjob->filename);
|
---|
| 2621 | pjob_delete(sharename, jobid);
|
---|
| 2622 | return False;
|
---|
| 2623 | }
|
---|
| 2624 |
|
---|
| 2625 | /****************************************************************************
|
---|
| 2626 | Get a snapshot of jobs in the system without traversing.
|
---|
| 2627 | ****************************************************************************/
|
---|
| 2628 |
|
---|
| 2629 | static bool get_stored_queue_info(struct tdb_print_db *pdb, int snum, int *pcount, print_queue_struct **ppqueue)
|
---|
| 2630 | {
|
---|
| 2631 | TDB_DATA data, cgdata;
|
---|
| 2632 | print_queue_struct *queue = NULL;
|
---|
| 2633 | uint32 qcount = 0;
|
---|
| 2634 | uint32 extra_count = 0;
|
---|
| 2635 | int total_count = 0;
|
---|
| 2636 | size_t len = 0;
|
---|
| 2637 | uint32 i;
|
---|
| 2638 | int max_reported_jobs = lp_max_reported_jobs(snum);
|
---|
| 2639 | bool ret = False;
|
---|
| 2640 | const char* sharename = lp_servicename(snum);
|
---|
| 2641 |
|
---|
| 2642 | /* make sure the database is up to date */
|
---|
| 2643 | if (print_cache_expired(lp_const_servicename(snum), True))
|
---|
| 2644 | print_queue_update(snum, False);
|
---|
| 2645 |
|
---|
| 2646 | *pcount = 0;
|
---|
| 2647 | *ppqueue = NULL;
|
---|
| 2648 |
|
---|
| 2649 | ZERO_STRUCT(data);
|
---|
| 2650 | ZERO_STRUCT(cgdata);
|
---|
| 2651 |
|
---|
| 2652 | /* Get the stored queue data. */
|
---|
| 2653 | data = tdb_fetch(pdb->tdb, string_tdb_data("INFO/linear_queue_array"));
|
---|
| 2654 |
|
---|
| 2655 | if (data.dptr && data.dsize >= sizeof(qcount))
|
---|
| 2656 | len += tdb_unpack(data.dptr + len, data.dsize - len, "d", &qcount);
|
---|
| 2657 |
|
---|
| 2658 | /* Get the changed jobs list. */
|
---|
| 2659 | cgdata = tdb_fetch(pdb->tdb, string_tdb_data("INFO/jobs_changed"));
|
---|
| 2660 | if (cgdata.dptr != NULL && (cgdata.dsize % 4 == 0))
|
---|
| 2661 | extra_count = cgdata.dsize/4;
|
---|
| 2662 |
|
---|
| 2663 | DEBUG(5,("get_stored_queue_info: qcount = %u, extra_count = %u\n", (unsigned int)qcount, (unsigned int)extra_count));
|
---|
| 2664 |
|
---|
| 2665 | /* Allocate the queue size. */
|
---|
| 2666 | if (qcount == 0 && extra_count == 0)
|
---|
| 2667 | goto out;
|
---|
| 2668 |
|
---|
| 2669 | if ((queue = SMB_MALLOC_ARRAY(print_queue_struct, qcount + extra_count)) == NULL)
|
---|
| 2670 | goto out;
|
---|
| 2671 |
|
---|
| 2672 | /* Retrieve the linearised queue data. */
|
---|
| 2673 |
|
---|
| 2674 | for( i = 0; i < qcount; i++) {
|
---|
| 2675 | uint32 qjob, qsize, qpage_count, qstatus, qpriority, qtime;
|
---|
| 2676 | len += tdb_unpack(data.dptr + len, data.dsize - len, "ddddddff",
|
---|
| 2677 | &qjob,
|
---|
| 2678 | &qsize,
|
---|
| 2679 | &qpage_count,
|
---|
| 2680 | &qstatus,
|
---|
| 2681 | &qpriority,
|
---|
| 2682 | &qtime,
|
---|
| 2683 | queue[i].fs_user,
|
---|
| 2684 | queue[i].fs_file);
|
---|
| 2685 | queue[i].job = qjob;
|
---|
| 2686 | queue[i].size = qsize;
|
---|
| 2687 | queue[i].page_count = qpage_count;
|
---|
| 2688 | queue[i].status = qstatus;
|
---|
| 2689 | queue[i].priority = qpriority;
|
---|
| 2690 | queue[i].time = qtime;
|
---|
| 2691 | }
|
---|
| 2692 |
|
---|
| 2693 | total_count = qcount;
|
---|
| 2694 |
|
---|
| 2695 | /* Add in the changed jobids. */
|
---|
| 2696 | for( i = 0; i < extra_count; i++) {
|
---|
| 2697 | uint32 jobid;
|
---|
| 2698 | struct printjob *pjob;
|
---|
| 2699 |
|
---|
| 2700 | jobid = IVAL(cgdata.dptr, i*4);
|
---|
| 2701 | DEBUG(5,("get_stored_queue_info: changed job = %u\n", (unsigned int)jobid));
|
---|
| 2702 | pjob = print_job_find(lp_const_servicename(snum), jobid);
|
---|
| 2703 | if (!pjob) {
|
---|
| 2704 | DEBUG(5,("get_stored_queue_info: failed to find changed job = %u\n", (unsigned int)jobid));
|
---|
| 2705 | remove_from_jobs_changed(sharename, jobid);
|
---|
| 2706 | continue;
|
---|
| 2707 | }
|
---|
| 2708 |
|
---|
| 2709 | queue[total_count].job = jobid;
|
---|
| 2710 | queue[total_count].size = pjob->size;
|
---|
| 2711 | queue[total_count].page_count = pjob->page_count;
|
---|
| 2712 | queue[total_count].status = pjob->status;
|
---|
| 2713 | queue[total_count].priority = 1;
|
---|
| 2714 | queue[total_count].time = pjob->starttime;
|
---|
| 2715 | fstrcpy(queue[total_count].fs_user, pjob->user);
|
---|
| 2716 | fstrcpy(queue[total_count].fs_file, pjob->jobname);
|
---|
| 2717 | total_count++;
|
---|
| 2718 | }
|
---|
| 2719 |
|
---|
| 2720 | /* Sort the queue by submission time otherwise they are displayed
|
---|
| 2721 | in hash order. */
|
---|
| 2722 |
|
---|
| 2723 | qsort(queue, total_count, sizeof(print_queue_struct), QSORT_CAST(printjob_comp));
|
---|
| 2724 |
|
---|
| 2725 | DEBUG(5,("get_stored_queue_info: total_count = %u\n", (unsigned int)total_count));
|
---|
| 2726 |
|
---|
| 2727 | if (max_reported_jobs && total_count > max_reported_jobs)
|
---|
| 2728 | total_count = max_reported_jobs;
|
---|
| 2729 |
|
---|
| 2730 | *ppqueue = queue;
|
---|
| 2731 | *pcount = total_count;
|
---|
| 2732 |
|
---|
| 2733 | ret = True;
|
---|
| 2734 |
|
---|
| 2735 | out:
|
---|
| 2736 |
|
---|
| 2737 | SAFE_FREE(data.dptr);
|
---|
| 2738 | SAFE_FREE(cgdata.dptr);
|
---|
| 2739 | return ret;
|
---|
| 2740 | }
|
---|
| 2741 |
|
---|
| 2742 | /****************************************************************************
|
---|
| 2743 | Get a printer queue listing.
|
---|
| 2744 | set queue = NULL and status = NULL if you just want to update the cache
|
---|
| 2745 | ****************************************************************************/
|
---|
| 2746 |
|
---|
| 2747 | int print_queue_status(int snum,
|
---|
| 2748 | print_queue_struct **ppqueue,
|
---|
| 2749 | print_status_struct *status)
|
---|
| 2750 | {
|
---|
| 2751 | fstring keystr;
|
---|
| 2752 | TDB_DATA data, key;
|
---|
| 2753 | const char *sharename;
|
---|
| 2754 | struct tdb_print_db *pdb;
|
---|
| 2755 | int count = 0;
|
---|
| 2756 |
|
---|
| 2757 | /* make sure the database is up to date */
|
---|
| 2758 |
|
---|
| 2759 | if (print_cache_expired(lp_const_servicename(snum), True))
|
---|
| 2760 | print_queue_update(snum, False);
|
---|
| 2761 |
|
---|
| 2762 | /* return if we are done */
|
---|
| 2763 | if ( !ppqueue || !status )
|
---|
| 2764 | return 0;
|
---|
| 2765 |
|
---|
| 2766 | *ppqueue = NULL;
|
---|
| 2767 | sharename = lp_const_servicename(snum);
|
---|
| 2768 | pdb = get_print_db_byname(sharename);
|
---|
| 2769 |
|
---|
| 2770 | if (!pdb)
|
---|
| 2771 | return 0;
|
---|
| 2772 |
|
---|
| 2773 | /*
|
---|
| 2774 | * Fetch the queue status. We must do this first, as there may
|
---|
| 2775 | * be no jobs in the queue.
|
---|
| 2776 | */
|
---|
| 2777 |
|
---|
| 2778 | ZERO_STRUCTP(status);
|
---|
| 2779 | slprintf(keystr, sizeof(keystr)-1, "STATUS/%s", sharename);
|
---|
| 2780 | key = string_tdb_data(keystr);
|
---|
| 2781 |
|
---|
| 2782 | data = tdb_fetch(pdb->tdb, key);
|
---|
| 2783 | if (data.dptr) {
|
---|
| 2784 | if (data.dsize == sizeof(*status)) {
|
---|
| 2785 | /* this memcpy is ok since the status struct was
|
---|
| 2786 | not packed before storing it in the tdb */
|
---|
| 2787 | memcpy(status, data.dptr, sizeof(*status));
|
---|
| 2788 | }
|
---|
| 2789 | SAFE_FREE(data.dptr);
|
---|
| 2790 | }
|
---|
| 2791 |
|
---|
| 2792 | /*
|
---|
| 2793 | * Now, fetch the print queue information. We first count the number
|
---|
| 2794 | * of entries, and then only retrieve the queue if necessary.
|
---|
| 2795 | */
|
---|
| 2796 |
|
---|
| 2797 | if (!get_stored_queue_info(pdb, snum, &count, ppqueue)) {
|
---|
| 2798 | release_print_db(pdb);
|
---|
| 2799 | return 0;
|
---|
| 2800 | }
|
---|
| 2801 |
|
---|
| 2802 | release_print_db(pdb);
|
---|
| 2803 | return count;
|
---|
| 2804 | }
|
---|
| 2805 |
|
---|
| 2806 | /****************************************************************************
|
---|
| 2807 | Pause a queue.
|
---|
| 2808 | ****************************************************************************/
|
---|
| 2809 |
|
---|
| 2810 | bool print_queue_pause(struct current_user *user, int snum, WERROR *errcode)
|
---|
| 2811 | {
|
---|
| 2812 | int ret;
|
---|
| 2813 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 2814 |
|
---|
| 2815 | if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
|
---|
| 2816 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2817 | return False;
|
---|
| 2818 | }
|
---|
| 2819 |
|
---|
| 2820 |
|
---|
| 2821 | become_root();
|
---|
| 2822 |
|
---|
| 2823 | ret = (*(current_printif->queue_pause))(snum);
|
---|
| 2824 |
|
---|
| 2825 | unbecome_root();
|
---|
| 2826 |
|
---|
| 2827 | if (ret != 0) {
|
---|
| 2828 | *errcode = WERR_INVALID_PARAM;
|
---|
| 2829 | return False;
|
---|
| 2830 | }
|
---|
| 2831 |
|
---|
| 2832 | /* force update the database */
|
---|
| 2833 | print_cache_flush(lp_const_servicename(snum));
|
---|
| 2834 |
|
---|
| 2835 | /* Send a printer notify message */
|
---|
| 2836 |
|
---|
| 2837 | notify_printer_status(snum, PRINTER_STATUS_PAUSED);
|
---|
| 2838 |
|
---|
| 2839 | return True;
|
---|
| 2840 | }
|
---|
| 2841 |
|
---|
| 2842 | /****************************************************************************
|
---|
| 2843 | Resume a queue.
|
---|
| 2844 | ****************************************************************************/
|
---|
| 2845 |
|
---|
| 2846 | bool print_queue_resume(struct current_user *user, int snum, WERROR *errcode)
|
---|
| 2847 | {
|
---|
| 2848 | int ret;
|
---|
| 2849 | struct printif *current_printif = get_printer_fns( snum );
|
---|
| 2850 |
|
---|
| 2851 | if (!print_access_check(user, snum, PRINTER_ACCESS_ADMINISTER)) {
|
---|
| 2852 | *errcode = WERR_ACCESS_DENIED;
|
---|
| 2853 | return False;
|
---|
| 2854 | }
|
---|
| 2855 |
|
---|
| 2856 | become_root();
|
---|
| 2857 |
|
---|
| 2858 | ret = (*(current_printif->queue_resume))(snum);
|
---|
| 2859 |
|
---|
| 2860 | unbecome_root();
|
---|
| 2861 |
|
---|
| 2862 | if (ret != 0) {
|
---|
| 2863 | *errcode = WERR_INVALID_PARAM;
|
---|
| 2864 | return False;
|
---|
| 2865 | }
|
---|
| 2866 |
|
---|
| 2867 | /* make sure the database is up to date */
|
---|
| 2868 | if (print_cache_expired(lp_const_servicename(snum), True))
|
---|
| 2869 | print_queue_update(snum, True);
|
---|
| 2870 |
|
---|
| 2871 | /* Send a printer notify message */
|
---|
| 2872 |
|
---|
| 2873 | notify_printer_status(snum, PRINTER_STATUS_OK);
|
---|
| 2874 |
|
---|
| 2875 | return True;
|
---|
| 2876 | }
|
---|
| 2877 |
|
---|
| 2878 | /****************************************************************************
|
---|
| 2879 | Purge a queue - implemented by deleting all jobs that we can delete.
|
---|
| 2880 | ****************************************************************************/
|
---|
| 2881 |
|
---|
| 2882 | bool print_queue_purge(struct current_user *user, int snum, WERROR *errcode)
|
---|
| 2883 | {
|
---|
| 2884 | print_queue_struct *queue;
|
---|
| 2885 | print_status_struct status;
|
---|
| 2886 | int njobs, i;
|
---|
| 2887 | bool can_job_admin;
|
---|
| 2888 |
|
---|
| 2889 | /* Force and update so the count is accurate (i.e. not a cached count) */
|
---|
| 2890 | print_queue_update(snum, True);
|
---|
| 2891 |
|
---|
| 2892 | can_job_admin = print_access_check(user, snum, JOB_ACCESS_ADMINISTER);
|
---|
| 2893 | njobs = print_queue_status(snum, &queue, &status);
|
---|
| 2894 |
|
---|
| 2895 | if ( can_job_admin )
|
---|
| 2896 | become_root();
|
---|
| 2897 |
|
---|
| 2898 | for (i=0;i<njobs;i++) {
|
---|
| 2899 | bool owner = is_owner(user, lp_const_servicename(snum), queue[i].job);
|
---|
| 2900 |
|
---|
| 2901 | if (owner || can_job_admin) {
|
---|
| 2902 | print_job_delete1(snum, queue[i].job);
|
---|
| 2903 | }
|
---|
| 2904 | }
|
---|
| 2905 |
|
---|
| 2906 | if ( can_job_admin )
|
---|
| 2907 | unbecome_root();
|
---|
| 2908 |
|
---|
| 2909 | /* update the cache */
|
---|
| 2910 | print_queue_update( snum, True );
|
---|
| 2911 |
|
---|
| 2912 | SAFE_FREE(queue);
|
---|
| 2913 |
|
---|
| 2914 | return True;
|
---|
| 2915 | }
|
---|