Debian Codegraph @

contrib/mod_shaper.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_shaper -- a module implementing daemon-wide rate throttling
3 * via IPC
4 * Copyright (c) 2004-2024 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_shaper, contrib software for proftpd 1.2 and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30 #include "privs.h"
31 #include "mod_ctrls.h"
32
33 #include <sys/mman.h>
34 #include <sys/ipc.h>
35 #include <sys/msg.h>
36 #include <sys/uio.h>
37
38 #define MOD_SHAPER_VERSION "mod_shaper/0.6.6"
39
40 /* Make sure the version of proftpd is as necessary. */
41 #if PROFTPD_VERSION_NUMBER < 0x0001030402
42 # error "ProFTPD 1.3.4rc2 or later required"
43 #endif
44
45 #ifndef PR_USE_CTRLS
46 # error "Controls support required (use --enable-ctrls)"
47 #endif
48
49 /* From src/main.c */
50 extern pid_t mpid;
51
52 module shaper_module;
53
54 static ctrls_acttab_t shaper_acttab[];
55 static const char *shaper_down_cmds[] = { C_RETR, NULL };
56 static const char *shaper_up_cmds[] = { C_APPE, C_STOR, C_STOU, NULL };
57 static int shaper_engine = FALSE;
58 static char *shaper_log_path = NULL;
59 static int shaper_logfd = -1;
60 static pool *shaper_pool = NULL;
61 static int shaper_qid = -1;
62 static unsigned long shaper_qmaxbytes = 0;
63 static int shaper_scrub_timer_id = -1;
64 static char *shaper_tab_path = NULL;
65 static pool *shaper_tab_pool = NULL;
66 static int shaper_tabfd = -1;
67
68 #define SHAPER_PROJ_ID 24
69
70 #if defined(FREEBSD4)
71 # define SHAPER_IOV_BASE (char *)
72 #elif defined(LINUX)
73 # define SHAPER_IOV_BASE (__ptr_t)
74 #elif defined(SOLARIS2)
75 # define SHAPER_IOV_BASE (caddr_t)
76 #else
77 # define SHAPER_IOV_BASE (void *)
78 #endif
79
80 #ifndef HAVE_FLOCK
81 # define LOCK_SH 1
82 # define LOCK_EX 2
83 # define LOCK_NB 4
84 # define LOCK_UN 8
85 #endif /* !HAVE_FLOCK */
86
87 #define SHAPER_DEFAULT_RATE -1.0
88 #define SHAPER_DEFAULT_PRIO 10
89 #define SHAPER_DEFAULT_SHARES 5
90 #define SHAPER_DEFAULT_UPSHARES SHAPER_DEFAULT_SHARES
91 #define SHAPER_DEFAULT_DOWNSHARES SHAPER_DEFAULT_SHARES
92
93 #define SHAPER_SCRUB_INTERVAL 60
94
95 #ifndef SHAPER_MAX_SEND_ATTEMPTS
96 # define SHAPER_MAX_SEND_ATTEMPTS 5
97 #endif
98
99 struct shaper_sess {
100 pid_t sess_pid;
101 unsigned int sess_prio;
102 int sess_downincr;
103 long double sess_downrate;
104 int sess_upincr;
105 long double sess_uprate;
106 };
107
108 struct {
109 int def_prio;
110 long double downrate;
111 unsigned int def_downshares;
112 long double uprate;
113 unsigned int def_upshares;
114 unsigned int nsessions;
115 array_header *sess_list;
116
117 } shaper_tab;
118
119 /* Define our own structure for messages, since one is not portably defined.
120 */
121 struct shaper_msg {
122 /* Message type */
123 long mtype;
124
125 /* Message data */
126 char mtext[1];
127 };
128
129 /* Necessary function prototypes. */
130 static void shaper_msg_clear(pid_t);
131 static int shaper_rate_alter(unsigned int, long double, long double);
132 static void shaper_sess_exit_ev(const void *, void *);
133 static void shaper_sigusr2_ev(const void *, void *);
134 static int shaper_table_send(void);
135
136 /* Support functions
137 */
138
139 static key_t shaper_get_key(const char *path) {
140 pr_fh_t *fh;
141 struct stat st;
142
143 /* ftok() uses stat(2) on the given path, which means that it needs to exist.
144 */
145 fh = pr_fsio_open(path, O_WRONLY|O_CREAT);
146 if (fh == NULL) {
147 int xerrno = errno;
148
149 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
150 "error opening '%s': %s", path, strerror(xerrno));
151
152 errno = xerrno;
153 return -1;
154 }
155
156 if (pr_fsio_fstat(fh, &st) < 0) {
157 int xerrno = errno;
158
159 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
160 "error checking '%s': %s", path, strerror(xerrno));
161
162 pr_fsio_close(fh);
163 errno = xerrno;
164 return -1;
165 }
166
167 if (S_ISDIR(st.st_mode)) {
168 int xerrno = EISDIR;
169
170 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
171 "error using '%s': %s", path, strerror(xerrno));
172
173 pr_fsio_close(fh);
174 errno = xerrno;
175 return -1;
176 }
177
178 pr_fsio_close(fh);
179
180 return ftok(path, SHAPER_PROJ_ID);
181 }
182
183 static int shaper_get_queue(const char *path) {
184 int qid;
185
186 /* Obtain a key for this path. */
187 key_t key = shaper_get_key(path);
188 if (key == (key_t) -1) {
189 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
190 "unable to get key for '%s': %s", path, strerror(errno));
191 return -1;
192 }
193
194 /* Try first using IPC_CREAT|IPC_EXCL, to check if there is an existing
195 * queue for this key. If there is, try again, using a flag of zero.
196 */
197 qid = msgget(key, IPC_CREAT|IPC_EXCL|0666);
198 if (qid < 0) {
199 if (errno == EEXIST)
200 qid = msgget(key, 0);
201
202 else
203 return -1;
204 }
205
206 return qid;
207 }
208
209 static int shaper_remove_queue(void) {
210 struct msqid_ds ds;
211 int res;
212
213 memset(&ds, 0, sizeof(ds));
214
215 res = msgctl(shaper_qid, IPC_RMID, &ds);
216 if (res < 0) {
217 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
218 "error removing queue ID %d: %s", shaper_qid, strerror(errno));
219
220 } else {
221 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
222 "removed queue ID %d", shaper_qid);
223 shaper_qid = -1;
224 }
225
226 return res;
227 }
228
229 static int shaper_msg_recv(void) {
230 int nmsgs = 0;
231 ssize_t msglen = 0;
232 struct shaper_msg *msg;
233
234 /* The expected message length consists of a priority (the unsigned int),
235 * the new downrate (the long double), and the new uprate (another long
236 * double).
237 */
238 size_t msgsz = sizeof(unsigned int) + sizeof(long double) +
239 sizeof(long double);
240
241 msg = malloc(sizeof(struct shaper_msg) + msgsz - sizeof(msg->mtext));
242 if (msg == NULL) {
243 pr_log_pri(PR_LOG_ALERT, MOD_SHAPER_VERSION ": Out of memory!");
244 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_NOMEM, NULL);
245 }
246
247 msglen = msgrcv(shaper_qid, msg, msgsz, getpid(), IPC_NOWAIT|MSG_NOERROR);
248 while (msglen > 0) {
249 unsigned int prio;
250 long double downrate, uprate;
251
252 pr_signals_handle();
253 nmsgs++;
254
255 memcpy(&prio, msg->mtext, sizeof(unsigned int));
256 memcpy(&downrate, msg->mtext + sizeof(unsigned int), sizeof(long double));
257 memcpy(&uprate, msg->mtext + sizeof(unsigned int) + sizeof(long double),
258 sizeof(long double));
259
260 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
261 "received prio %u, rate %3.2Lf down, %3.2Lf up", prio, downrate,
262 uprate);
263
264 if (shaper_rate_alter(prio, downrate, uprate) < 0) {
265 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
266 "error altering rate for current session: %s", strerror(errno));
267 }
268
269 msglen = msgrcv(shaper_qid, msg, msgsz, getpid(), IPC_NOWAIT|MSG_NOERROR);
270 }
271
272 free(msg);
273
274 if (msglen < 0 &&
275 #ifdef ENOMSG
276 errno != ENOMSG &&
277 #endif /* ENOMSG */
278 errno != EAGAIN)
279 return -1;
280
281 return nmsgs;
282 }
283
284 static int shaper_msg_send(pid_t dst_pid, unsigned int prio,
285 long double downrate, long double uprate) {
286 unsigned int error_count = 0;
287 int res;
288 struct shaper_msg *msg;
289 size_t msgsz = sizeof(unsigned int) + sizeof(long double) +
290 sizeof(long double);
291
292 msg = malloc(sizeof(struct shaper_msg) + msgsz - sizeof(msg->mtext));
293 if (msg == NULL) {
294 pr_log_pri(PR_LOG_ALERT, MOD_SHAPER_VERSION ": Out of memory!");
295 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_NOMEM, NULL);
296 }
297
298 msg->mtype = dst_pid;
299 memcpy(msg->mtext, &prio, sizeof(unsigned int));
300 memcpy(msg->mtext + sizeof(unsigned int), &downrate, sizeof(long double));
301 memcpy(msg->mtext + sizeof(unsigned int) + sizeof(long double), &uprate,
302 sizeof(long double));
303
304 /* Remove any old messages in the queue for the destination PID. This
305 * helps keep the queue clear and moving, more resistant to (inadvertent
306 * or not) DoS situations.
307 */
308 shaper_msg_clear(dst_pid);
309
310 while (msgsnd(shaper_qid, msg, msgsz, IPC_NOWAIT) < 0) {
311 pr_signals_handle();
312
313 if (errno != EAGAIN) {
314 free(msg);
315 return -1;
316
317 } else {
318 /* The EAGAIN error happens when there are too many bytes of messages
319 * on the queue. Check to see what the current number of messages
320 * on the queue is, and log the error.
321 *
322 * If this error is hit too many times in a loop, we may need to give
323 * up permanently. (XXX in the future, if one queue is too small for
324 * a busy daemon, look into a different queue allocation strategy.)
325 */
326 struct msqid_ds ds;
327
328 if (msgctl(shaper_qid, IPC_STAT, &ds) < 0) {
329 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
330 "error checking queue ID %d: %s", shaper_qid, strerror(errno));
331
332 } else {
333 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
334 "unable to send message to PID %lu via queue ID %d, max bytes (%lu) "
335 "reached (%lu messages of %lu bytes currently in queue)",
336 (unsigned long) dst_pid, shaper_qid, shaper_qmaxbytes,
337 (unsigned long) ds.msg_qnum, (unsigned long) ds.msg_qnum * msgsz);
338 }
339
340 error_count++;
341 if (error_count > SHAPER_MAX_SEND_ATTEMPTS) {
342 free(msg);
343
344 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
345 "unable to send message to PID %lu via queue ID %d after %u attempts "
346 "(%u max attempts allowed), failing", (unsigned long) dst_pid,
347 shaper_qid, error_count, SHAPER_MAX_SEND_ATTEMPTS);
348
349 errno = EPERM;
350 return -1;
351 }
352 }
353
354 }
355 free(msg);
356
357 /* Send SIGUSR2 to the destination process, to let it know that it should
358 * check the queue for messages.
359 */
360 PRIVS_ROOT
361 res = kill(dst_pid, SIGUSR2);
362 PRIVS_RELINQUISH
363
364 if (res < 0) {
365 if (errno == ESRCH) {
366 shaper_msg_clear(dst_pid);
367
368 } else {
369 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
370 "error sending notice: %s", strerror(errno));
371 }
372 }
373
374 #if 0
375 /* Handle our own signal, if necessary. */
376 if (getpid() == dst_pid)
377 pr_signals_handle();
378 #endif
379
380 return 0;
381 }
382
383 static void shaper_msg_clear(pid_t dst_pid) {
384 ssize_t msglen = 0;
385 struct shaper_msg *msg;
386 size_t msgsz = sizeof(unsigned int) + sizeof(long double) +
387 sizeof(long double);
388
389 msg = malloc(sizeof(struct shaper_msg) + msgsz - sizeof(msg->mtext));
390 if (msg == NULL) {
391 pr_log_pri(PR_LOG_ALERT, MOD_SHAPER_VERSION ": Out of memory!");
392 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_NOMEM, NULL);
393 }
394
395 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
396 "clearing queue ID %d of messages for process ID %lu", shaper_qid,
397 (unsigned long) dst_pid);
398 msglen = msgrcv(shaper_qid, msg, msgsz, dst_pid, IPC_NOWAIT|MSG_NOERROR);
399 while (msglen > 0) {
400 pr_signals_handle();
401
402 msglen = msgrcv(shaper_qid, msg, msgsz, dst_pid, IPC_NOWAIT|MSG_NOERROR);
403 }
404
405 free(msg);
406 }
407
408 static void shaper_remove_config(unsigned int prio) {
409 config_rec *c;
410 register unsigned int i;
411 pool *tmp_pool = make_sub_pool(shaper_pool);
412 array_header *list = make_array(tmp_pool, 1, sizeof(config_rec *));
413
414 /* This function is very similar to remove_config(), except that we
415 * want to remove only TransferRate config_recs, and only those
416 * config_recs whose priority matches the given priority.
417 *
418 * To do this, first we'll do a recursive scan for all TransferRate
419 * config_recs that match our priority, and add them to a list.
420 * Then we'll loop through the list, removing them from the config
421 * tree. It can't be done in one loop, as we'll not be able to
422 * track which config_recs we've seen and left alone before.
423 */
424
425 c = find_config(main_server->conf, CONF_PARAM, "TransferRate", TRUE);
426 while (c) {
427 pr_signals_handle();
428
429 if (*((unsigned int *) c->argv[3]) == prio)
430 *((config_rec **) push_array(list)) = c;
431
432 c = find_config_next(c, c->next, CONF_PARAM, "TransferRate", TRUE);
433 }
434
435 for (i = 0; i < list->nelts; i++) {
436 xaset_t *set;
437
438 c = ((config_rec **) list->elts)[i];
439 set = c->set;
440
441 xaset_remove(set, (xasetmember_t *) c);
442
443 if (!set->xas_list) {
444 if (c->parent && c->parent->subset == set)
445 c->parent->subset = NULL;
446
447 else if (main_server->conf == set)
448 main_server->conf = NULL;
449
450 destroy_pool(set->pool);
451
452 } else {
453 destroy_pool(c->pool);
454 }
455 }
456
457 destroy_pool(tmp_pool);
458 return;
459 }
460
461 static int shaper_rate_alter(unsigned int prio, long double downrate,
462 long double uprate) {
463 config_rec *c;
464
465 /* Remove any TransferRate entries at this same priority. */
466 shaper_remove_config(prio);
467
468 /* Create separate TransferRate entries for the download and upload
469 * rates, for now. It would be more efficient to have a single config_rec
470 * entry, but only when the downrate and uprate are the same.
471 */
472
473 if (downrate > 0.0) {
474 c = add_config_param_set(&main_server->conf, "TransferRate", 4, NULL,
475 NULL, NULL, NULL);
476 c->argv[0] = shaper_down_cmds;
477
478 c->argv[1] = pcalloc(c->pool, sizeof(long double));
479 *((long double *) c->argv[1]) = downrate;
480
481 /* No freebytes altered via mod_shaper. */
482 c->argv[2] = pcalloc(c->pool, sizeof(off_t));
483 *((off_t *) c->argv[2]) = 0;
484
485 c->argv[3] = pcalloc(c->pool, sizeof(unsigned int));
486 *((unsigned int *) c->argv[3]) = prio;
487
488 c->flags |= CF_MERGEDOWN_MULTI;
489 }
490
491 if (uprate > 0.0) {
492 c = add_config_param_set(&main_server->conf, "TransferRate", 4, NULL,
493 NULL, NULL, NULL);
494 c->argv[0] = shaper_up_cmds;
495
496 c->argv[1] = pcalloc(c->pool, sizeof(long double));
497 *((long double *) c->argv[1]) = uprate;
498
499 /* No freebytes altered via mod_shaper. */
500 c->argv[2] = pcalloc(c->pool, sizeof(off_t));
501 *((off_t *) c->argv[2]) = 0;
502
503 c->argv[3] = pcalloc(c->pool, sizeof(unsigned int));
504 *((unsigned int *) c->argv[3]) = prio;
505
506 c->flags |= CF_MERGEDOWN_MULTI;
507 }
508
509 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
510 "merging in new TransferRate entries");
511 fixup_dirs(main_server, CF_SILENT);
512
513 return 0;
514 }
515
516 /* Flush the ShaperTable out to disk. */
517 static int shaper_table_flush(void) {
518 register unsigned int i;
519 int res;
520 struct iovec tab_iov[6];
521 struct shaper_sess *sess_list;
522
523 /* Seek to the start of the file. */
524 if (lseek(shaper_tabfd, 0, SEEK_SET) == (off_t) -1) {
525 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
526 "error seeking to start of ShaperTable: %s", strerror(errno));
527 return -1;
528 }
529
530 /* Write out the ShaperTable header. */
531 tab_iov[0].iov_base = SHAPER_IOV_BASE &shaper_tab.def_prio;
532 tab_iov[0].iov_len = sizeof(shaper_tab.def_prio);
533
534 tab_iov[1].iov_base = SHAPER_IOV_BASE &shaper_tab.downrate;
535 tab_iov[1].iov_len = sizeof(shaper_tab.downrate);
536
537 tab_iov[2].iov_base = SHAPER_IOV_BASE &shaper_tab.def_downshares;
538 tab_iov[2].iov_len = sizeof(shaper_tab.def_downshares);
539
540 tab_iov[3].iov_base = SHAPER_IOV_BASE &shaper_tab.uprate;
541 tab_iov[3].iov_len = sizeof(shaper_tab.uprate);
542
543 tab_iov[4].iov_base = SHAPER_IOV_BASE &shaper_tab.def_upshares;
544 tab_iov[4].iov_len = sizeof(shaper_tab.def_upshares);
545
546 tab_iov[5].iov_base = SHAPER_IOV_BASE &shaper_tab.nsessions;
547 tab_iov[5].iov_len = sizeof(shaper_tab.nsessions);
548
549 res = writev(shaper_tabfd, tab_iov, 6);
550 if (res < 0) {
551 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
552 "error writing ShaperTable (%d) header: %s", shaper_tabfd,
553 strerror(errno));
554 return -1;
555 }
556
557 sess_list = shaper_tab.sess_list->elts;
558 for (i = 0; i < shaper_tab.nsessions; i++) {
559 tab_iov[0].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_pid;
560 tab_iov[0].iov_len = sizeof(pid_t);
561
562 tab_iov[1].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_prio;
563 tab_iov[1].iov_len = sizeof(unsigned int);
564
565 tab_iov[2].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_downincr;
566 tab_iov[2].iov_len = sizeof(int);
567
568 tab_iov[3].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_downrate;
569 tab_iov[3].iov_len = sizeof(long double);
570
571 tab_iov[4].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_upincr;
572 tab_iov[4].iov_len = sizeof(int);
573
574 tab_iov[5].iov_base = SHAPER_IOV_BASE &sess_list[i].sess_uprate;
575 tab_iov[5].iov_len = sizeof(long double);
576
577 res = writev(shaper_tabfd, tab_iov, 6);
578 if (res < 0)
579 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
580 "error writing ShaperTable session entry: %s", strerror(errno));
581 }
582
583 return 0;
584 }
585
586 #ifndef HAVE_FLOCK
587 static const char *get_lock_type(struct flock *lock) {
588 const char *lock_type;
589
590 switch (lock->l_type) {
591 case F_RDLCK:
592 lock_type = "read";
593 break;
594
595 case F_WRLCK:
596 lock_type = "write";
597 break;
598
599 case F_UNLCK:
600 lock_type = "unlock";
601 break;
602
603 default:
604 lock_type = "[unknown]";
605 }
606
607 return lock_type;
608 }
609 #endif /* !HAVE_FLOCK */
610
611 static int shaper_table_lock(int op) {
612 static int have_lock = FALSE;
613
614 #ifndef HAVE_FLOCK
615 int flag;
616 struct flock lock;
617 #endif /* !HAVE_FLOCK */
618
619 if (have_lock &&
620 ((op & LOCK_SH) || (op & LOCK_EX))) {
621 return 0;
622 }
623
624 if (!have_lock &&
625 (op & LOCK_UN)) {
626 return 0;
627 }
628
629 #ifdef HAVE_FLOCK
630 pr_trace_msg("lock", 9, "attempting to %s ShaperTable fd %d via flock(2)",
631 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd);
632 while (flock(shaper_tabfd, op) < 0) {
633 int xerrno = errno;
634
635 if (xerrno == EINTR) {
636 pr_signals_handle();
637 continue;
638 }
639
640 pr_trace_msg("lock", 9, "%s of ShaperTable fd %d failed: %s",
641 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd, strerror(xerrno));
642
643 errno = xerrno;
644 return -1;
645 }
646
647 pr_trace_msg("lock", 9, "%s of ShaperTable fd %d successful",
648 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd);
649
650 if ((op & LOCK_SH) ||
651 (op & LOCK_EX)) {
652 have_lock = TRUE;
653
654 } else if (op & LOCK_UN) {
655 have_lock = FALSE;
656 }
657
658 return 0;
659 #else
660 flag = F_SETLKW;
661
662 lock.l_whence = 0;
663 lock.l_start = lock.l_len = 0;
664
665 if (op & LOCK_SH) {
666 lock.l_type = F_RDLCK;
667
668 } else if (op & LOCK_EX) {
669 lock.l_type = F_WRLCK;
670
671 } else if (op & LOCK_UN) {
672 lock.l_type = F_UNLCK;
673
674 } else {
675 errno = EINVAL;
676 return -1;
677 }
678
679 if (op & LOCK_NB)
680 flag = F_SETLK;
681
682 pr_trace_msg("lock", 9, "attempting to %s ShaperTable fd %d via fcntl(2)",
683 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd);
684 while (fcntl(shaper_tabfd, flag, &lock) < 0) {
685 int xerrno = errno;
686
687 if (xerrno == EINTR) {
688 pr_signals_handle();
689 continue;
690 }
691
692 pr_trace_msg("lock", 9, "%s of ShaperTable fd %d failed: %s",
693 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd, strerror(xerrno));
694
695 if (xerrno == EACCES) {
696 /* Get the PID of the process blocking this lock. */
697 if (fcntl(shaper_tabfd, F_GETLK, &lock) == 0) {
698 pr_trace_msg("lock", 3, "process ID %lu has blocking %s lock on "
699 "ShaperTable fd %d", (unsigned long) lock.l_pid, get_lock_type(&lock),
700 shaper_tabfd);
701 }
702 }
703
704 errno = xerrno;
705 return -1;
706 }
707
708 pr_trace_msg("lock", 9, "%s of ShaperTable fd %d successful",
709 op == LOCK_UN ? "unlock" : "lock", shaper_tabfd);
710
711 if ((op & LOCK_SH) ||
712 (op & LOCK_EX)) {
713 have_lock = TRUE;
714
715 } else if (op & LOCK_UN) {
716 have_lock = FALSE;
717 }
718
719 return 0;
720 #endif /* HAVE_FLOCK */
721 }
722
723 static int shaper_table_init(pr_fh_t *fh) {
724 unsigned int nsessions = 0;
725 struct stat st;
726 struct iovec tab_iov[6];
727
728 if (pr_fsio_fstat(fh, &st) < 0) {
729 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
730 "unable to fstat ShaperTable: %s", strerror(errno));
731 errno = EINVAL;
732 return -1;
733 }
734
735 shaper_tabfd = fh->fh_fd;
736
737 /* XXX maybe add a shaper control to clear/re-init the table, in cases
738 * where the format changes?
739 */
740
741 /* If the table already exists (i.e. size > 0), return. */
742 if (st.st_size > 0) {
743 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
744 "ShaperTable '%s' has size %" PR_LU " bytes, is already initialized",
745 fh->fh_path, (pr_off_t) st.st_size);
746 return 0;
747 }
748
749 tab_iov[0].iov_base = SHAPER_IOV_BASE &shaper_tab.def_prio;
750 tab_iov[0].iov_len = sizeof(shaper_tab.def_prio);
751
752 tab_iov[1].iov_base = SHAPER_IOV_BASE &shaper_tab.downrate;
753 tab_iov[1].iov_len = sizeof(shaper_tab.downrate);
754
755 tab_iov[2].iov_base = SHAPER_IOV_BASE &shaper_tab.def_downshares;
756 tab_iov[2].iov_len = sizeof(shaper_tab.def_downshares);
757
758 tab_iov[3].iov_base = SHAPER_IOV_BASE &shaper_tab.uprate;
759 tab_iov[3].iov_len = sizeof(shaper_tab.uprate);
760
761 tab_iov[4].iov_base = SHAPER_IOV_BASE &shaper_tab.def_upshares;
762 tab_iov[4].iov_len = sizeof(shaper_tab.def_upshares);
763
764 tab_iov[5].iov_base = SHAPER_IOV_BASE &nsessions;
765 tab_iov[5].iov_len = sizeof(nsessions);
766
767 if (lseek(fh->fh_fd, 0, SEEK_SET) < 0) {
768 return -1;
769 }
770
771 if (writev(fh->fh_fd, tab_iov, 6) < 0) {
772 return -1;
773 }
774
775 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
776 "initialized ShaperTable with rate %3.2Lf KB/s (down), %3.2Lf KB/s (up), "
777 "default priority %u, default shares %u down, %u up", shaper_tab.downrate,
778 shaper_tab.uprate, shaper_tab.def_prio, shaper_tab.def_downshares,
779 shaper_tab.def_upshares);
780
781 return 0;
782 }
783
784 /* Refresh the in-memory ShaperTable from disk. */
785 static int shaper_table_refresh(void) {
786 register unsigned int i;
787 int res;
788 struct iovec tab_iov[6];
789
790 if (lseek(shaper_tabfd, 0, SEEK_SET) == (off_t) -1) {
791 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
792 "error seeking to start of ShaperTable: %s", strerror(errno));
793 return -1;
794 }
795
796 /* Read the ShaperTable header. */
797
798 tab_iov[0].iov_base = SHAPER_IOV_BASE &shaper_tab.def_prio;
799 tab_iov[0].iov_len = sizeof(shaper_tab.def_prio);
800
801 tab_iov[1].iov_base = SHAPER_IOV_BASE &shaper_tab.downrate;
802 tab_iov[1].iov_len = sizeof(shaper_tab.downrate);
803
804 tab_iov[2].iov_base = SHAPER_IOV_BASE &shaper_tab.def_downshares;
805 tab_iov[2].iov_len = sizeof(shaper_tab.def_downshares);
806
807 tab_iov[3].iov_base = SHAPER_IOV_BASE &shaper_tab.uprate;
808 tab_iov[3].iov_len = sizeof(shaper_tab.uprate);
809
810 tab_iov[4].iov_base = SHAPER_IOV_BASE &shaper_tab.def_upshares;
811 tab_iov[4].iov_len = sizeof(shaper_tab.def_upshares);
812
813 tab_iov[5].iov_base = SHAPER_IOV_BASE &shaper_tab.nsessions;
814 tab_iov[5].iov_len = sizeof(shaper_tab.nsessions);
815
816 res = readv(shaper_tabfd, tab_iov, 6);
817 if (res < 0) {
818 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
819 "error reading ShaperTable header: %s", strerror(errno));
820 return -1;
821 }
822
823 /* For every session, read in its information and add it to the list.
824 * For this, we need a pool for the session list.
825 */
826
827 if (shaper_tab_pool) {
828 destroy_pool(shaper_tab_pool);
829 shaper_tab_pool = NULL;
830 shaper_tab.sess_list = NULL;
831 }
832
833 shaper_tab_pool = make_sub_pool(shaper_pool);
834 pr_pool_tag(shaper_tab_pool, MOD_SHAPER_VERSION ": ShaperTable pool");
835
836 shaper_tab.sess_list = make_array(shaper_tab_pool, 0,
837 sizeof(struct shaper_sess));
838
839 for (i = 0; i < shaper_tab.nsessions; i++) {
840 struct shaper_sess *sess = push_array(shaper_tab.sess_list);
841
842 tab_iov[0].iov_base = SHAPER_IOV_BASE &sess->sess_pid;
843 tab_iov[0].iov_len = sizeof(sess->sess_pid);
844
845 tab_iov[1].iov_base = SHAPER_IOV_BASE &sess->sess_prio;
846 tab_iov[1].iov_len = sizeof(sess->sess_prio);
847
848 tab_iov[2].iov_base = SHAPER_IOV_BASE &sess->sess_downincr;
849 tab_iov[2].iov_len = sizeof(sess->sess_downincr);
850
851 tab_iov[3].iov_base = SHAPER_IOV_BASE &sess->sess_downrate;
852 tab_iov[3].iov_len = sizeof(sess->sess_downrate);
853
854 tab_iov[4].iov_base = SHAPER_IOV_BASE &sess->sess_upincr;
855 tab_iov[4].iov_len = sizeof(sess->sess_upincr);
856
857 tab_iov[5].iov_base = SHAPER_IOV_BASE &sess->sess_uprate;
858 tab_iov[5].iov_len = sizeof(sess->sess_uprate);
859
860 res = readv(shaper_tabfd, tab_iov, 6);
861 if (res < 0) {
862 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
863 "error reading session entry %u from ShaperTable: %s", i + 1,
864 strerror(errno));
865 return -1;
866 }
867 }
868
869 return 0;
870 }
871
872 /* Scan the ShaperTable for any sessions who might have exited in a Bad Way
873 * and not cleaned up their entries.
874 */
875 static void shaper_table_scrub(void) {
876 register unsigned int i;
877 struct shaper_sess *sess_list;
878 array_header *new_sess_list;
879 int send_tab = FALSE;
880
881 if (shaper_table_lock(LOCK_EX) < 0)
882 return;
883
884 if (shaper_table_refresh() < 0) {
885 shaper_table_lock(LOCK_UN);
886 return;
887 }
888
889 if (shaper_tab.nsessions == 0) {
890 /* No sessions in the ShaperTable to be removed. */
891 shaper_table_lock(LOCK_UN);
892 return;
893 }
894
895 sess_list = shaper_tab.sess_list->elts;
896 new_sess_list = make_array(shaper_tab_pool, 0, sizeof(struct shaper_sess));
897
898 for (i = 0; i < shaper_tab.nsessions; i++) {
899
900 /* Check to see if the PID in this entry is valid. If not, erase
901 * the slot.
902 */
903 if (kill(sess_list[i].sess_pid, 0) < 0) {
904 if (errno == ESRCH) {
905
906 /* OK, the recorded PID is no longer valid. */
907 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
908 "removed dead session (pid %u) from ShaperTable",
909 (unsigned int) sess_list[i].sess_pid);
910 send_tab = TRUE;
911 }
912
913 } else {
914 struct shaper_sess *sess = push_array(new_sess_list);
915
916 sess->sess_pid = sess_list[i].sess_pid;
917 sess->sess_prio = sess_list[i].sess_prio;
918 sess->sess_downincr = sess_list[i].sess_downincr;
919 sess->sess_downrate = sess_list[i].sess_downrate;
920 sess->sess_upincr = sess_list[i].sess_upincr;
921 sess->sess_uprate = sess_list[i].sess_uprate;
922 }
923 }
924
925 /* Replace the session list. The memory pointed to by the overwritten
926 * pointer will be freed when shaper_tab_pool is freed, which will be
927 * when the table is next refreshed.
928 */
929
930 shaper_tab.nsessions = new_sess_list->nelts;
931 shaper_tab.sess_list = new_sess_list;
932
933 if (send_tab && shaper_table_send() < 0) {
934 shaper_table_lock(LOCK_UN);
935 return;
936 }
937
938 if (shaper_table_flush() < 0) {
939 shaper_table_lock(LOCK_UN);
940 return;
941 }
942
943 shaper_table_lock(LOCK_UN);
944 return;
945 }
946
947 static int shaper_table_scrub_cb(CALLBACK_FRAME) {
948 shaper_table_scrub();
949
950 /* Always return 1, resetting the timer. */
951 return 1;
952 }
953
954 /* Scan the ShaperTable, sending messages to each session for their new rate
955 * and its priority.
956 */
957 static int shaper_table_send(void) {
958 register unsigned int i;
959 unsigned int total_downshares = 0, total_upshares = 0;
960 long double rate_per_downshare, rate_per_upshare;
961 struct shaper_sess *sess_list = shaper_tab.sess_list->elts;
962
963 for (i = 0; i < shaper_tab.nsessions; i++) {
964 total_downshares += (shaper_tab.def_downshares +
965 sess_list[i].sess_downincr);
966 total_upshares += (shaper_tab.def_upshares +
967 sess_list[i].sess_upincr);
968 }
969
970 if (total_downshares == 0) {
971 total_downshares = 1;
972 }
973
974 if (total_upshares == 0) {
975 total_upshares = 1;
976 }
977
978 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
979 "total session shares: %u down, %u up", total_downshares, total_upshares);
980
981 rate_per_downshare = shaper_tab.downrate / total_downshares;
982 rate_per_upshare = shaper_tab.uprate / total_upshares;
983
984 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
985 "rate per share: %3.2Lf down, %3.2Lf up", rate_per_downshare,
986 rate_per_upshare);
987
988 for (i = 0; i < shaper_tab.nsessions; i++) {
989 sess_list[i].sess_downrate = rate_per_downshare *
990 (shaper_tab.def_downshares + sess_list[i].sess_downincr);
991 sess_list[i].sess_uprate = rate_per_upshare *
992 (shaper_tab.def_upshares + sess_list[i].sess_upincr);
993
994 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
995 "pid %u has shares of %u down, %u up, sending rates of %3.2Lf down, "
996 "%3.2Lf up", (unsigned int) sess_list[i].sess_pid,
997 shaper_tab.def_downshares + sess_list[i].sess_downincr,
998 shaper_tab.def_upshares + sess_list[i].sess_upincr,
999 sess_list[i].sess_downrate, sess_list[i].sess_uprate);
1000
1001 if (shaper_msg_send(sess_list[i].sess_pid, sess_list[i].sess_prio,
1002 sess_list[i].sess_downrate, sess_list[i].sess_uprate) < 0) {
1003 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1004 "error sending msg to pid %u: %s",
1005 (unsigned int) sess_list[i].sess_pid, strerror(errno));
1006 }
1007 }
1008
1009 return 0;
1010 }
1011
1012 static int shaper_table_sess_add(pid_t sess_pid, unsigned int prio,
1013 int downincr, int upincr) {
1014 struct shaper_sess *sess;
1015
1016 if (shaper_table_lock(LOCK_EX) < 0) {
1017 return -1;
1018 }
1019
1020 if (shaper_table_refresh() < 0) {
1021 int xerrno = errno;
1022
1023 shaper_table_lock(LOCK_UN);
1024 errno = xerrno;
1025 return -1;
1026 }
1027
1028 shaper_tab.nsessions++;
1029 sess = push_array(shaper_tab.sess_list);
1030 sess->sess_pid = sess_pid;
1031
1032 if (prio != (unsigned int) -1) {
1033 sess->sess_prio = prio;
1034
1035 } else {
1036 sess->sess_prio = shaper_tab.def_prio;
1037 }
1038
1039 sess->sess_downincr = downincr;
1040 sess->sess_downrate = 0.0;
1041 sess->sess_upincr = upincr;
1042 sess->sess_uprate = 0.0;
1043
1044 if (shaper_table_send() < 0) {
1045 int xerrno = errno;
1046
1047 shaper_table_lock(LOCK_UN);
1048 errno = xerrno;
1049 return -1;
1050 }
1051
1052 if (shaper_table_flush() < 0) {
1053 int xerrno = errno;
1054
1055 shaper_table_lock(LOCK_UN);
1056 errno = xerrno;
1057 return -1;
1058 }
1059
1060 shaper_table_lock(LOCK_UN);
1061 return 0;
1062 }
1063
1064 static int shaper_table_sess_modify(pid_t sess_pid, unsigned int prio,
1065 int downincr, int upincr) {
1066 register unsigned int i;
1067 int found = FALSE, adj_down_ok = FALSE, adj_up_ok = FALSE;
1068 struct shaper_sess *sess_list;
1069
1070 if (shaper_table_lock(LOCK_EX) < 0)
1071 return -1;
1072
1073 if (shaper_table_refresh() < 0) {
1074 int xerrno = errno;
1075
1076 shaper_table_lock(LOCK_UN);
1077 errno = xerrno;
1078 return -1;
1079 }
1080
1081 /* XXX for large ShaperTables, this linear scan will increase the time
1082 * needed for adjusting sessions.
1083 */
1084 sess_list = shaper_tab.sess_list->elts;
1085 for (i = 0; i < shaper_tab.nsessions; i++) {
1086 if (sess_list[i].sess_pid != sess_pid)
1087 continue;
1088
1089 found = TRUE;
1090
1091 if ((shaper_tab.def_downshares + sess_list[i].sess_downincr +
1092 downincr) >= 1) {
1093 adj_down_ok = TRUE;
1094 sess_list[i].sess_downincr += downincr;
1095 }
1096
1097 if ((shaper_tab.def_upshares + sess_list[i].sess_upincr +
1098 upincr) >= 1) {
1099 adj_up_ok = TRUE;
1100 sess_list[i].sess_upincr += upincr;
1101 }
1102
1103 if (prio != (unsigned int) -1)
1104 sess_list[i].sess_prio = prio;
1105
1106 break;
1107 }
1108
1109 /* If the session was not found, or if the given adjustments were not OK,
1110 * do not send the changes out, but be done now.
1111 */
1112 if (!found || (!adj_down_ok && !adj_up_ok)) {
1113 shaper_table_lock(LOCK_UN);
1114
1115 if (!found)
1116 errno = ENOENT;
1117
1118 else if (!adj_down_ok) {
1119 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1120 "error modifying session: shares increment (%s%d) will drop "
1121 "session downshares (%u) below 1", downincr > 0 ? "+" : "", downincr,
1122 shaper_tab.def_downshares);
1123 errno = EINVAL;
1124
1125 } else if (!adj_up_ok) {
1126 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1127 "error modifying session: shares increment (%s%d) will drop "
1128 "session upshares (%u) below 1", upincr > 0 ? "+" : "", upincr,
1129 shaper_tab.def_upshares);
1130 errno = EINVAL;
1131 }
1132
1133 return -1;
1134 }
1135
1136 if (shaper_table_send() < 0) {
1137 int xerrno = errno;
1138
1139 shaper_table_lock(LOCK_UN);
1140 errno = xerrno;
1141 return -1;
1142 }
1143
1144 if (shaper_table_flush() < 0) {
1145 int xerrno = errno;
1146
1147 shaper_table_lock(LOCK_UN);
1148 errno = xerrno;
1149 return -1;
1150 }
1151
1152 shaper_table_lock(LOCK_UN);
1153 return 0;
1154 }
1155
1156 static int shaper_table_sess_remove(pid_t sess_pid) {
1157 register unsigned int i;
1158 int found = FALSE;
1159 struct shaper_sess *sess_list;
1160 array_header *new_sess_list;
1161
1162 if (shaper_table_lock(LOCK_EX) < 0)
1163 return -1;
1164
1165 if (shaper_table_refresh() < 0) {
1166 int xerrno = errno;
1167
1168 shaper_table_lock(LOCK_UN);
1169 errno = xerrno;
1170 return -1;
1171 }
1172
1173 if (shaper_tab.nsessions == 0) {
1174 /* No sessions in the ShaperTable to be removed. */
1175 shaper_table_lock(LOCK_UN);
1176 return 0;
1177 }
1178
1179 sess_list = shaper_tab.sess_list->elts;
1180 new_sess_list = make_array(shaper_tab_pool, 0, sizeof(struct shaper_sess));
1181
1182 for (i = 0; i < shaper_tab.nsessions; i++) {
1183 if (sess_list[i].sess_pid != sess_pid) {
1184 struct shaper_sess *sess = push_array(new_sess_list);
1185
1186 sess->sess_pid = sess_list[i].sess_pid;
1187 sess->sess_prio = sess_list[i].sess_prio;
1188 sess->sess_downincr = sess_list[i].sess_downincr;
1189 sess->sess_downrate = sess_list[i].sess_downrate;
1190 sess->sess_upincr = sess_list[i].sess_upincr;
1191 sess->sess_uprate = sess_list[i].sess_uprate;
1192
1193 } else
1194 found = TRUE;
1195 }
1196
1197 if (found)
1198 shaper_tab.nsessions--;
1199
1200 /* Replace the session list. The memory pointed to by the overwritten
1201 * pointer will be freed when shaper_tab_pool is freed, which will be
1202 * when the table is next refreshed.
1203 */
1204
1205 shaper_tab.sess_list = new_sess_list;
1206
1207 if (shaper_table_send() < 0) {
1208 int xerrno = errno;
1209
1210 shaper_table_lock(LOCK_UN);
1211 errno = xerrno;
1212 return -1;
1213 }
1214
1215 if (shaper_table_flush() < 0) {
1216 int xerrno = errno;
1217
1218 shaper_table_lock(LOCK_UN);
1219 errno = xerrno;
1220 return -1;
1221 }
1222
1223 shaper_table_lock(LOCK_UN);
1224 return 0;
1225 }
1226
1227 /* Control handlers
1228 */
1229
1230 /* usage: shaper all priority|rate|downrate|uprate|shares|downshares|upshares
1231 * val
1232 */
1233 static int shaper_handle_all(pr_ctrls_t *ctrl, int reqargc,
1234 char **reqargv) {
1235 register int i;
1236 int send_tab = TRUE;
1237
1238 if (reqargc < 2 ||
1239 reqargc > 14 ||
1240 reqargc % 2 != 0) {
1241 pr_ctrls_add_response(ctrl, "wrong number of parameters");
1242 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1243 }
1244
1245 if (shaper_table_lock(LOCK_EX) < 0) {
1246 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1247 "error write-locking ShaperTable: %s", strerror(errno));
1248 pr_ctrls_add_response(ctrl, "error handling request");
1249 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1250 }
1251
1252 if (shaper_table_refresh() < 0) {
1253 shaper_table_lock(LOCK_UN);
1254 pr_ctrls_add_response(ctrl, "error handling request");
1255 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1256 }
1257
1258 for (i = 0; i < reqargc;) {
1259 if (strcmp(reqargv[i], "downrate") == 0) {
1260 char *tmp;
1261 long double rate;
1262
1263 rate = strtod(reqargv[i+1], &tmp);
1264
1265 if (tmp && *tmp) {
1266 pr_ctrls_add_response(ctrl, "invalid downrate value (%s)",
1267 reqargv[i+1]);
1268 send_tab = FALSE;
1269 i += 2;
1270 continue;
1271 }
1272
1273 if (rate < 0.0) {
1274 pr_ctrls_add_response(ctrl, "downrate must be greater than 0 (%3.2Lf)",
1275 rate);
1276 send_tab = FALSE;
1277 i += 2;
1278 continue;
1279 }
1280
1281 shaper_tab.downrate = rate;
1282 pr_ctrls_add_response(ctrl, "overall downrate (%3.2Lf) set",
1283 shaper_tab.downrate);
1284
1285 i += 2;
1286
1287 } else if (strcmp(reqargv[i], "downshares") == 0) {
1288 int shares = atoi(reqargv[i+1]);
1289
1290 if (shares < 1) {
1291 pr_ctrls_add_response(ctrl, "downshares (%d) must be greater than 1",
1292 shares);
1293 send_tab = FALSE;
1294 i += 2;
1295 continue;
1296 }
1297
1298 shaper_tab.def_downshares = shares;
1299 pr_ctrls_add_response(ctrl, "default downshares (%u) set",
1300 shaper_tab.def_downshares);
1301
1302 i += 2;
1303
1304 } else if (strcmp(reqargv[i], "priority") == 0) {
1305 int prio = atoi(reqargv[i+1]);
1306
1307 if (prio < 0) {
1308 pr_ctrls_add_response(ctrl, "priority (%d) must be greater than 0",
1309 prio);
1310 send_tab = FALSE;
1311 i += 2;
1312 continue;
1313 }
1314
1315 shaper_tab.def_prio = prio;
1316 pr_ctrls_add_response(ctrl, "default priority (%u) set",
1317 shaper_tab.def_prio);
1318
1319 i += 2;
1320
1321 } else if (strcmp(reqargv[i], "rate") == 0) {
1322 char *tmp;
1323 long double rate;
1324
1325 rate = strtod(reqargv[i+1], &tmp);
1326
1327 if (tmp && *tmp) {
1328 pr_ctrls_add_response(ctrl, "invalid rate value (%s)", reqargv[i+1]);
1329 send_tab = FALSE;
1330 i += 2;
1331 continue;
1332 }
1333
1334 if (rate < 0.0) {
1335 pr_ctrls_add_response(ctrl, "rate must be greater than 0 (%3.2Lf)",
1336 rate);
1337 send_tab = FALSE;
1338 i += 2;
1339 continue;
1340 }
1341
1342 shaper_tab.downrate = rate;
1343 shaper_tab.uprate = rate;
1344 pr_ctrls_add_response(ctrl, "overall rates (%3.2Lf down, %3.2Lf up) set",
1345 shaper_tab.downrate, shaper_tab.uprate);
1346
1347 i += 2;
1348
1349 } else if (strcmp(reqargv[i], "shares") == 0) {
1350 int shares = atoi(reqargv[i+1]);
1351
1352 if (shares < 1) {
1353 pr_ctrls_add_response(ctrl, "shares (%d) must be greater than 1",
1354 shares);
1355 send_tab = FALSE;
1356 i += 2;
1357 continue;
1358 }
1359
1360 shaper_tab.def_downshares = shares;
1361 shaper_tab.def_upshares = shares;
1362 pr_ctrls_add_response(ctrl, "default shares (%u down, %u up) set",
1363 shaper_tab.def_downshares, shaper_tab.def_upshares);
1364
1365 i += 2;
1366
1367 } else if (strcmp(reqargv[i], "uprate") == 0) {
1368 char *tmp;
1369 long double rate;
1370
1371 rate = strtod(reqargv[i+1], &tmp);
1372
1373 if (tmp && *tmp) {
1374 pr_ctrls_add_response(ctrl, "invalid uprate value (%s)", reqargv[i+1]);
1375 send_tab = FALSE;
1376 i += 2;
1377 continue;
1378 }
1379
1380 if (rate < 0.0) {
1381 pr_ctrls_add_response(ctrl, "uprate must be greater than 0 (%3.2Lf)",
1382 rate);
1383 send_tab = FALSE;
1384 i += 2;
1385 continue;
1386 }
1387
1388 shaper_tab.uprate = rate;
1389 pr_ctrls_add_response(ctrl, "overall uprate (%3.2Lf) set",
1390 shaper_tab.uprate);
1391
1392 i += 2;
1393
1394 } else if (strcmp(reqargv[i], "upshares") == 0) {
1395 int shares = atoi(reqargv[i+1]);
1396
1397 if (shares < 1) {
1398 pr_ctrls_add_response(ctrl, "upshares (%d) must be greater than 1",
1399 shares);
1400 send_tab = FALSE;
1401 i += 2;
1402 continue;
1403 }
1404
1405 shaper_tab.def_upshares = shares;
1406 pr_ctrls_add_response(ctrl, "default upshares (%u) set",
1407 shaper_tab.def_upshares);
1408
1409 i += 2;
1410
1411 } else {
1412 pr_ctrls_add_response(ctrl, "unknown shaper all option '%s'",
1413 reqargv[i]);
1414 send_tab = FALSE;
1415 i += 2;
1416 continue;
1417 }
1418 }
1419
1420 if (send_tab == FALSE) {
1421 shaper_table_lock(LOCK_UN);
1422 return PR_CTRLS_STATUS_OPERATION_DENIED;
1423 }
1424
1425 if (shaper_table_send() < 0) {
1426 shaper_table_lock(LOCK_UN);
1427 pr_ctrls_add_response(ctrl, "error handling request");
1428 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1429 }
1430
1431 if (shaper_table_flush() < 0) {
1432 shaper_table_lock(LOCK_UN);
1433 pr_ctrls_add_response(ctrl, "error handling request");
1434 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1435 }
1436
1437 shaper_table_lock(LOCK_UN);
1438 return PR_CTRLS_STATUS_OK;
1439 }
1440
1441 /* usage: shaper info */
1442 static int shaper_handle_info(pr_ctrls_t *ctrl, int reqargc,
1443 char **reqargv) {
1444 register unsigned int i;
1445 struct shaper_sess *sess_list;
1446 unsigned int total_downshares = 0, total_upshares = 0;
1447 char *downbuf = NULL, *upbuf = NULL;
1448 size_t downbufsz = 14, upbufsz = 14;
1449
1450 if (shaper_table_lock(LOCK_SH) < 0) {
1451 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1452 "unable to read-lock ShaperTable: %s", strerror(errno));
1453 pr_ctrls_add_response(ctrl, "error handling request");
1454 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1455 }
1456
1457 if (shaper_table_refresh() < 0) {
1458 int xerrno = errno;
1459 shaper_table_lock(LOCK_UN);
1460
1461 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1462 "error refreshing ShaperTable: %s", strerror(xerrno));
1463 pr_ctrls_add_response(ctrl, "error handling request");
1464 return PR_CTRLS_STATUS_INTERNAL_ERROR;
1465 }
1466
1467 pr_ctrls_add_response(ctrl, "Overall Rates: %3.2Lf KB/s down, %3.2Lf KB/s up",
1468 shaper_tab.downrate, shaper_tab.uprate);
1469 pr_ctrls_add_response(ctrl, "Default Shares Per Session: %u down, %u up",
1470 shaper_tab.def_downshares, shaper_tab.def_upshares);
1471 pr_ctrls_add_response(ctrl, "Default Priority: %u", shaper_tab.def_prio);
1472 pr_ctrls_add_response(ctrl, "Number of Shaped Sessions: %u",
1473 shaper_tab.nsessions);
1474
1475 sess_list = shaper_tab.sess_list->elts;
1476
1477 for (i = 0; i < shaper_tab.nsessions; i++) {
1478 total_downshares += (shaper_tab.def_downshares +
1479 sess_list[i].sess_downincr);
1480 total_upshares += (shaper_tab.def_upshares +
1481 sess_list[i].sess_upincr);
1482 }
1483
1484 if (shaper_tab.nsessions > 0) {
1485 pr_ctrls_add_response(ctrl, "%-5s %8s %-14s %11s %-14s %11s",
1486 "PID", "Priority", "DShares", "DRate (KB/s)", "UShares", "URate (KB/s)");
1487 pr_ctrls_add_response(ctrl, "----- -------- -------------- ------------ -------------- ------------");
1488 downbuf = palloc(ctrl->ctrls_tmp_pool, downbufsz);
1489 upbuf = palloc(ctrl->ctrls_tmp_pool, upbufsz);
1490 }
1491
1492 for (i = 0; i < shaper_tab.nsessions; i++) {
1493 memset(downbuf, '\0', downbufsz);
1494 memset(upbuf, '\0', upbufsz);
1495
1496 pr_snprintf(downbuf, downbufsz, "%u/%u (%s%d)",
1497 shaper_tab.def_downshares + sess_list[i].sess_downincr, total_downshares,
1498 sess_list[i].sess_downincr > 0 ? "+" : "", sess_list[i].sess_downincr);
1499 downbuf[downbufsz-1] = '\0';
1500
1501 pr_snprintf(upbuf, upbufsz, "%u/%u (%s%d)",
1502 shaper_tab.def_upshares + sess_list[i].sess_upincr, total_upshares,
1503 sess_list[i].sess_upincr > 0 ? "+" : "", sess_list[i].sess_upincr);
1504 upbuf[upbufsz-1] = '\0';
1505
1506 pr_ctrls_add_response(ctrl, "%5u %8u %14s %11.2Lf %14s %11.2Lf",
1507 (unsigned int) sess_list[i].sess_pid, sess_list[i].sess_prio,
1508 downbuf, sess_list[i].sess_downrate, upbuf, sess_list[i].sess_uprate);
1509 }
1510
1511 shaper_table_lock(LOCK_UN);
1512 return PR_CTRLS_STATUS_OK;
1513 }
1514
1515 /* usage: shaper sess class|host|user name [priority prio] [shares incr]
1516 * [downshares incr] [upshares incr]
1517 */
1518 static int shaper_handle_sess(pr_ctrls_t *ctrl, int reqargc,
1519 char **reqargv) {
1520 register int i;
1521 int adjusted = FALSE, send_tab = TRUE;
1522 int prio = -1, downincr = 0, upincr = 0;
1523
1524 if (reqargc < 4 ||
1525 reqargc > 6 ||
1526 reqargc % 2 != 0) {
1527 pr_ctrls_add_response(ctrl, "wrong number of parameters");
1528 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1529 }
1530
1531 for (i = 2; i < reqargc;) {
1532 if (strcmp(reqargv[i], "downshares") == 0) {
1533
1534 if (*reqargv[i+1] != '+' && *reqargv[i+1] != '-') {
1535 pr_ctrls_add_response(ctrl,
1536 "downshares (%s) must start with '+' or '-'", reqargv[i+1]);
1537 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1538 }
1539
1540 downincr = atoi(reqargv[i+1]);
1541
1542 if (downincr == 0) {
1543 pr_ctrls_add_response(ctrl, "downshares cannot be 0");
1544 send_tab = FALSE;
1545 i += 2;
1546 continue;
1547 }
1548
1549 pr_ctrls_add_response(ctrl, "adjusted session downshares by %s%d",
1550 downincr > 0 ? "+" : "", downincr);
1551
1552 i += 2;
1553
1554 } else if (strcmp(reqargv[i], "priority") == 0) {
1555 prio = atoi(reqargv[i+1]);
1556
1557 if (prio < 0) {
1558 pr_ctrls_add_response(ctrl, "priority (%d) must be greater than 0",
1559 prio);
1560 send_tab = FALSE;
1561 i += 2;
1562 continue;
1563 }
1564
1565 pr_ctrls_add_response(ctrl, "set session priority to %u", prio);
1566 i += 2;
1567
1568 } else if (strcmp(reqargv[i], "shares") == 0) {
1569 int incr;
1570
1571 if (*reqargv[i+1] != '+' && *reqargv[i+1] != '-') {
1572 pr_ctrls_add_response(ctrl, "shares (%s) must start with '+' or '-'",
1573 reqargv[i+1]);
1574 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1575 }
1576
1577 incr = atoi(reqargv[i+1]);
1578
1579 if (incr == 0) {
1580 pr_ctrls_add_response(ctrl, "shares cannot be 0");
1581 send_tab = FALSE;
1582 i += 2;
1583 continue;
1584 }
1585
1586 pr_ctrls_add_response(ctrl,
1587 "adjusted session downshares and upshares by %s%d",
1588 incr > 0 ? "+" : "", incr);
1589
1590 downincr = upincr = incr;
1591 i += 2;
1592
1593 } else if (strcmp(reqargv[i], "upshares") == 0) {
1594
1595 if (*reqargv[i+1] != '+' && *reqargv[i+1] != '-') {
1596 pr_ctrls_add_response(ctrl,
1597 "upshares (%s) must start with '+' or '-'", reqargv[i+1]);
1598 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1599 }
1600
1601 upincr = atoi(reqargv[i+1]);
1602
1603 if (upincr == 0) {
1604 pr_ctrls_add_response(ctrl, "upshares cannot be 0");
1605 send_tab = FALSE;
1606 i += 2;
1607 continue;
1608 }
1609
1610 pr_ctrls_add_response(ctrl, "adjusted session upshares by %s%d",
1611 upincr > 0 ? "+" : "", upincr);
1612
1613 i += 2;
1614
1615 } else {
1616 pr_ctrls_add_response(ctrl, "unknown shaper session option '%s'",
1617 reqargv[i]);
1618 send_tab = FALSE;
1619 i += 2;
1620 continue;
1621 }
1622 }
1623
1624 if (send_tab == FALSE) {
1625 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1626 }
1627
1628 /* Sessions that are not shaped (i.e. excluded from mod_shaper) cannot be
1629 * adjusted. If exempted at login time, they cannot later be shaped.
1630 */
1631 /* XXX add ability to add non-shaped session to ShaperTable at
1632 * post-login time? And/or remove a session from ShaperTable before
1633 * session exit?
1634 */
1635
1636 if (strcmp(reqargv[0], "user") == 0) {
1637 pr_scoreboard_entry_t *score;
1638 const char *user = reqargv[1];
1639
1640 if (pr_rewind_scoreboard() < 0) {
1641 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1642 "error rewinding scoreboard: %s", strerror(errno));
1643 }
1644
1645 while ((score = pr_scoreboard_entry_read()) != NULL) {
1646 pr_signals_handle();
1647
1648 if (strcmp(score->sce_user, user) == 0) {
1649 if (shaper_table_sess_modify(score->sce_pid, prio, downincr,
1650 upincr) < 0) {
1651 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1652 "error adjusting pid %u: %s", (unsigned int) score->sce_pid,
1653 strerror(errno));
1654 pr_ctrls_add_response(ctrl, "error adjusting pid %u: %s",
1655 (unsigned int) score->sce_pid, strerror(errno));
1656
1657 } else {
1658 adjusted = TRUE;
1659 }
1660 }
1661 }
1662
1663 pr_restore_scoreboard();
1664
1665 } else if (strcmp(reqargv[0], "host") == 0) {
1666 pr_scoreboard_entry_t *score;
1667 const char *addr;
1668 const pr_netaddr_t *na;
1669
1670 na = pr_netaddr_get_addr(ctrl->ctrls_tmp_pool, reqargv[1], NULL);
1671 if (na == NULL) {
1672 pr_ctrls_add_response(ctrl, "error resolving '%s': %s", reqargv[1],
1673 strerror(errno));
1674 return PR_CTRLS_STATUS_SUBJECT_NOT_FOUND;
1675 }
1676
1677 addr = pr_netaddr_get_ipstr(na);
1678
1679 if (pr_rewind_scoreboard() < 0) {
1680 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1681 "error rewinding scoreboard: %s", strerror(errno));
1682 }
1683
1684 while ((score = pr_scoreboard_entry_read()) != NULL) {
1685 pr_signals_handle();
1686
1687 if (strcmp(score->sce_client_addr, addr) == 0) {
1688 if (shaper_table_sess_modify(score->sce_pid, prio, downincr,
1689 upincr) < 0) {
1690 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1691 "error adjusting pid %u: %s", (unsigned int) score->sce_pid,
1692 strerror(errno));
1693 pr_ctrls_add_response(ctrl, "error adjusting pid %u: %s",
1694 (unsigned int) score->sce_pid, strerror(errno));
1695
1696 } else {
1697 adjusted = TRUE;
1698 }
1699 }
1700 }
1701
1702 pr_restore_scoreboard();
1703
1704 } else if (strcmp(reqargv[0], "class") == 0) {
1705 pr_scoreboard_entry_t *score;
1706 const char *class = reqargv[1];
1707
1708 if (pr_rewind_scoreboard() < 0) {
1709 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1710 "error rewinding scoreboard: %s", strerror(errno));
1711 }
1712
1713 while ((score = pr_scoreboard_entry_read()) != NULL) {
1714 pr_signals_handle();
1715
1716 if (strcmp(score->sce_class, class) == 0) {
1717 if (shaper_table_sess_modify(score->sce_pid, prio, downincr,
1718 upincr) < 0) {
1719 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
1720 "error adjusting pid %u: %s", (unsigned int) score->sce_pid,
1721 strerror(errno));
1722 pr_ctrls_add_response(ctrl, "error adjusting pid %u: %s",
1723 (unsigned int) score->sce_pid, strerror(errno));
1724
1725 } else {
1726 adjusted = TRUE;
1727 }
1728 }
1729 }
1730
1731 pr_restore_scoreboard();
1732
1733 } else {
1734 pr_ctrls_add_response(ctrl, "unknown shaper session target type: '%s'",
1735 reqargv[0]);
1736 return PR_CTRLS_STATUS_UNSUPPORTED_OPERATION;
1737 }
1738
1739 if (adjusted == TRUE) {
1740 pr_ctrls_add_response(ctrl, "sessions adjusted");
1741 }
1742
1743 return PR_CTRLS_STATUS_OK;
1744 }
1745
1746 static int shaper_handle_shaper(pr_ctrls_t *ctrl, int reqargc,
1747 char **reqargv) {
1748
1749 /* Sanity check */
1750 if (reqargc == 0 ||
1751 reqargv == NULL) {
1752 pr_ctrls_add_response(ctrl, "missing required parameters");
1753 return PR_CTRLS_STATUS_WRONG_PARAMETERS;
1754 }
1755
1756 if (strcmp(reqargv[0], "all") == 0) {
1757
1758 /* Check the all ACL */
1759 if (pr_ctrls_check_acl(ctrl, shaper_acttab, "all") != TRUE) {
1760
1761 /* Access denied */
1762 pr_ctrls_add_response(ctrl, "access denied");
1763 return PR_CTRLS_STATUS_ACCESS_DENIED;
1764 }
1765
1766 return shaper_handle_all(ctrl, --reqargc, ++reqargv);
1767
1768 } else if (strcmp(reqargv[0], "info") == 0) {
1769
1770 /* Check the info ACL */
1771 if (pr_ctrls_check_acl(ctrl, shaper_acttab, "info") != TRUE) {
1772
1773 /* Access denied */
1774 pr_ctrls_add_response(ctrl, "access denied");
1775 return PR_CTRLS_STATUS_ACCESS_DENIED;
1776 }
1777
1778 return shaper_handle_info(ctrl, --reqargc, ++reqargv);
1779
1780 } else if (strcmp(reqargv[0], "sess") == 0) {
1781
1782 /* Check the sess ACL */
1783 if (pr_ctrls_check_acl(ctrl, shaper_acttab, "sess") != TRUE) {
1784
1785 /* Access denied */
1786 pr_ctrls_add_response(ctrl, "access denied");
1787 return PR_CTRLS_STATUS_ACCESS_DENIED;
1788 }
1789
1790 return shaper_handle_sess(ctrl, --reqargc, ++reqargv);
1791 }
1792
1793 pr_ctrls_add_response(ctrl, "unknown shaper action: '%s'", reqargv[0]);
1794 return PR_CTRLS_STATUS_UNSUPPORTED_OPERATION;
1795 }
1796
1797 /* Configuration handlers
1798 */
1799
1800 /* usage: ShaperAll [priority prio] [rate rate] [downrate rate] [uprate rate]
1801 * [shares nshares] [downshares nshares] [upshares nshares]
1802 */
1803 MODRET set_shaperall(cmd_rec *cmd) {
1804 register unsigned int i;
1805
1806 if (cmd->argc-1 < 2 || cmd->argc-1 > 14 || (cmd->argc-1) % 2 != 0)
1807 CONF_ERROR(cmd, "wrong number of parameters");
1808
1809 CHECK_CONF(cmd, CONF_ROOT);
1810
1811 for (i = 1; i < cmd->argc;) {
1812 if (strcmp(cmd->argv[i], "downrate") == 0) {
1813 char *tmp;
1814 long double rate;
1815
1816 rate = strtod(cmd->argv[i+1], &tmp);
1817
1818 if (tmp && *tmp)
1819 CONF_ERROR(cmd, "invalid downrate parameter");
1820
1821 if (rate < 0.0)
1822 CONF_ERROR(cmd, "downrate must be greater than 0");
1823
1824 shaper_tab.downrate = rate;
1825 i += 2;
1826
1827 } else if (strcmp(cmd->argv[i], "downshares") == 0) {
1828 int shares = atoi(cmd->argv[i+1]);
1829
1830 if (shares < 1)
1831 CONF_ERROR(cmd, "downshares must be greater than 1");
1832
1833 shaper_tab.def_downshares = shares;
1834 i += 2;
1835
1836 } else if (strcmp(cmd->argv[i], "priority") == 0) {
1837 int prio = atoi(cmd->argv[i+1]);
1838
1839 if (prio < 0)
1840 CONF_ERROR(cmd, "priority must be greater than 0");
1841
1842 shaper_tab.def_prio = prio;
1843 i += 2;
1844
1845 } else if (strcmp(cmd->argv[i], "rate") == 0) {
1846 char *tmp;
1847 long double rate;
1848
1849 rate = strtod(cmd->argv[i+1], &tmp);
1850
1851 if (tmp && *tmp)
1852 CONF_ERROR(cmd, "invalid rate parameter");
1853
1854 if (rate < 0.0)
1855 CONF_ERROR(cmd, "rate must be greater than 0");
1856
1857 shaper_tab.downrate = rate;
1858 shaper_tab.uprate = rate;
1859 i += 2;
1860
1861 } else if (strcmp(cmd->argv[i], "shares") == 0) {
1862 int shares = atoi(cmd->argv[i+1]);
1863
1864 if (shares < 1)
1865 CONF_ERROR(cmd, "shares must be greater than 1");
1866
1867 shaper_tab.def_downshares = shares;
1868 shaper_tab.def_upshares = shares;
1869 i += 2;
1870
1871 } else if (strcmp(cmd->argv[i], "uprate") == 0) {
1872 char *tmp;
1873 long double rate;
1874
1875 rate = strtod(cmd->argv[i+1], &tmp);
1876
1877 if (tmp && *tmp)
1878 CONF_ERROR(cmd, "invalid uprate parameter");
1879
1880 if (rate < 0.0)
1881 CONF_ERROR(cmd, "uprate must be greater than 0");
1882
1883 shaper_tab.uprate = rate;
1884 i += 2;
1885
1886 } else if (strcmp(cmd->argv[i], "upshares") == 0) {
1887 int shares = atoi(cmd->argv[i+1]);
1888
1889 if (shares < 1)
1890 CONF_ERROR(cmd, "upshares must be greater than 1");
1891
1892 shaper_tab.def_upshares = shares;
1893 i += 2;
1894
1895 } else
1896 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown option: '",
1897 cmd->argv[i], "'", NULL));
1898 }
1899
1900 return PR_HANDLED(cmd);
1901 }
1902
1903 /* usage: ShaperControlsACLs actions|all allow|deny user|group list */
1904 MODRET set_shaperctrlsacls(cmd_rec *cmd) {
1905 char *bad_action = NULL, **actions = NULL;
1906
1907 CHECK_ARGS(cmd, 4);
1908 CHECK_CONF(cmd, CONF_ROOT);
1909
1910 actions = pr_ctrls_parse_acl(cmd->tmp_pool, cmd->argv[1]);
1911
1912 /* Check the second parameter to make sure it is "allow" or "deny" */
1913 if (strcmp(cmd->argv[2], "allow") != 0 &&
1914 strcmp(cmd->argv[2], "deny") != 0) {
1915 CONF_ERROR(cmd, "second parameter must be 'allow' or 'deny'");
1916 }
1917
1918 /* Check the third parameter to make sure it is "user" or "group" */
1919 if (strcmp(cmd->argv[3], "user") != 0 &&
1920 strcmp(cmd->argv[3], "group") != 0) {
1921 CONF_ERROR(cmd, "third parameter must be 'user' or 'group'");
1922 }
1923
1924 bad_action = pr_ctrls_set_module_acls(shaper_acttab, shaper_pool, actions,
1925 cmd->argv[2], cmd->argv[3], cmd->argv[4]);
1926 if (bad_action != NULL) {
1927 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": unknown shaper action: '",
1928 bad_action, "'", NULL));
1929 }
1930
1931 return PR_HANDLED(cmd);
1932 }
1933
1934 /* usage: ShaperEngine on|off */
1935 MODRET set_shaperengine(cmd_rec *cmd) {
1936 int engine;
1937 config_rec *c;
1938
1939 CHECK_ARGS(cmd, 1);
1940 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
1941
1942 engine = get_boolean(cmd, 1);
1943 if (engine == -1) {
1944 CONF_ERROR(cmd, "expected Boolean parameter");
1945 }
1946
1947 c = add_config_param(cmd->argv[0], 1, NULL);
1948 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
1949 *((unsigned int *) c->argv[0]) = engine;
1950 c->flags |= CF_MERGEDOWN;
1951
1952 return PR_HANDLED(cmd);
1953 }
1954
1955 /* usage: ShaperLog path|"none" */
1956 MODRET set_shaperlog(cmd_rec *cmd) {
1957 CHECK_ARGS(cmd, 1);
1958 CHECK_CONF(cmd, CONF_ROOT);
1959
1960 if (strcasecmp(cmd->argv[0], "none") != 0 &&
1961 pr_fs_valid_path(cmd->argv[1]) < 0) {
1962 CONF_ERROR(cmd, "must be an absolute path");
1963 }
1964
1965 shaper_log_path = pstrdup(shaper_pool, cmd->argv[1]);
1966 return PR_HANDLED(cmd);
1967 }
1968
1969 /* usage: ShaperSession [priority prio] [shares num] [downshares num]
1970 * [upshares num]
1971 */
1972 MODRET set_shapersession(cmd_rec *cmd) {
1973 int prio = -1;
1974 int downshares = 0, upshares = 0;
1975 config_rec *c;
1976
1977 register unsigned int i;
1978
1979 if (cmd->argc-1 < 2 ||
1980 cmd->argc-1 > 8 ||
1981 (cmd->argc-1) % 2 != 0) {
1982 CONF_ERROR(cmd, "wrong number of parameters");
1983 }
1984
1985 CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
1986
1987 for (i = 1; i < cmd->argc;) {
1988 if (strcmp(cmd->argv[i], "downshares") == 0) {
1989 char *shareno;
1990
1991 shareno = cmd->argv[i+1];
1992 if (*shareno != '+' &&
1993 *shareno != '-') {
1994 CONF_ERROR(cmd, "downshares parameter must start with '+' or '-'");
1995 }
1996
1997 downshares = atoi(shareno);
1998 i += 2;
1999
2000 } else if (strcmp(cmd->argv[i], "priority") == 0) {
2001 prio = atoi(cmd->argv[i+1]);
2002 if (prio < 0) {
2003 CONF_ERROR(cmd, "priority must be greater than 0");
2004 }
2005
2006 i += 2;
2007
2008 } else if (strcmp(cmd->argv[i], "shares") == 0) {
2009 char *shareno;
2010
2011 shareno = cmd->argv[i+1];
2012 if (*shareno != '+' &&
2013 *shareno != '-') {
2014 CONF_ERROR(cmd, "shares parameter must start with '+' or '-'");
2015 }
2016
2017 downshares = upshares = atoi(shareno);
2018 i += 2;
2019
2020 } else if (strcmp(cmd->argv[i], "upshares") == 0) {
2021 char *shareno;
2022
2023 shareno = cmd->argv[i+1];
2024 if (*shareno != '+' &&
2025 *shareno != '-') {
2026 CONF_ERROR(cmd, "upshares parameter must start with '+' or '-'");
2027 }
2028
2029 upshares = atoi(shareno);
2030 i += 2;
2031
2032 } else {
2033 CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown option: '",
2034 (char *) cmd->argv[i], "'", NULL));
2035 }
2036 }
2037
2038 c = add_config_param(cmd->argv[0], 3, NULL, NULL);
2039 c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
2040 *((unsigned int *) c->argv[0]) = (unsigned int) prio;
2041 c->argv[1] = pcalloc(c->pool, sizeof(int));
2042 *((int *) c->argv[1]) = downshares;
2043 c->argv[2] = pcalloc(c->pool, sizeof(int));
2044 *((int *) c->argv[2]) = upshares;
2045 c->flags |= CF_MERGEDOWN;
2046
2047 return PR_HANDLED(cmd);
2048 }
2049
2050 /* usage: ShaperTable path */
2051 MODRET set_shapertable(cmd_rec *cmd) {
2052 CHECK_ARGS(cmd, 1);
2053 CHECK_CONF(cmd, CONF_ROOT);
2054
2055 if (pr_fs_valid_path(cmd->argv[1]) < 0) {
2056 CONF_ERROR(cmd, "must be an absolute path");
2057 }
2058
2059 shaper_tab_path = pstrdup(shaper_pool, cmd->argv[1]);
2060 return PR_HANDLED(cmd);
2061 }
2062
2063 /* Command handlers
2064 */
2065
2066 MODRET shaper_pre_pass(cmd_rec *cmd) {
2067
2068 /* Make sure this session process has the ShaperTable open.
2069 *
2070 * NOTE: I'm not sure this is needed anymore, since an fd to the
2071 * ShaperTable is opened during the 'core.postparse' event handler,
2072 * in the daemon process.
2073 */
2074
2075 PRIVS_ROOT
2076 shaper_tabfd = open(shaper_tab_path, O_RDWR);
2077 PRIVS_RELINQUISH
2078
2079 if (shaper_tabfd < 0)
2080 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2081 "unable to open ShaperTable: %s", strerror(errno));
2082
2083 return PR_DECLINED(cmd);
2084 }
2085
2086 MODRET shaper_post_pass(cmd_rec *cmd) {
2087 config_rec *c;
2088 int downincr = 0, upincr = 0;
2089 unsigned int prio = -1;
2090
2091 c = find_config(TOPLEVEL_CONF, CONF_PARAM, "ShaperEngine", FALSE);
2092 if (c != NULL &&
2093 *((unsigned char *) c->argv[0]) == TRUE) {
2094 shaper_engine = TRUE;
2095
2096 } else {
2097 /* Don't need the ShaperTable open anymore. */
2098 close(shaper_tabfd);
2099 shaper_tabfd = -1;
2100
2101 return PR_DECLINED(cmd);
2102 }
2103
2104 if (!shaper_tab_path) {
2105 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2106 "ShaperTable not configured, disabling ShaperEngine");
2107 shaper_engine = FALSE;
2108 return PR_DECLINED(cmd);
2109 }
2110
2111 if (shaper_tabfd < 0) {
2112 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2113 "ShaperTable not open, disabling ShaperEngine");
2114 shaper_engine = FALSE;
2115 return PR_DECLINED(cmd);
2116 }
2117
2118 if (shaper_tab.downrate < 0.0 || shaper_tab.uprate < 0.0) {
2119 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2120 "overall rates negative or not configured, disabling ShaperEngine");
2121 shaper_engine = FALSE;
2122 return PR_DECLINED(cmd);
2123 }
2124
2125 pr_event_register(&shaper_module, "core.exit", shaper_sess_exit_ev, NULL);
2126 pr_event_register(&shaper_module, "core.signal.USR2", shaper_sigusr2_ev,
2127 NULL);
2128
2129 c = find_config(TOPLEVEL_CONF, CONF_PARAM, "ShaperSession", FALSE);
2130 if (c) {
2131 prio = *((unsigned int *) c->argv[0]);
2132 downincr = *((int *) c->argv[1]);
2133 upincr = *((int *) c->argv[2]);
2134 }
2135
2136 /* Update the ShaperTable, adding a new entry for the current session. */
2137 if (shaper_table_sess_add(getpid(), prio, downincr, upincr) < 0) {
2138 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2139 "error adding session to ShaperTable: %s", strerror(errno));
2140 }
2141
2142 return PR_DECLINED(cmd);
2143 }
2144
2145 MODRET shaper_post_err_pass(cmd_rec *cmd) {
2146
2147 /* Close the ShaperTable if we failed to authenticate. */
2148 close(shaper_tabfd);
2149 shaper_tabfd = -1;
2150
2151 return PR_DECLINED(cmd);
2152 }
2153
2154 /* Event handlers
2155 */
2156
2157 static void shaper_shutdown_ev(const void *event_data, void *user_data) {
2158
2159 /* Remove the queue from the system, and delete the ShaperTable. We can
2160 * only do this reliably when the standalone daemon process exits; if it's
2161 * an inetd process, there may be other proftpd processes still running.
2162 */
2163 if (getpid() == mpid &&
2164 ServerType == SERVER_STANDALONE) {
2165
2166 if (shaper_qid >= 0) {
2167 shaper_remove_queue();
2168 }
2169
2170 if (shaper_tab_path) {
2171 if (pr_fsio_unlink(shaper_tab_path) < 0) {
2172 pr_log_debug(DEBUG9, MOD_SHAPER_VERSION
2173 ": error unlinking '%s': %s", shaper_tab_path, strerror(errno));
2174 }
2175 }
2176 }
2177
2178 return;
2179 }
2180
2181 static void shaper_sess_exit_ev(const void *event_data, void *user_data) {
2182
2183 /* Remove this session from the ShaperTable. */
2184 if (shaper_table_sess_remove(getpid()) < 0) {
2185 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2186 "error removing session from ShaperTable: %s", strerror(errno));
2187 }
2188
2189 /* Clear any messages for this session from the queue as well. */
2190 shaper_msg_clear(getpid());
2191
2192 return;
2193 }
2194
2195 #if defined(PR_SHARED_MODULE)
2196 static void shaper_mod_unload_ev(const void *event_data, void *user_data) {
2197 if (strcmp("mod_shaper.c", (const char *) event_data) == 0) {
2198 /* Unregister ourselves from all events. */
2199 pr_event_unregister(&shaper_module, NULL, NULL);
2200
2201 /* Unregister all control actions. */
2202 (void) pr_ctrls_unregister(&shaper_module, "shaper");
2203
2204 if (shaper_scrub_timer_id != -1) {
2205 (void) pr_timer_remove(shaper_scrub_timer_id, &shaper_module);
2206 shaper_scrub_timer_id = -1;
2207 }
2208
2209 if (shaper_pool) {
2210 destroy_pool(shaper_pool);
2211 shaper_pool = NULL;
2212 shaper_tab_pool = NULL;
2213 shaper_tab.sess_list = NULL;
2214 }
2215 }
2216 }
2217 #endif /* PR_SHARED_MODULE */
2218
2219 static void shaper_postparse_ev(const void *event_data, void *user_data) {
2220 if (shaper_log_path &&
2221 strcasecmp(shaper_log_path, "none") != 0 &&
2222 pr_log_openfile(shaper_log_path, &shaper_logfd, 0660) < 0) {
2223 pr_log_debug(DEBUG2, MOD_SHAPER_VERSION
2224 ": error opening ShaperLog '%s': %s", shaper_log_path, strerror(errno));
2225 shaper_logfd = -1;
2226 }
2227
2228 if (shaper_tab_path) {
2229 pr_fh_t *fh;
2230 int xerrno;
2231 struct stat st;
2232
2233 PRIVS_ROOT
2234 fh = pr_fsio_open(shaper_tab_path, O_RDWR|O_CREAT);
2235 xerrno = errno;
2236 PRIVS_RELINQUISH
2237
2238 if (fh == NULL) {
2239 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2240 "error opening ShaperTable '%s': %s", shaper_tab_path,
2241 strerror(xerrno));
2242 pr_log_debug(DEBUG0, MOD_SHAPER_VERSION
2243 ": error opening ShaperTable '%s': %s", shaper_tab_path,
2244 strerror(xerrno));
2245 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2246 NULL);
2247 }
2248
2249 if (pr_fsio_fstat(fh, &st) < 0) {
2250 xerrno = errno;
2251
2252 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2253 "error checking ShaperTable '%s': %s", shaper_tab_path,
2254 strerror(xerrno));
2255 pr_log_debug(DEBUG0, MOD_SHAPER_VERSION
2256 ": error checking ShaperTable '%s': %s", shaper_tab_path,
2257 strerror(xerrno));
2258
2259 pr_fsio_close(fh);
2260 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2261 NULL);
2262 }
2263
2264 if (S_ISDIR(st.st_mode)) {
2265 xerrno = EISDIR;
2266
2267 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2268 "error using ShaperTable '%s': %s", shaper_tab_path,
2269 strerror(xerrno));
2270 pr_log_debug(DEBUG0, MOD_SHAPER_VERSION
2271 ": error using ShaperTable '%s': %s", shaper_tab_path,
2272 strerror(xerrno));
2273
2274 pr_fsio_close(fh);
2275 pr_session_disconnect(&shaper_module, PR_SESS_DISCONNECT_BAD_CONFIG,
2276 NULL);
2277 }
2278
2279 /* Initialize ShaperTable */
2280 if (shaper_table_init(fh) < 0)
2281 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2282 "error initializing ShaperTable: %s", strerror(errno));
2283
2284 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2285 "determining queue ID for path '%s'", shaper_tab_path);
2286
2287 shaper_qid = shaper_get_queue(shaper_tab_path);
2288 if (shaper_qid < 0) {
2289 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2290 "error obtaining queue ID: %s", strerror(errno));
2291
2292 } else {
2293 struct msqid_ds ds;
2294
2295 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2296 "obtained queue ID %d", shaper_qid);
2297
2298 if (msgctl(shaper_qid, IPC_STAT, &ds) < 0) {
2299 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2300 "error checking queue ID %d: %s", shaper_qid, strerror(errno));
2301
2302 } else {
2303 shaper_qmaxbytes = ds.msg_qbytes;
2304 }
2305
2306 /* We could be being restarted, in which case we want to send our
2307 * reinitialized table to existing sessions.
2308 */
2309
2310 if (shaper_table_lock(LOCK_EX) < 0) {
2311 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2312 "error locking ShaperTable: %s", strerror(errno));
2313 return;
2314 }
2315
2316 if (shaper_table_refresh() < 0) {
2317 shaper_table_lock(LOCK_UN);
2318 return;
2319 }
2320
2321 if (shaper_table_send() < 0) {
2322 shaper_table_lock(LOCK_UN);
2323 return;
2324 }
2325
2326 if (shaper_table_flush() < 0) {
2327 shaper_table_lock(LOCK_UN);
2328 return;
2329 }
2330
2331 shaper_table_lock(LOCK_UN);
2332 }
2333
2334 if (shaper_scrub_timer_id == -1) {
2335 shaper_scrub_timer_id = pr_timer_add(SHAPER_SCRUB_INTERVAL, -1,
2336 &shaper_module, shaper_table_scrub_cb, "shaper table scrubber");
2337 }
2338
2339 } else {
2340 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2341 "no ShaperTable configured");
2342 }
2343
2344 }
2345
2346 static void shaper_restart_ev(const void *event_data, void *user_data) {
2347 register unsigned int i;
2348
2349 (void) close(shaper_logfd);
2350 shaper_logfd = -1;
2351 shaper_log_path = NULL;
2352
2353 if (shaper_pool) {
2354 destroy_pool(shaper_pool);
2355
2356 /* Make sure to mark subpools as invalid now as well. */
2357 shaper_tab_pool = NULL;
2358 shaper_tab.sess_list = NULL;
2359 }
2360
2361 shaper_pool = make_sub_pool(permanent_pool);
2362 pr_pool_tag(shaper_pool, MOD_SHAPER_VERSION);
2363
2364 for (i = 0; shaper_acttab[i].act_action; i++) {
2365 shaper_acttab[i].act_acl = pcalloc(shaper_pool, sizeof(ctrls_acl_t));
2366 pr_ctrls_init_acl(shaper_acttab[i].act_acl);
2367 }
2368
2369 if (shaper_scrub_timer_id != -1) {
2370 (void) pr_timer_remove(shaper_scrub_timer_id, &shaper_module);
2371 shaper_scrub_timer_id = -1;
2372 }
2373
2374 return;
2375 }
2376
2377 static void shaper_sigusr2_ev(const void *event_data, void *user_data) {
2378 int res;
2379
2380 /* Check the queue for any messages for us. */
2381 res = shaper_msg_recv();
2382
2383 switch (res) {
2384 case -1:
2385 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2386 "error receiving updates for pid %lu: %s", (unsigned long) getpid(),
2387 strerror(errno));
2388 break;
2389
2390 case 0:
2391 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2392 "received signal, no updates for pid %lu", (unsigned long) getpid());
2393 break;
2394
2395 default:
2396 (void) pr_log_writefile(shaper_logfd, MOD_SHAPER_VERSION,
2397 "received signal, read in %d %s for pid %lu", res,
2398 res == 1 ? "update" : "updates", (unsigned long) getpid());
2399 }
2400
2401 return;
2402 }
2403
2404 /* Initialization functions
2405 */
2406
2407 static int shaper_init(void) {
2408
2409 shaper_pool = make_sub_pool(permanent_pool);
2410 pr_pool_tag(shaper_pool, MOD_SHAPER_VERSION);
2411
2412 shaper_tab.def_prio = SHAPER_DEFAULT_PRIO;
2413 shaper_tab.downrate = SHAPER_DEFAULT_RATE;
2414 shaper_tab.def_downshares = SHAPER_DEFAULT_DOWNSHARES;
2415 shaper_tab.uprate = SHAPER_DEFAULT_RATE;
2416 shaper_tab.def_upshares = SHAPER_DEFAULT_UPSHARES;
2417 shaper_tab.nsessions = 0;
2418
2419 if (pr_ctrls_register(&shaper_module, "shaper", "tune mod_shaper settings",
2420 shaper_handle_shaper) < 0) {
2421 pr_log_pri(PR_LOG_NOTICE, MOD_SHAPER_VERSION
2422 ": error registering 'shaper' control: %s", strerror(errno));
2423
2424 } else {
2425 register unsigned int i;
2426
2427 for (i = 0; shaper_acttab[i].act_action; i++) {
2428 shaper_acttab[i].act_acl = pcalloc(shaper_pool, sizeof(ctrls_acl_t));
2429 pr_ctrls_init_acl(shaper_acttab[i].act_acl);
2430 }
2431 }
2432
2433 #if defined(PR_SHARED_MODULE)
2434 pr_event_register(&shaper_module, "core.module-unload", shaper_mod_unload_ev,
2435 NULL);
2436 #endif /* PR_SHARED_MODULE */
2437 pr_event_register(&shaper_module, "core.postparse", shaper_postparse_ev,
2438 NULL);
2439 pr_event_register(&shaper_module, "core.restart", shaper_restart_ev, NULL);
2440 pr_event_register(&shaper_module, "core.shutdown", shaper_shutdown_ev, NULL);
2441
2442 return 0;
2443 }
2444
2445 static int shaper_sess_init(void) {
2446
2447 /* The ShaperTable scrubbing timer should only run in the daemon. */
2448 pr_timer_remove(shaper_scrub_timer_id, &shaper_module);
2449
2450 return 0;
2451 }
2452
2453 /* Module API tables
2454 */
2455
2456 static ctrls_acttab_t shaper_acttab[] = {
2457 { "all", NULL, NULL, NULL },
2458 { "info", NULL, NULL, NULL },
2459 { "sess", NULL, NULL, NULL },
2460 { NULL, NULL, NULL, NULL }
2461 };
2462
2463 static conftable shaper_conftab[] = {
2464 { "ShaperAll", set_shaperall, NULL },
2465 { "ShaperControlsACLs", set_shaperctrlsacls, NULL },
2466 { "ShaperEngine", set_shaperengine, NULL },
2467 { "ShaperLog", set_shaperlog, NULL },
2468 { "ShaperSession", set_shapersession, NULL },
2469 { "ShaperTable", set_shapertable, NULL },
2470 { NULL }
2471 };
2472
2473 static cmdtable shaper_cmdtab[] = {
2474 { PRE_CMD, C_PASS, G_NONE, shaper_pre_pass, FALSE, FALSE },
2475 { POST_CMD, C_PASS, G_NONE, shaper_post_pass, FALSE, FALSE },
2476 { POST_CMD_ERR, C_PASS, G_NONE, shaper_post_err_pass, FALSE, FALSE },
2477 { 0, NULL }
2478 };
2479
2480 module shaper_module = {
2481 NULL, NULL,
2482
2483 /* Module API version 2.0 */
2484 0x20,
2485
2486 /* Module name */
2487 "shaper",
2488
2489 /* Module configuration handler table */
2490 shaper_conftab,
2491
2492 /* Module command handler table */
2493 shaper_cmdtab,
2494
2495 /* Module authentication handler table */
2496 NULL,
2497
2498 /* Module initialization function */
2499 shaper_init,
2500
2501 /* Session initialization function */
2502 shaper_sess_init,
2503
2504 /* Module version */
2505 MOD_SHAPER_VERSION
2506 };