PERFORCE change 166741 for review

Edward Tomasz Napierala trasz at FreeBSD.org
Wed Jul 29 18:29:00 UTC 2009


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

Change 166741 by trasz at trasz_victim on 2009/07/29 18:28:15

	Resolve numeric ids back into user/group names.

Affected files ...

.. //depot/projects/soc2009/trasz_limits/usr.sbin/hrl/hrl.c#17 edit

Differences ...

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

@@ -44,8 +44,6 @@
 static void
 print_subject(int subject, id_t id)
 {
-	struct passwd *pwd;
-	struct group *grp;
 
 	switch (subject) {
 	case HRL_SUBJECT_PROCESS:
@@ -124,18 +122,18 @@
  * This routine replaces user/group name with numeric id.
  */
 static char *
-resolve_ids(char *str)
+resolve_ids(char *rule)
 {
 	id_t id;
 	const char *subject, *textid, *rest;
 	char *resolved;
 
-	subject = strsep(&str, ":");
-	textid = strsep(&str, ":");
+	subject = strsep(&rule, ":");
+	textid = strsep(&rule, ":");
 	if (textid == NULL)
-		errx(1, "error in rule specification: no subject");
-	if (str != NULL)
-		rest = str;
+		errx(1, "error in rule specification -- no subject");
+	if (rule != NULL)
+		rest = rule;
 	else
 		rest = "";
 
@@ -160,9 +158,47 @@
 	if (resolved == NULL)
 		err(1, "asprintf");
 
-	fprintf(stderr, "resolved to '%s'\n", resolved);
+	return (resolved);
+}
+
+/*
+ * Print rules, one per line,  */
+static void
+print_rules(char *rules)
+{
+	char *rule;
+	const char *subject, *textid, *rest;
+	id_t id;
+	struct passwd *pwd;
+	struct group *grp;
+
+	while ((rule = strsep(&rules, ",")) != NULL) {
+		if (rule[0] == '\0')
+			break; /* XXX */
+		subject = strsep(&rule, ":");
+		textid = strsep(&rule, ":");
+		if (textid == NULL)
+			errx(1, "rule passed from the kernel didn't contain subject");
+		if (rule != NULL)
+			rest = rule;
+		else
+			rest = "";
+
+		/* Replace numerical user and group ids with names. */
+		if (strcasecmp(subject, "user") == 0) {
+			id = parse_user(textid);
+			pwd = getpwuid(id);
+			if (pwd != NULL)
+				textid = pwd->pw_name;
+		} else if (strcasecmp(subject, "group") == 0) {
+			id = parse_group(textid);
+			grp = getgrgid(id);
+			if (grp != NULL)
+				textid = grp->gr_name;
+		}
 
-	return (resolved);
+		printf("%s:%s:%s\n", subject, textid, rest);
+	}
 }
 
 static void
@@ -176,14 +212,11 @@
 	free(rule);
 }
 
-/*
- * Query the kernel about a resource usage and print it out.
- */
 static void
 show_limits(char *filter)
 {
 	int error;
-	char *outbuf = NULL, *tmp;
+	char *outbuf = NULL;
 	size_t outbuflen = BUFLEN_DEFAULT / 4;
 
 	do {
@@ -197,11 +230,8 @@
 			err(1, "hrl_get_limits");
 	} while (error && errno == EFBIG);
 
-	for (tmp = outbuf; *tmp != '\0'; tmp++)
-		if (*tmp == ',')
-			*tmp = '\n';
-
-	printf("Resource utilisation:\n%s\n", outbuf);
+	printf("Resource utilisation:\n");
+	print_rules(outbuf);
 	free(filter);
 	free(outbuf);
 }
@@ -254,7 +284,7 @@
 show_rules(char *filter)
 {
 	int error;
-	char *outbuf = NULL, *tmp;
+	char *outbuf = NULL;
 	size_t filterlen, outbuflen = BUFLEN_DEFAULT / 4;
 
 	if (filter != NULL)
@@ -273,19 +303,8 @@
 			err(1, "hrl_get_rules");
 	} while (error && errno == EFBIG);
 
-	for (tmp = outbuf; *tmp != '\0'; tmp++)
-		if (*tmp == ',')
-			*tmp = '\n';
-
-	if (filter != NULL)
-		printf("Defined resource limits matching \"%s\":\n%s\n", filter, outbuf);
-	else
-		printf("Defined resource limits:\n%s\n", outbuf);
-
-	/*
-	 * XXX: Resolve numeric ID-s back to names.
-	 */
-
+	printf("Defined resource limits:\n");
+	print_rules(outbuf);
 	free(outbuf);
 }
 


More information about the p4-projects mailing list