On Tue, 23 Jan 2007 09:49:27 +0000
Pavel Machek <[EMAIL PROTECTED]> wrote:

> Hi!
> 
> > This adds some functions to get_kernel_console_loglevel, inspired by
> > those in suspend.c. We could also move parts to a shared file, if
> > desired. 
> 
> Yep, they should move to shared file.

Yes, they were even more equal then I remembered. Well here goes. This
patch introduces loglevel.[ch]

Index: Makefile
===================================================================
RCS file: /cvsroot/suspend/suspend/Makefile,v
retrieving revision 1.45
diff -u -r1.45 Makefile
--- Makefile    10 Jan 2007 13:24:27 -0000      1.45
+++ Makefile    24 Jan 2007 12:52:19 -0000
@@ -111,14 +111,14 @@
 splashy_funcs.o: splashy_funcs.c splashy_funcs.h
        $(CC) -g $(CFLAGS) $(CC_FLAGS) -c $< -o $@
 
-$(S2DISK):     vt.o md5.o encrypt.o config.o suspend.c swsusp.h config.h 
encrypt.h md5.h $(SPLASHOBJ)
-       $(CC) -g $(CFLAGS) $(CC_FLAGS) vt.o md5.o encrypt.o config.o suspend.c 
-o $@ $(SPLASHOBJ) $(LD_FLAGS)
+$(S2DISK):     vt.o md5.o encrypt.o config.o loglevel.o suspend.c swsusp.h 
config.h encrypt.h md5.h $(SPLASHOBJ)
+       $(CC) -g $(CFLAGS) $(CC_FLAGS) vt.o md5.o encrypt.o config.o loglevel.o 
suspend.c -o $@ $(SPLASHOBJ) $(LD_FLAGS)
 
-$(S2BOTH):     md5.o encrypt.o config.o suspend.c swsusp.h config.h encrypt.h 
md5.h s2ram.c dmidecode.c whitelist.c radeontool.c $(S2RAMOBJ) $(SPLASHOBJ)
-       $(CC) -g $(CFLAGS) -DCONFIG_BOTH $(CC_FLAGS) md5.o encrypt.o config.o 
suspend.c s2ram.c -o $@ $(S2RAMOBJ) $(SPLASHOBJ) $(LD_FLAGS) -lpci
+$(S2BOTH):     md5.o encrypt.o config.o loglevel.o suspend.c swsusp.h config.h 
encrypt.h md5.h s2ram.c dmidecode.c whitelist.c radeontool.c $(S2RAMOBJ) 
$(SPLASHOBJ)
+       $(CC) -g $(CFLAGS) -DCONFIG_BOTH $(CC_FLAGS) md5.o encrypt.o config.o 
loglevel.o suspend.c s2ram.c -o $@ $(S2RAMOBJ) $(SPLASHOBJ) $(LD_FLAGS) -lpci
 
-resume:        md5.o encrypt.o config.o resume.c swsusp.h config.h encrypt.h 
md5.h $(SPLASHOBJ)
-       $(CC) $(CFLAGS) $(CC_FLAGS) $(STATIC_CC_FLAGS) md5.o encrypt.o config.o 
vt.o resume.c $(SPLASHOBJ) -static -o resume $(LD_FLAGS) $(STATIC_LD_FLAGS)
+resume:        md5.o encrypt.o config.o loglevel.o resume.c swsusp.h config.h 
encrypt.h md5.h $(SPLASHOBJ)
+       $(CC) $(CFLAGS) $(CC_FLAGS) $(STATIC_CC_FLAGS) md5.o encrypt.o config.o 
vt.o loglevel.o resume.c $(SPLASHOBJ) -static -o resume $(LD_FLAGS) 
$(STATIC_LD_FLAGS)
 
 swap-offset: swap-offset.c
        $(CC) $(CFLAGS) swap-offset.c -o swap-offset
Index: loglevel.c
===================================================================
RCS file: loglevel.c
diff -N loglevel.c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ loglevel.c  24 Jan 2007 12:52:19 -0000
@@ -0,0 +1,63 @@
+/* loglevel.c - routines to modify kernel console loglevel
+ *
+ * Released under GPL v2.
+ * (c) 2007 Tim Dijkstra
+ */
+
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+
+
+static FILE *printk_file;
+static int proc_mounted = 0;
+
+inline void open_printk(void)
+{
+       struct stat stat_buf;
+       char *procname = "/proc/sys/kernel/printk";
+
+       if (stat(procname, &stat_buf) && errno == ENOENT) {
+               if (mount("none", "/proc", "proc", 0, NULL)) {
+                       fprintf(stderr, "resume: Could not mount proc\n");
+                       return;
+               } else
+                       proc_mounted = 1;
+       }
+
+        printk_file = fopen(procname, "r+");
+}
+
+inline int get_kernel_console_loglevel(void)
+{
+        int level = -1;
+
+        if (printk_file) {
+                rewind(printk_file);
+                fscanf(printk_file, "%d", &level);
+        }
+        return level;
+}
+
+inline void set_kernel_console_loglevel(int level)
+{
+        if (printk_file) {
+                rewind(printk_file);
+                fprintf(printk_file, "%d\n", level);
+                fflush(printk_file);
+        }
+
+}
+
+inline void close_printk(void)
+{
+        if (printk_file)
+                fclose(printk_file);
+
+       if (proc_mounted)
+               umount("/proc");
+}
+
Index: loglevel.h
===================================================================
RCS file: loglevel.h
diff -N loglevel.h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ loglevel.h  24 Jan 2007 12:52:19 -0000
@@ -0,0 +1,10 @@
+/* loglevel.h - routines to modify kernel console loglevel
+ *
+ * Released under GPL v2.
+ * (c) 2007 Tim Dijkstra
+ */
+
+inline void open_printk(void);
+inline int get_kernel_console_loglevel(void);
+inline void set_kernel_console_loglevel(int level);
+inline void close_printk(void);
Index: resume.c
===================================================================
RCS file: /cvsroot/suspend/suspend/resume.c,v
retrieving revision 1.38
diff -u -r1.38 resume.c
--- resume.c    24 Jan 2007 12:41:40 -0000      1.38
+++ resume.c    24 Jan 2007 12:52:20 -0000
@@ -16,7 +16,6 @@
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
-#include <sys/mount.h>
 #include <sys/time.h>
 #include <time.h>
 #include <syscall.h>
