Hi!
> +int main(int argc, char *argv[])
> +{
> +     int lc, status;
> +     long ps;
> +     char *msg;
> +     pid_t pid;
> +     void *ptr;
> +
> +     msg = parse_opts(argc, argv, NULL, NULL);
> +     if (msg != NULL)
> +             tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
> +
> +     ps = sysconf(_SC_PAGESIZE);
> +     setup();
> +
> +     for (lc = 0; TEST_LOOPING(lc); lc++) {
> +             Tst_count = 0;
> +
> +             switch (pid = fork()) {
> +             case -1:
> +                     tst_brkm(TBROK|TERRNO, cleanup, "fork");
> +             case 0:
> +                     if (posix_memalign(&ptr, ps, ps) < 0)
> +                             tst_brkm(TBROK|TERRNO, cleanup,
> +                                             "posix_memalign");
> +                     if (madvise(ptr, ps, MADV_MERGEABLE) < 0)
> +                             tst_brkm(TBROK|TERRNO, cleanup, "madvise");
> +                     *(char *)NULL = 0; /* SIGSEGV occurs, expected. */
> +                     exit(0);
> +             default:
> +                     break;
> +             }
> +             if (waitpid(pid, &status, WUNTRACED|WCONTINUED) == -1)
> +                     tst_brkm(TBROK|TERRNO, cleanup, "waitpid");
> +     }
> +
> +     tst_resm(TPASS, "still alive.");
> +     cleanup();
> +     tst_exit();
> +}
> +
> +static void sighandler(int sig)
> +{
> +     if (sig == SIGSEGV)
> +             tst_exit();
> +     else
> +             tst_resm(TFAIL, "sighandler received invalid signal:%d", sig);
> +}

I'm little worried about the tst_xxx interface used in signal handlers
as it is not signal-async-safe and may fail (in strange circumstances).

But this is in child process, right? So you could call _exit() and check
the return value in parent.

Also the return value from tst_resm() would end up as return value from
child and wouldn't propagate to parent if I'm not mistaken.

> +void setup(void)
> +{
> +     int fd;
> +     char buf[BUFSIZ], s[BUFSIZ];
> +
> +     tst_require_root(NULL);
> +
> +     if (tst_kvercmp(2, 6, 32) < 0)
> +             tst_brkm(TCONF, NULL, "2.6.32 or greater kernel required.");
> +
> +     tst_sig(FORK, sighandler, cleanup);
> +     TEST_PAUSE;
> +
> +     /* save original /sys/kernel/mm/ksm/run value */
> +     snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "run");

No need for the snprintf() The PATH_KSM is static string so you can just
write these strings alongside as open(PATH_KSM "run", O_RDONLY);

> +     fd = open(buf, O_RDONLY);
> +     if (fd == -1)
> +             tst_brkm(TBROK|TERRNO, cleanup, "open");
> +     if (read(fd, s, 1) != 1)
> +             tst_brkm(TBROK|TERRNO, cleanup, "read");
> +     close(fd);
> +     is_running = atoi(s);
> +
> +     /* echo 1 > /sys/kernel/mm/ksm/run */
> +     if (is_running != 1) {
> +             fd = open(buf, O_WRONLY);
> +             if (fd == -1)
> +                     tst_brkm(TBROK|TERRNO, cleanup, "open");
> +             if (write(fd, "1", 1) != 1)
> +                     tst_brkm(TBROK|TERRNO, cleanup, "write");
> +             close(fd);
> +     }
> +}
> +
> +void cleanup(void)
> +{
> +     int fd;
> +     char buf[BUFSIZ], s[BUFSIZ];
> +
> +     /* restore /sys/kernel/mm/ksm/run value */
> +     if (is_running != 1) {
> +             snprintf(buf, BUFSIZ, "%s%s", PATH_KSM, "run");
> +             snprintf(s, BUFSIZ, "%d", is_running);
> +             fd = open(buf, O_WRONLY);
> +             if (fd == -1)
> +                     tst_brkm(TBROK|TERRNO, cleanup, "open");
> +             if (write(fd, s, 1) != 1)
> +                     tst_brkm(TBROK|TERRNO, cleanup, "write");
> +             close(fd);
> +     }
> +
> +     TEST_CLEANUP;

Perhaps a function write_ksm_run(int val) should be better than
duplicating the code.

> +}
> +#else
> +int main(void)
> +{
> +     tst_brkm(TCONF, NULL, "no MADV_MERGEABLE found.");
> +}
> +#endif

-- 
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, 
user administration capabilities and model configuration. Take 
the hassle out of deploying and managing Subversion and the 
tools developers use with it. 
http://p.sf.net/sfu/wandisco-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to