PERFORCE change 165452 for review

Edward Tomasz Napierala trasz at FreeBSD.org
Mon Jun 29 22:30:01 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=165452

Change 165452 by trasz at trasz_victim on 2009/06/29 22:28:58

	Get rid of passing structures in hrl(2) altogether.

Affected files ...

.. //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#22 edit
.. //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#17 edit
.. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#13 edit

Differences ...

==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_hrl.c#22 (text+ko) ====

@@ -215,6 +215,32 @@
 }
 
 static const char *
+hrl_subject_name(int subject)
+{
+	int i;
+
+	for (i = 0; subjectnames[i].d_name != NULL; i++) {
+		if (subjectnames[i].d_value == subject)
+			return (subjectnames[i].d_name);
+	}
+
+	panic("hrl_subject_name: unknown subject");
+}
+
+static const char *
+hrl_action_name(int action)
+{
+	int i;
+
+	for (i = 0; actionnames[i].d_name != NULL; i++) {
+		if (actionnames[i].d_value == action)
+			return (actionnames[i].d_name);
+	}
+
+	panic("hrl_action_name: unknown action");
+}
+
+static const char *
 hrl_resource_name(int resource)
 {
 	int i;
@@ -667,23 +693,43 @@
 	return (0);
 }
 
+static struct sbuf *
+hrl_rules_to_sbuf(struct hrl_rule *usage, int nrules)
+{
+	int i;
+	struct sbuf *sb;
+
+	sb = sbuf_new_auto();
+	for (i = 0; i < nrules; i++) {
+		sbuf_printf(sb, "%s:%d:%s:%s=%jd",
+		    hrl_subject_name(usage[i].hr_subject),
+		    (int)usage[i].hr_subject_id,
+		    hrl_resource_name(usage[i].hr_resource),
+		    hrl_action_name(usage[i].hr_action),
+		    usage[i].hr_amount);
+		if (usage[i].hr_per != usage[i].hr_subject)
+			sbuf_printf(sb, "/%s,",
+			    hrl_subject_name(usage[i].hr_per));
+		else
+			sbuf_printf(sb, ",");
+	}
+	return (sb);
+}
+
 static int
