tipc: simplify print buffer handling in tipc_printf
authorErik Hugne <erik.hugne@ericsson.com>
Fri, 29 Jun 2012 04:50:22 +0000 (00:50 -0400)
committerPaul Gortmaker <paul.gortmaker@windriver.com>
Fri, 13 Jul 2012 23:28:28 +0000 (19:28 -0400)
tipc_printf was previously used both to construct debug traces
and to append data to buffers that should be sent over netlink
to the tipc-config application.  A global print_buffer was
used to format the string before it was copied to the actual
output buffer.  This could lead to concurrent access of the
global print_buffer, which then had to be lock protected.
This is simplified by changing tipc_printf to append data
directly to the output buffer using vscnprintf.

With the new implementation of tipc_printf, there is no longer
any risk of concurrent access to the internal log buffer, so
the lock (and the comments describing it) are no longer
strictly necessary.  However, there are still a few functions
that do grab this lock before resizing/dumping the log
buffer.  We leave the lock, and these functions untouched since
they will be removed with a subsequent commit that drops the
deprecated log buffer handling code

Signed-off-by: Erik Hugne <erik.hugne@ericsson.com>
Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
Signed-off-by: Paul Gortmaker <paul.gortmaker@windriver.com>
net/tipc/log.c

index 026733f249196fb519338c84b8e8644c33c9e233..d01e37a61b9384aae0e19d05c305989f64e8b8ec 100644 (file)
@@ -71,21 +71,11 @@ struct print_buf *const TIPC_LOG = &log_buf;
  * on the caller to prevent simultaneous use of the print buffer(s) being
  * manipulated.
  */
-static char print_string[TIPC_PB_MAX_STR];
 static DEFINE_SPINLOCK(print_lock);
 
 static void tipc_printbuf_move(struct print_buf *pb_to,
                               struct print_buf *pb_from);
 
-#define FORMAT(PTR, LEN, FMT) \
-{\
-       va_list args;\
-       va_start(args, FMT);\
-       LEN = vsprintf(PTR, FMT, args);\
-       va_end(args);\
-       *(PTR + LEN) = '\0';\
-}
-
 /**
  * tipc_printbuf_init - initialize print buffer to empty
  * @pb: pointer to print buffer structure
@@ -220,39 +210,17 @@ static void tipc_printbuf_move(struct print_buf *pb_to,
  */
 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
 {
-       int chars_to_add;
-       int chars_left;
-       char save_char;
-
-       spin_lock_bh(&print_lock);
-
-       FORMAT(print_string, chars_to_add, fmt);
-       if (chars_to_add >= TIPC_PB_MAX_STR)
-               strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
-
-       if (pb->buf) {
-               chars_left = pb->buf + pb->size - pb->crs - 1;
-               if (chars_to_add <= chars_left) {
-                       strcpy(pb->crs, print_string);
-                       pb->crs += chars_to_add;
-               } else if (chars_to_add >= (pb->size - 1)) {
-                       strcpy(pb->buf, print_string + chars_to_add + 1
-                              - pb->size);
-                       pb->crs = pb->buf + pb->size - 1;
-               } else {
-                       strcpy(pb->buf, print_string + chars_left);
-                       save_char = print_string[chars_left];
-                       print_string[chars_left] = 0;
-                       strcpy(pb->crs, print_string);
-                       print_string[chars_left] = save_char;
-                       pb->crs = pb->buf + chars_to_add - chars_left;
-               }
-       }
-
-       if (pb->echo)
-               printk("%s", print_string);
+       int i;
+       va_list args;
+       char *buf;
+       int len;
 
-       spin_unlock_bh(&print_lock);
+       buf = pb->crs;
+       len = pb->buf + pb->size - pb->crs;
+       va_start(args, fmt);
+       i = vscnprintf(buf, len, fmt, args);
+       va_end(args);
+       pb->crs += i;
 }
 
 /**