svn commit: r284783 - head/sys/kern
Mateusz Guzik
mjg at FreeBSD.org
Thu Jun 25 00:15:38 UTC 2015
Author: mjg
Date: Thu Jun 25 00:15:37 2015
New Revision: 284783
URL: https://svnweb.freebsd.org/changeset/base/284783
Log:
rlimit: deduplicate code in chg* functions
Modified:
head/sys/kern/kern_resource.c
Modified: head/sys/kern/kern_resource.c
==============================================================================
--- head/sys/kern/kern_resource.c Wed Jun 24 23:05:17 2015 (r284782)
+++ head/sys/kern/kern_resource.c Thu Jun 25 00:15:37 2015 (r284783)
@@ -1371,49 +1371,52 @@ ui_racct_foreach(void (*callback)(struct
}
#endif
-/*
- * Change the count associated with number of processes
- * a given user is using. When 'max' is 0, don't enforce a limit
- */
-int
-chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
+static inline int
+chglimit(struct uidinfo *uip, long *limit, int diff, rlim_t max, const char *name)
{
/* Don't allow them to exceed max, but allow subtraction. */
if (diff > 0 && max != 0) {
- if (atomic_fetchadd_long(&uip->ui_proccnt, (long)diff) + diff > max) {
- atomic_subtract_long(&uip->ui_proccnt, (long)diff);
+ if (atomic_fetchadd_long(limit, (long)diff) + diff > max) {
+ atomic_subtract_long(limit, (long)diff);
return (0);
}
} else {
- atomic_add_long(&uip->ui_proccnt, (long)diff);
- if (uip->ui_proccnt < 0)
- printf("negative proccnt for uid = %d\n", uip->ui_uid);
+ atomic_add_long(limit, (long)diff);
+ if (*limit < 0)
+ printf("negative %s for uid = %d\n", name, uip->ui_uid);
}
return (1);
}
/*
+ * Change the count associated with number of processes
+ * a given user is using. When 'max' is 0, don't enforce a limit
+ */
+int
+chgproccnt(struct uidinfo *uip, int diff, rlim_t max)
+{
+
+ return (chglimit(uip, &uip->ui_proccnt, diff, max, "proccnt"));
+}
+
+/*
* Change the total socket buffer size a user has used.
*/
int
chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to, rlim_t max)
{
- int diff;
+ int diff, rv;
diff = to - *hiwat;
- if (diff > 0) {
- if (atomic_fetchadd_long(&uip->ui_sbsize, (long)diff) + diff > max) {
- atomic_subtract_long(&uip->ui_sbsize, (long)diff);
- return (0);
- }
+ if (diff > 0 && max == 0) {
+ rv = 0;
} else {
- atomic_add_long(&uip->ui_sbsize, (long)diff);
- if (uip->ui_sbsize < 0)
- printf("negative sbsize for uid = %d\n", uip->ui_uid);
+ rv = chglimit(uip, &uip->ui_sbsize, diff, max, "sbsize");
+ if (rv != 0)
+ *hiwat = to;
}
- *hiwat = to;
- return (1);
+ return (rv);
}
/*
@@ -1424,36 +1427,14 @@ int
chgptscnt(struct uidinfo *uip, int diff, rlim_t max)
{
- /* Don't allow them to exceed max, but allow subtraction. */
- if (diff > 0 && max != 0) {
- if (atomic_fetchadd_long(&uip->ui_ptscnt, (long)diff) + diff > max) {
- atomic_subtract_long(&uip->ui_ptscnt, (long)diff);
- return (0);
- }
- } else {
- atomic_add_long(&uip->ui_ptscnt, (long)diff);
- if (uip->ui_ptscnt < 0)
- printf("negative ptscnt for uid = %d\n", uip->ui_uid);
- }
- return (1);
+ return (chglimit(uip, &uip->ui_ptscnt, diff, max, "ptscnt"));
}
int
chgkqcnt(struct uidinfo *uip, int diff, rlim_t max)
{
- if (diff > 0 && max != 0) {
- if (atomic_fetchadd_long(&uip->ui_kqcnt, (long)diff) +
- diff > max) {
- atomic_subtract_long(&uip->ui_kqcnt, (long)diff);
- return (0);
- }
- } else {
- atomic_add_long(&uip->ui_kqcnt, (long)diff);
- if (uip->ui_kqcnt < 0)
- printf("negative kqcnt for uid = %d\n", uip->ui_uid);
- }
- return (1);
+ return (chglimit(uip, &uip->ui_kqcnt, diff, max, "kqcnt"));
}
void
More information about the svn-src-all
mailing list