-hrl_get_rules(struct thread *td, char *inputstr, void *bufp, size_t buflen)
+hrl_get_rules(struct thread *td, char *inputstr, struct sbuf **outputsbuf)
 {
 	int error = 0, copied = 0;
 	struct hrl_rule *buf, filter;
 	struct hrl_node *node;
 
-	if (buflen > HRL_MAX_LIMITS * sizeof(struct hrl_rule))
-		return (EINVAL);
-
 	if (inputstr != NULL) {
 		error = hrl_rule_parse(&filter, inputstr);
 		if (error)
 			return (error);
 	}
 
-	buf = malloc(buflen, M_HRL, M_WAITOK);
+	buf = malloc(HRL_MAX_RULES * sizeof(struct hrl_rule), M_HRL, M_WAITOK);
 
 	/*
 	 * Copy the limits to the temporary buffer.  We cannot
@@ -695,9 +741,11 @@
 		 * XXX: Do not show everything to the client; just the
 		 *      nodes that affect him.
 		 */
-		/* +1 to make room for the terminating NULL entry. */
-		if ((copied + 1) * sizeof(*buf) >= buflen) {
-			error = EFBIG;
+		if (copied >= HRL_MAX_RULES) {
+			/*
+			 * XXX: Hey, what to do now?
+			 */
+			error = EDOOFUS;
 			break;
 		}
 
@@ -713,14 +761,7 @@
 	if (error)
 		goto out;
 
-	/* Add terminating NULL entry. */
-	bzero(buf + copied, sizeof(*buf));
-	copied++;
-
-	error = copyout(buf, bufp, sizeof(struct hrl_rule) * copied);
-	if (error)
-		goto out;
-
+	*outputsbuf = hrl_rules_to_sbuf(buf, copied);
 out:
 	free(buf, M_HRL);
 	
@@ -911,7 +952,7 @@
 
 	switch (uap->op) {
 	case HRL_OP_GET_RULES:
-		error = hrl_get_rules(td, inputstr, uap->outbufp, uap->outbuflen);
+		error = hrl_get_rules(td, inputstr, &outputsbuf);
 		break;
 	case HRL_OP_ADD_RULE:
 		error = hrl_add_rule(td, inputstr);
@@ -935,10 +976,6 @@
 		error = EINVAL;
 	}
 
-	/*
-	 * XXX: Finish moving copyouts into this place.
-	 */
-
 	if (outputsbuf != NULL) {
 		sbuf_finish(outputsbuf);
 		if (uap->outbuflen < sbuf_len(outputsbuf) + 1) {

==== //depot/projects/soc2009/trasz_limits/sys/sys/hrl.h#17 (text+ko) ====

@@ -36,6 +36,16 @@
  * Hierarchical Resource Limits.
  */
 
+#define	HRL_OP_GET_RULES		1
+#define	HRL_OP_ADD_RULE			6
+#define	HRL_OP_REMOVE_RULE		7
+#define	HRL_OP_GET_USAGE_PID		2
+#define	HRL_OP_GET_USAGE_UID		3
+#define	HRL_OP_GET_USAGE_GID		4
+#define	HRL_OP_GET_USAGE_JAILID		5
+
+#ifdef _KERNEL
+
 /*
  * 'hrl_rule' describes a single limit configured by the system
  * administrator or a temporary limit set using setrlimit(2).
@@ -93,17 +103,7 @@
 #define	HRL_ACTION_SIGXFSZ		0x0009
 #define	HRL_ACTION_MAX			HRL_ACTION_SIGXFSZ
 
-#define	HRL_MAX_LIMITS			1024
-
-#define	HRL_OP_GET_RULES		1
-#define	HRL_OP_ADD_RULE			6
-#define	HRL_OP_REMOVE_RULE		7
-#define	HRL_OP_GET_USAGE_PID		2
-#define	HRL_OP_GET_USAGE_UID		3
-#define	HRL_OP_GET_USAGE_GID		4
-#define	HRL_OP_GET_USAGE_JAILID		5
-
-#ifdef _KERNEL
+#define	HRL_MAX_RULES			1024
 
 /*
  * 'hrl_usage' defines resource consumption for a particular

==== //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#13 (text+ko) ====

@@ -40,6 +40,7 @@
 
 #define	BUFLEN_DEFAULT	1024
 
+#if 0
 static void
 print_subject(int subject, id_t id)
 {
@@ -75,115 +76,7 @@
 		break;
 	}
 }
-
-static void
-print_resource(int resource)
-{
-
-	switch (resource) {
-	case HRL_RESOURCE_CPUTIME:
-		printf("cputime");
-		break;
-	case HRL_RESOURCE_FILESIZE:
-		printf("filesize");
-		break;
-	case HRL_RESOURCE_DATASIZE:
-		printf("datasize");
-		break;
-	case HRL_RESOURCE_STACKSIZE:
-		printf("stacksize");
-		break;
-	case HRL_RESOURCE_COREDUMPSIZE:
-		printf("coredumpsize");
-		break;
-	case HRL_RESOURCE_MEMORYUSE:
-		printf("memoryuse");
-		break;
-	case HRL_RESOURCE_MEMORYLOCKED:
-		printf("memorylocked");
-		break;
-	case HRL_RESOURCE_MAXPROCESSES:
-		printf("maxprocesses");
-		break;
-	case HRL_RESOURCE_FILEDESCRIPTORS:
-		printf("filedescriptors");
-		break;
-	case HRL_RESOURCE_SBSIZE:
-		printf("sbsize");
-		break;
-	case HRL_RESOURCE_VMEMORYUSE:
-		printf("vmemoryuse");
-		break;
-	case HRL_RESOURCE_PTY:
-		printf("pty");
-		break;
-	default:
-		printf("undefined<0x%x>", resource);
-	}
-}
-
-static void
-print_action(int action)
-{
-
-	switch (action) {
-	case HRL_ACTION_DENY:
-		printf("deny");
-		break;
-	case HRL_ACTION_DELAY:
-		printf("delay");
-		break;
-	case HRL_ACTION_LOG:
-		printf("log");
-		break;
-	case HRL_ACTION_SIGHUP:
-		printf("sighup");
-		break;
-	case HRL_ACTION_SIGINT:
-		printf("sigint");
-		break;
-	case HRL_ACTION_SIGKILL:
-		printf("sigkill");
-		break;
-	case HRL_ACTION_SIGSEGV:
-		printf("sigsegv");
-		break;
-	case HRL_ACTION_SIGXCPU:
-		printf("sigxcpu");
-		break;
-	case HRL_ACTION_SIGXFSZ:
-		printf("sigxfsz");
-		break;
-	default:
-		printf("undefined<0x%x>", action);
-	}
-}
-
-static void
-print_per(int per)
-{
-
-	switch (per) {
-	case HRL_SUBJECT_PROCESS:
-		printf("process");
-		break;
-	case HRL_SUBJECT_USER:
-		printf("user");
-		break;
-	case HRL_SUBJECT_GROUP:
-		printf("group");
-		break;
-	case HRL_SUBJECT_LOGINCLASS:
-		printf("class");
-		break;
-	case HRL_SUBJECT_JAIL:
-		printf("jail");
-		break;
-	default:
-		printf("undefined<0x%x>", per);
-		break;
-	}
-}
+#endif
 
 static id_t
 parse_pid(const char *s)
@@ -240,58 +133,49 @@
 	return (id);
 }
 
+/*
+ * Query the kernel about resource limit rules and print them out.
+ */
 static void
 print_rules(char *filter)
 {
 	int error;
-	size_t ruleslen, i;
-	struct hrl_rule *rules = NULL;
+	char *outbuf = NULL, *tmp;
+	size_t outbuflen = BUFLEN_DEFAULT / 4;
 
-	ruleslen = BUFLEN_DEFAULT / 4;
 	do {
-		ruleslen *= 4;
-		rules = realloc(rules, ruleslen);
-		if (rules == NULL)
+		outbuflen *= 4;
+		outbuf = realloc(outbuf, outbuflen);
+		if (outbuf == NULL)
 			err(1, "realloc");
 
 		if (filter != NULL)
-			error = hrl(HRL_OP_GET_RULES, filter, strlen(filter) + 1, rules, ruleslen);
+			error = hrl(HRL_OP_GET_RULES, filter, strlen(filter) + 1, outbuf, outbuflen);
 		else
-			error = hrl(HRL_OP_GET_RULES, NULL, 0, rules, ruleslen);
+			error = hrl(HRL_OP_GET_RULES, NULL, 0, outbuf, outbuflen);
 		if (error && errno != EFBIG)
 			err(1, "hrl");
 	} while (error && errno == EFBIG);
 
+	for (tmp = outbuf; *tmp != '\0'; tmp++)
+		if (*tmp == ',')
+			*tmp = '\n';
+
 	if (filter != NULL)
-		printf("Defined resource limits matching \"%s\":\n", filter);
+		printf("Defined resource limits matching \"%s\":\n%s\n", filter, outbuf);
 	else
-		printf("Defined resource limits:\n");
+		printf("Defined resource limits:\n%s\n", outbuf);
 
-	if (rules[0].hr_subject == 0) {
-		printf("No resource limits defined.\n");
+	/*
+	 * XXX: Resolve numeric ID-s back to names.
+	 */
 
-		return;
-	}
-
-	for (i = 0; i < ruleslen / sizeof(*rules); i++) {
-		/* NULL entry terminating the table? */
-		if (rules[i].hr_subject == 0)
-			break;
-		print_subject(rules[i].hr_subject, rules[i].hr_subject_id);
-		printf(":");
-		print_resource(rules[i].hr_resource);
-		printf(":");
-		print_action(rules[i].hr_action);
-		printf("=");
-		printf("%jd", rules[i].hr_amount);
-		if (rules[i].hr_subject != rules[i].hr_per) {
-			printf("/");
-			print_per(rules[i].hr_per);
-		}
-		printf("\n");
-	}
+	free(outbuf);
 }
 
+/*
+ * Query the kernel about a resource usage and print it out.
+ */
 static void
 print_usage(int op, id_t id)
 {


More information about the p4-projects mailing list