@@ -37,6 +36,7 @@
 #include "config.h"
 #include "md5.h"
 #include "splash.h"
+#include "loglevel.h"
 
 static char snapshot_dev_name[MAX_STR_LEN] = SNAPSHOT_DEVICE;
 static char resume_dev_name[MAX_STR_LEN] = RESUME_DEVICE;
@@ -716,29 +716,6 @@
        return error;
 }
 
-static void set_kernel_console_loglevel(int level)
-{
-       FILE *file;
-       struct stat stat_buf;
-       char *procname = "/proc/sys/kernel/printk";
-       int proc_mounted = 0;
-
-       if (stat(procname, &stat_buf) && errno == ENOENT) {
-               if (mount("none", "/proc", "proc", 0, NULL)) {
-                       fprintf(stderr, "resume: Could not mount proc\n");
-                       return;
-               } else
-                       proc_mounted = 1;
-       }
-       file = fopen(procname, "w");
-       if (file) {
-               fprintf(file, "%d\n", level);
-               fclose(file);
-       }
-       if (proc_mounted)
-               umount("/proc");
-}
-
 /* Parse the command line and/or configuration file */
 static inline int get_config(int argc, char *argv[])
 {
@@ -789,7 +766,7 @@
        unsigned int mem_size;
        struct stat stat_buf;
        int dev;
-       int n, error;
+       int n, error, orig_loglevel;
 
        error = get_config(argc, argv);
        if (error)
@@ -814,6 +791,10 @@
                return error;
        }
 
+       open_printk();
+       orig_loglevel = get_kernel_console_loglevel();
+       set_kernel_console_loglevel(suspend_loglevel);
+
        while (stat(resume_dev_name, &stat_buf)) {
                fprintf(stderr, 
                        "resume: Could not stat the resume device file '%s'\n"
@@ -831,8 +812,6 @@
                        resume_dev_name[n] = '\0';
        }
 
-       set_kernel_console_loglevel(suspend_loglevel);
-
        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);
 
@@ -866,9 +845,14 @@
        splash.finish();
 Close:
        close(dev);
-
-       set_kernel_console_loglevel(max_loglevel);
 Free:
+       if (error)
+           set_kernel_console_loglevel(max_loglevel);
+       else if (orig_loglevel >= 0)
+           set_kernel_console_loglevel(orig_loglevel);
+
+       close_printk();
+
        free(mem_pool);
 
        return error;
Index: suspend.c
===================================================================
RCS file: /cvsroot/suspend/suspend/suspend.c,v
retrieving revision 1.67
diff -u -r1.67 suspend.c
--- suspend.c   22 Jan 2007 17:27:56 -0000      1.67
+++ suspend.c   24 Jan 2007 12:52:20 -0000
@@ -44,6 +44,7 @@
 #include "md5.h"
 #include "splash.h"
 #include "vt.h"
+#include "loglevel.h"
 #ifdef CONFIG_BOTH
 #include "s2ram.h"
 #endif
@@ -967,39 +968,6 @@
        close(fd);
 }
 
-static FILE *printk_file;
-
-static inline void open_printk(void)
-{
-       printk_file = fopen("/proc/sys/kernel/printk", "r+");
-}
-
-static inline int get_kernel_console_loglevel(void)
-{
-       int level = -1;
-
-       if (printk_file) {
-               rewind(printk_file);
-               fscanf(printk_file, "%d", &level);
-       }
-       return level;
-}
-
-static inline void set_kernel_console_loglevel(int level)
-{
-       if (printk_file) {
-               rewind(printk_file);
-               fprintf(printk_file, "%d\n", level);
-               fflush(printk_file);
-       }
-}
-
-static inline void close_printk(void)
-{
-       if (printk_file)
-               fclose(printk_file);
-}
-
 static FILE *swappiness_file;
 
 static inline void open_swappiness(void)

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Suspend-devel mailing list
Suspend-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/suspend-devel

Reply via email to