/* termiosmodule.c -- POSIX terminal I/O module implementation. */ #include "Python.h" /* Apparently, on SGI, termios.h won't define CTRL if _XOPEN_SOURCE is defined, so we define it here. */ #if defined(__sgi) #define CTRL(c) ((c)&037) #endif #include #ifdef __osf__ /* On OSF, sys/ioctl.h requires that struct termio already be defined, * so this needs to be included first on that platform. */ #include #endif #include /* HP-UX requires that this be included to pick up MDCD, MCTS, MDSR, * MDTR, MRI, and MRTS (appearantly used internally by some things * defined as macros; these are not used here directly). */ #ifdef HAVE_SYS_MODEM_H #include #endif /* HP-UX requires that this be included to pick up TIOCGPGRP and friends */ #ifdef HAVE_SYS_BSDTTY_H #include #endif PyDoc_STRVAR(termios__doc__, "This module provides an interface to the Posix calls for tty I/O control.\n\ For a complete description of these calls, see the Posix or Unix manual\n\ pages. It is only available for those Unix versions that support Posix\n\ termios style tty I/O control.\n\ \n\ All functions in this module take a file descriptor fd as their first\n\ argument. This can be an integer file descriptor, such as returned by\n\ sys.stdin.fileno(), or a file object, such as sys.stdin itself."); static PyObject *TermiosError; static int fdconv(PyObject* obj, void* p) { int fd; fd = PyObject_AsFileDescriptor(obj); if (fd >= 0) { *(int*)p = fd; return 1; } return 0; } PyDoc_STRVAR(termios_tcgetattr__doc__, "tcgetattr(fd) -> list_of_attrs\n\ \n\ Get the tty attributes for file descriptor fd, as follows:\n\ [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] where cc is a list\n\ of the tty special characters (each a string of length 1, except the items\n\ with indices VMIN and VTIME, which are integers when these fields are\n\ defined). The interpretation of the flags and the speeds as well as the\n\ indexing in the cc array must be done using the symbolic constants defined\n\ in this module."); static PyObject * termios_tcgetattr(PyObject *self, PyObject *args) { int fd; struct termios mode; PyObject *cc; speed_t ispeed, ospeed; PyObject *v; int i; char ch; if (!PyArg_ParseTuple(args, "O&:tcgetattr", fdconv, (void*)&fd)) return NULL; if (tcgetattr(fd, &mode) == -1) return PyErr_SetFromErrno(TermiosError); ispeed = cfgetispeed(&mode); ospeed = cfgetospeed(&mode); cc = PyList_New(NCCS); if (cc == NULL) return NULL; for (i = 0; i < NCCS; i++) { ch = (char)mode.c_cc[i]; v = PyBytes_FromStringAndSize(&ch, 1); if (v == NULL) goto err; PyList_SetItem(cc, i, v); } /* Convert the MIN and TIME slots to integer. On some systems, the MIN and TIME slots are the same as the EOF and EOL slots. So we only do this in noncanonical input mode. */ if ((mode.c_lflag & ICANON) == 0) { v = PyLong_FromLong((long)mode.c_cc[VMIN]); if (v == NULL) goto err; PyList_SetItem(cc, VMIN, v); v = PyLong_FromLong((long)mode.c_cc[VTIME]); if (v == NULL) goto err; PyList_SetItem(cc, VTIME, v); } if (!(v = PyList_New(7))) goto err; PyList_SetItem(v, 0, PyLong_FromLong((long)mode.c_iflag)); PyList_SetItem(v, 1, PyLong_FromLong((long)mode.c_oflag)); PyList_SetItem(v, 2, PyLong_FromLong((long)mode.c_cflag)); PyList_SetItem(v, 3, PyLong_FromLong((long)mode.c_lflag)); PyList_SetItem(v, 4, PyLong_FromLong((long)ispeed)); PyList_SetItem(v, 5, PyLong_FromLong((long)ospeed)); PyList_SetItem(v, 6, cc); if (PyErr_Occurred()){ Py_DECREF(v); goto err; } return v; err: Py_DECREF(cc); return NULL; } PyDoc_STRVAR(termios_tcsetattr__doc__, "tcsetattr(fd, when, attributes) -> None\n\ \n\ Set the tty attributes for file descriptor fd.\n\ The attributes to be set are taken from the attributes argument, which\n\ is a list like the one returned by tcgetattr(). The when argument\n\ determines when the attributes are changed: termios.TCSANOW to\n\ change immediately, termios.TCSADRAIN to change after transmitting all\n\ queued output, or termios.TCSAFLUSH to change after transmitting all\n\ queued output and discarding all queued input. "); static PyObject * termios_tcsetattr(PyObject *self, PyObject *args) { int fd, when; struct termios mode; speed_t ispeed, ospeed; PyObject *term, *cc, *v; int i; if (!PyArg_ParseTuple(args, "O&iO:tcsetattr", fdconv, &fd, &when, &term)) return NULL; if (!PyList_Check(term) || PyList_Size(term) != 7) { PyErr_SetString(PyExc_TypeError, "tcsetattr, arg 3: must be 7 element list"); return NULL; } /* Get the old mode, in case there are any hidden fields... */ if (tcgetattr(fd, &mode) == -1) return PyErr_SetFromErrno(TermiosError); mode.c_iflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 0)); mode.c_oflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 1)); mode.c_cflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 2)); mode.c_lflag = (tcflag_t) PyLong_AsLong(PyList_GetItem(term, 3)); ispeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 4)); ospeed = (speed_t) PyLong_AsLong(PyList_GetItem(term, 5)); cc = PyList_GetItem(term, 6); if (PyErr_Occurred()) return NULL; if (!PyList_Check(cc) || PyList_Size(cc) != NCCS) { PyErr_Format(PyExc_TypeError, "tcsetattr: attributes[6] must be %d element list", NCCS); return NULL; } for (i = 0; i < NCCS; i++) { v = PyList_GetItem(cc, i); if (PyBytes_Check(v) && PyBytes_Size(v) == 1) mode.c_cc[i] = (cc_t) * PyBytes_AsString(v); else if (PyLong_Check(v)) mode.c_cc[i] = (cc_t) PyLong_AsLong(v); else { PyErr_SetString(PyExc_TypeError, "tcsetattr: elements of attributes must be characters or integers"); return NULL; } } if (cfsetispeed(&mode, (speed_t) ispeed) == -1) return PyErr_SetFromErrno(TermiosError); if (cfsetospeed(&mode, (speed_t) ospeed) == -1) return PyErr_SetFromErrno(TermiosError); if (tcsetattr(fd, when, &mode) == -1) return PyErr_SetFromErrno(TermiosError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(termios_tcsendbreak__doc__, "tcsendbreak(fd, duration) -> None\n\ \n\ Send a break on file descriptor fd.\n\ A zero duration sends a break for 0.25-0.5 seconds; a nonzero duration\n\ has a system dependent meaning."); static PyObject * termios_tcsendbreak(PyObject *self, PyObject *args) { int fd, duration; if (!PyArg_ParseTuple(args, "O&i:tcsendbreak", fdconv, &fd, &duration)) return NULL; if (tcsendbreak(fd, duration) == -1) return PyErr_SetFromErrno(TermiosError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(termios_tcdrain__doc__, "tcdrain(fd) -> None\n\ \n\ Wait until all output written to file descriptor fd has been transmitted."); static PyObject * termios_tcdrain(PyObject *self, PyObject *args) { int fd; if (!PyArg_ParseTuple(args, "O&:tcdrain", fdconv, &fd)) return NULL; if (tcdrain(fd) == -1) return PyErr_SetFromErrno(TermiosError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(termios_tcflush__doc__, "tcflush(fd, queue) -> None\n\ \n\ Discard queued data on file descriptor fd.\n\ The queue selector specifies which queue: termios.TCIFLUSH for the input\n\ queue, termios.TCOFLUSH for the output queue, or termios.TCIOFLUSH for\n\ both queues. "); static PyObject * termios_tcflush(PyObject *self, PyObject *args) { int fd, queue; if (!PyArg_ParseTuple(args, "O&i:tcflush", fdconv, &fd, &queue)) return NULL; if (tcflush(fd, queue) == -1) return PyErr_SetFromErrno(TermiosError); Py_INCREF(Py_None); return Py_None; } PyDoc_STRVAR(termios_tcflow__doc__, "tcflow(fd, action) -> None\n\ \n\ Suspend or resume input or output on file descriptor fd.\n\ The action argument can be termios.TCOOFF to suspend output,\n\ termios.TCOON to restart output, termios.TCIOFF to suspend input,\n\ or termios.TCION to restart input."); static PyObject * termios_tcflow(PyObject *self, PyObject *args) { int fd, action; if (!PyArg_ParseTuple(args, "O&i:tcflow", fdconv, &fd, &action)) return NULL; if (tcflow(fd, action) == -1) return PyErr_SetFromErrno(TermiosError); Py_INCREF(Py_None); return Py_None; } static PyMethodDef termios_methods[] = { {"tcgetattr", termios_tcgetattr, METH_VARARGS, termios_tcgetattr__doc__}, {"tcsetattr", termios_tcsetattr, METH_VARARGS, termios_tcsetattr__doc__}, {"tcsendbreak", termios_tcsendbreak, METH_VARARGS, termios_tcsendbreak__doc__}, {"tcdrain", termios_tcdrain, METH_VARARGS, termios_tcdrain__doc__}, {"tcflush", termios_tcflush, METH_VARARGS, termios_tcflush__doc__}, {"tcflow", termios_tcflow, METH_VARARGS, termios_tcflow__doc__}, {NULL, NULL} }; #if defined(VSWTCH) && !defined(VSWTC) #define VSWTC VSWTCH #endif #if defined(VSWTC) && !defined(VSWTCH) #define VSWTCH VSWTC #endif static struct constant { char *name; long value; } termios_constants[] = { /* cfgetospeed(), cfsetospeed() constants */ {"B0", B0}, {"B50", B50}, {"B75", B75}, {"B110", B110}, {"B134", B134}, {"B150", B150}, {"B200", B200}, {"B300", B300}, {"B600", B600}, {"B1200", B1200}, {"B1800", B1800}, {"B2400", B2400}, {"B4800", B4800}, {"B9600", B9600}, {"B19200", B19200}, {"B38400", B38400}, #ifdef B57600 {"B57600", B57600}, #endif #ifdef B115200 {"B115200", B115200}, #endif #ifdef B230400 {"B230400", B230400}, #endif #ifdef B460800 {"B460800", B460800}, #endif #ifdef B500000 {"B500000", B500000}, #endif #ifdef B576000 {"B576000", B576000}, #endif #ifdef B921600 {"B921600", B921600}, #endif #ifdef B1000000 {"B1000000", B1000000}, #endif #ifdef B1152000 {"B1152000", B1152000}, #endif #ifdef B1500000 {"B1500000", B1500000}, #endif #ifdef B2000000 {"B2000000", B2000000}, #endif #ifdef B2500000 {"B2500000", B2500000}, #endif #ifdef B3000000 {"B3000000", B3000000}, #endif #ifdef B3500000 {"B3500000", B3500000}, #endif #ifdef B4000000 {"B4000000", B4000000}, #endif #ifdef CBAUDEX {"CBAUDEX", CBAUDEX}, #endif /* tcsetattr() constants */ {"TCSANOW", TCSANOW}, {"TCSADRAIN", TCSADRAIN}, {"TCSAFLUSH", TCSAFLUSH}, #ifdef TCSASOFT {"TCSASOFT", TCSASOFT}, #endif /* tcflush() constants */ {"TCIFLUSH", TCIFLUSH}, {"TCOFLUSH", TCOFLUSH}, {"TCIOFLUSH", TCIOFLUSH}, /* tcflow() constants */ {"TCOOFF", TCOOFF}, {"TCOON", TCOON}, {"TCIOFF", TCIOFF}, {"TCION", TCION}, /* struct termios.c_iflag constants */ {"IGNBRK", IGNBRK}, {"BRKINT", BRKINT}, {"IGNPAR", IGNPAR}, {"PARMRK", PARMRK}, {"INPCK", INPCK}, {"ISTRIP", ISTRIP}, {"INLCR", INLCR}, {"IGNCR", IGNCR}, {"ICRNL", ICRNL}, #ifdef IUCLC {"IUCLC", IUCLC}, #endif {"IXON", IXON}, {"IXANY", IXANY}, {"IXOFF", IXOFF}, #ifdef IMAXBEL {"IMAXBEL", IMAXBEL}, #endif /* struct termios.c_oflag constants */ {"OPOST", OPOST}, #ifdef OLCUC {"OLCUC", OLCUC}, #endif #ifdef ONLCR {"ONLCR", ONLCR}, #endif #ifdef OCRNL {"OCRNL", OCRNL}, #endif #ifdef ONOCR {"ONOCR", ONOCR}, #endif #ifdef ONLRET {"ONLRET", ONLRET}, #endif #ifdef OFILL {"OFILL", OFILL}, #endif #ifdef OFDEL {"OFDEL", OFDEL}, #endif #ifdef NLDLY {"NLDLY", NLDLY}, #endif #ifdef CRDLY {"CRDLY", CRDLY}, #endif #ifdef TABDLY {"TABDLY", TABDLY}, #endif #ifdef BSDLY {"BSDLY", BSDLY}, #endif #ifdef VTDLY {"VTDLY", VTDLY}, #endif #ifdef FFDLY {"FFDLY", FFDLY}, #endif /* struct termios.c_oflag-related values (delay mask) */ #ifdef NL0 {"NL0", NL0}, #endif #ifdef NL1 {"NL1", NL1}, #endif #ifdef CR0 {"CR0", CR0}, #endif #ifdef CR1 {"CR1", CR1}, #endif #ifdef CR2 {"CR2", CR2}, #endif #ifdef CR3 {"CR3", CR3}, #endif #ifdef TAB0 {"TAB0", TAB0}, #endif #ifdef TAB1 {"TAB1", TAB1}, #endif #ifdef TAB2 {"TAB2", TAB2}, #endif #ifdef TAB3 {"TAB3", TAB3}, #endif #ifdef XTABS {"XTABS", XTABS}, #endif #ifdef BS0 {"BS0", BS0}, #endif #ifdef BS1 {"BS1", BS1}, #endif #ifdef VT0 {"VT0", VT0}, #endif #ifdef VT1 {"VT1", VT1}, #endif #ifdef FF0 {"FF0", FF0}, #endif #ifdef FF1 {"FF1", FF1}, #endif /* struct termios.c_cflag constants */ {"CSIZE", CSIZE}, {"CSTOPB", CSTOPB}, {"CREAD", CREAD}, {"PARENB", PARENB}, {"PARODD", PARODD}, {"HUPCL", HUPCL}, {"CLOCAL", CLOCAL}, #ifdef CIBAUD {"CIBAUD", CIBAUD}, #endif #ifdef CRTSCTS {"CRTSCTS", (long)CRTSCTS}, #endif /* struct termios.c_cflag-related values (character size) */ {"CS5", CS5}, {"CS6", CS6}, {"CS7", CS7}, {"CS8", CS8}, /* struct termios.c_lflag constants */ {"ISIG", ISIG}, {"ICANON", ICANON}, #ifdef XCASE {"XCASE", XCASE}, #endif {"ECHO", ECHO}, {"ECHOE", ECHOE}, {"ECHOK", ECHOK}, {"ECHONL", ECHONL}, #ifdef ECHOCTL {"ECHOCTL", ECHOCTL}, #endif #ifdef ECHOPRT {"ECHOPRT", ECHOPRT}, #endif #ifdef ECHOKE {"ECHOKE", ECHOKE}, #endif #ifdef FLUSHO {"FLUSHO", FLUSHO}, #endif {"NOFLSH", NOFLSH}, {"TOSTOP", TOSTOP}, #ifdef PENDIN {"PENDIN", PENDIN}, #endif {"IEXTEN", IEXTEN}, /* indexes into the control chars array returned by tcgetattr() */ {"VINTR", VINTR}, {"VQUIT", VQUIT}, {"VERASE", VERASE}, {"VKILL", VKILL}, {"VEOF", VEOF}, {"VTIME", VTIME}, {"VMIN", VMIN}, #ifdef VSWTC /* The #defines above ensure that if either is defined, both are, * but both may be omitted by the system headers. ;-( */ {"VSWTC", VSWTC}, {"VSWTCH", VSWTCH}, #endif {"VSTART", VSTART}, {"VSTOP", VSTOP}, {"VSUSP", VSUSP}, {"VEOL", VEOL}, #ifdef VREPRINT {"VREPRINT", VREPRINT}, #endif #ifdef VDISCARD {"VDISCARD", VDISCARD}, #endif #ifdef VWERASE {"VWERASE", VWERASE}, #endif #ifdef VLNEXT {"VLNEXT", VLNEXT}, #endif #ifdef VEOL2 {"VEOL2", VEOL2}, #endif #ifdef B460800 {"B460800", B460800}, #endif #ifdef CBAUD {"CBAUD", CBAUD}, #endif #ifdef CDEL {"CDEL", CDEL}, #endif #ifdef CDSUSP {"CDSUSP", CDSUSP}, #endif #ifdef CEOF {"CEOF", CEOF}, #endif #ifdef CEOL {"CEOL", CEOL}, #endif #ifdef CEOL2 {"CEOL2", CEOL2}, #endif #ifdef CEOT {"CEOT", CEOT}, #endif #ifdef CERASE {"CERASE", CERASE}, #endif #ifdef CESC {"CESC", CESC}, #endif #ifdef CFLUSH {"CFLUSH", CFLUSH}, #endif #ifdef CINTR {"CINTR", CINTR}, #endif #ifdef CKILL {"CKILL", CKILL}, #endif #ifdef CLNEXT {"CLNEXT", CLNEXT}, #endif #ifdef CNUL {"CNUL", CNUL}, #endif #ifdef COMMON {"COMMON", COMMON}, #endif #ifdef CQUIT {"CQUIT", CQUIT}, #endif #ifdef CRPRNT {"CRPRNT", CRPRNT}, #endif #ifdef CSTART {"CSTART", CSTART}, #endif #ifdef CSTOP {"CSTOP", CSTOP}, #endif #ifdef CSUSP {"CSUSP", CSUSP}, #endif #ifdef CSWTCH {"CSWTCH", CSWTCH}, #endif #ifdef CWERASE {"CWERASE", CWERASE}, #endif #ifdef EXTA {"EXTA", EXTA}, #endif #ifdef EXTB {"EXTB", EXTB}, #endif #ifdef FIOASYNC {"FIOASYNC", FIOASYNC}, #endif #ifdef FIOCLEX {"FIOCLEX", FIOCLEX}, #endif #ifdef FIONBIO {"FIONBIO", FIONBIO}, #endif #ifdef FIONCLEX {"FIONCLEX", FIONCLEX}, #endif #ifdef FIONREAD {"FIONREAD", FIONREAD}, #endif #ifdef IBSHIFT {"IBSHIFT", IBSHIFT}, #endif #ifdef INIT_C_CC {"INIT_C_CC", INIT_C_CC}, #endif #ifdef IOCSIZE_MASK {"IOCSIZE_MASK", IOCSIZE_MASK}, #endif #ifdef IOCSIZE_SHIFT {"IOCSIZE_SHIFT", IOCSIZE_SHIFT}, #endif #ifdef NCC {"NCC", NCC}, #endif #ifdef NCCS {"NCCS", NCCS}, #endif #ifdef NSWTCH {"NSWTCH", NSWTCH}, #endif #ifdef N_MOUSE {"N_MOUSE", N_MOUSE}, #endif #ifdef N_PPP {"N_PPP", N_PPP}, #endif #ifdef N_SLIP {"N_SLIP", N_SLIP}, #endif #ifdef N_STRIP {"N_STRIP", N_STRIP}, #endif #ifdef N_TTY {"N_TTY", N_TTY}, #endif #ifdef TCFLSH {"TCFLSH", TCFLSH}, #endif #ifdef TCGETA {"TCGETA", TCGETA}, #endif #ifdef TCGETS {"TCGETS", TCGETS}, #endif #ifdef TCSBRK {"TCSBRK", TCSBRK}, #endif #ifdef TCSBRKP {"TCSBRKP", TCSBRKP}, #endif #ifdef TCSETA {"TCSETA", TCSETA}, #endif #ifdef TCSETAF {"TCSETAF", TCSETAF}, #endif #ifdef TCSETAW {"TCSETAW", TCSETAW}, #endif #ifdef TCSETS {"TCSETS", TCSETS}, #endif #ifdef TCSETSF {"TCSETSF", TCSETSF}, #endif #ifdef TCSETSW {"TCSETSW", TCSETSW}, #endif #ifdef TCXONC {"TCXONC", TCXONC}, #endif #ifdef TIOCCONS {"TIOCCONS", TIOCCONS}, #endif #ifdef TIOCEXCL {"TIOCEXCL", TIOCEXCL}, #endif #ifdef TIOCGETD {"TIOCGETD", TIOCGETD}, #endif #ifdef TIOCGICOUNT {"TIOCGICOUNT", TIOCGICOUNT}, #endif #ifdef TIOCGLCKTRMIOS {"TIOCGLCKTRMIOS", TIOCGLCKTRMIOS}, #endif #ifdef TIOCGPGRP {"TIOCGPGRP", TIOCGPGRP}, #endif #ifdef TIOCGSERIAL {"TIOCGSERIAL", TIOCGSERIAL}, #endif #ifdef TIOCGSOFTCAR {"TIOCGSOFTCAR", TIOCGSOFTCAR}, #endif #ifdef TIOCGWINSZ {"TIOCGWINSZ", TIOCGWINSZ}, #endif #ifdef TIOCINQ {"TIOCINQ", TIOCINQ}, #endif #ifdef TIOCLINUX {"TIOCLINUX", TIOCLINUX}, #endif #ifdef TIOCMBIC {"TIOCMBIC", TIOCMBIC}, #endif #ifdef TIOCMBIS {"TIOCMBIS", TIOCMBIS}, #endif #ifdef TIOCMGET {"TIOCMGET", TIOCMGET}, #endif #ifdef TIOCMIWAIT {"TIOCMIWAIT", TIOCMIWAIT}, #endif #ifdef TIOCMSET {"TIOCMSET", TIOCMSET}, #endif #ifdef TIOCM_CAR {"TIOCM_CAR", TIOCM_CAR}, #endif #ifdef TIOCM_CD {"TIOCM_CD", TIOCM_CD}, #endif #ifdef TIOCM_CTS {"TIOCM_CTS", TIOCM_CTS}, #endif #ifdef TIOCM_DSR {"TIOCM_DSR", TIOCM_DSR}, #endif #ifdef TIOCM_DTR {"TIOCM_DTR", TIOCM_DTR}, #endif #ifdef TIOCM_LE {"TIOCM_LE", TIOCM_LE}, #endif #ifdef TIOCM_RI {"TIOCM_RI", TIOCM_RI}, #endif #ifdef TIOCM_RNG {"TIOCM_RNG", TIOCM_RNG}, #endif #ifdef TIOCM_RTS {"TIOCM_RTS", TIOCM_RTS}, #endif #ifdef TIOCM_SR {"TIOCM_SR", TIOCM_SR}, #endif #ifdef TIOCM_ST {"TIOCM_ST", TIOCM_ST}, #endif #ifdef TIOCNOTTY {"TIOCNOTTY", TIOCNOTTY}, #endif #ifdef TIOCNXCL {"TIOCNXCL", TIOCNXCL}, #endif #ifdef TIOCOUTQ {"TIOCOUTQ", TIOCOUTQ}, #endif #ifdef TIOCPKT {"TIOCPKT", TIOCPKT}, #endif #ifdef TIOCPKT_DATA {"TIOCPKT_DATA", TIOCPKT_DATA}, #endif #ifdef TIOCPKT_DOSTOP {"TIOCPKT_DOSTOP", TIOCPKT_DOSTOP}, #endif #ifdef TIOCPKT_FLUSHREAD {"TIOCPKT_FLUSHREAD", TIOCPKT_FLUSHREAD}, #endif #ifdef TIOCPKT_FLUSHWRITE {"TIOCPKT_FLUSHWRITE", TIOCPKT_FLUSHWRITE}, #endif #ifdef TIOCPKT_NOSTOP {"TIOCPKT_NOSTOP", TIOCPKT_NOSTOP}, #endif #ifdef TIOCPKT_START {"TIOCPKT_START", TIOCPKT_START}, #endif #ifdef TIOCPKT_STOP {"TIOCPKT_STOP", TIOCPKT_STOP}, #endif #ifdef TIOCSCTTY {"TIOCSCTTY", TIOCSCTTY}, #endif #ifdef TIOCSERCONFIG {"TIOCSERCONFIG", TIOCSERCONFIG}, #endif #ifdef TIOCSERGETLSR {"TIOCSERGETLSR", TIOCSERGETLSR}, #endif #ifdef TIOCSERGETMULTI {"TIOCSERGETMULTI", TIOCSERGETMULTI}, #endif #ifdef TIOCSERGSTRUCT {"TIOCSERGSTRUCT", TIOCSERGSTRUCT}, #endif #ifdef TIOCSERGWILD {"TIOCSERGWILD", TIOCSERGWILD}, #endif #ifdef TIOCSERSETMULTI {"TIOCSERSETMULTI", TIOCSERSETMULTI}, #endif #ifdef TIOCSERSWILD {"TIOCSERSWILD", TIOCSERSWILD}, #endif #ifdef TIOCSER_TEMT {"TIOCSER_TEMT", TIOCSER_TEMT}, #endif #ifdef TIOCSETD {"TIOCSETD", TIOCSETD}, #endif #ifdef TIOCSLCKTRMIOS {"TIOCSLCKTRMIOS", TIOCSLCKTRMIOS}, #endif #ifdef TIOCSPGRP {"TIOCSPGRP", TIOCSPGRP}, #endif #ifdef TIOCSSERIAL {"TIOCSSERIAL", TIOCSSERIAL}, #endif #ifdef TIOCSSOFTCAR {"TIOCSSOFTCAR", TIOCSSOFTCAR}, #endif #ifdef TIOCSTI {"TIOCSTI", TIOCSTI}, #endif #ifdef TIOCSWINSZ {"TIOCSWINSZ", TIOCSWINSZ}, #endif #ifdef TIOCTTYGSTRUCT {"TIOCTTYGSTRUCT", TIOCTTYGSTRUCT}, #endif /* sentinel */ {NULL, 0} }; static struct PyModuleDef termiosmodule = { PyModuleDef_HEAD_INIT, "termios", termios__doc__, -1, termios_methods, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_termios(void) { PyObject *m; struct constant *constant = termios_constants; m = PyModule_Create(&termiosmodule); if (m == NULL) return NULL; if (TermiosError == NULL) { TermiosError = PyErr_NewException("termios.error", NULL, NULL); } Py_INCREF(TermiosError); PyModule_AddObject(m, "error", TermiosError); while (constant->name != NULL) { PyModule_AddIntConstant(m, constant->name, constant->value); ++constant; } return m; } href='#n381'>381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5SMcache.c
 *			Nov 13 2006
 *			James Laird <jlaird@hdfgroup.org>
 *
 * Purpose:		Implement shared message metadata cache methods.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5SMmodule.h"         /* This source code file is part of the H5SM module */


/***********/
/* Headers */
/***********/
#include "H5Eprivate.h"		/* Error handling		  	*/
#include "H5Fprivate.h"		/* File access                          */
#include "H5FLprivate.h"	/* Free Lists                           */
#include "H5MFprivate.h"        /* File memory management		*/
#include "H5MMprivate.h"	/* Memory management			*/
#include "H5SMpkg.h"            /* Shared object header messages        */
#include "H5WBprivate.h"        /* Wrapped Buffers                      */


/****************/
/* Local Macros */
/****************/


/******************/
/* Local Typedefs */
/******************/


/********************/
/* Local Prototypes */
/********************/

/* Metadata cache (H5AC) callbacks */
static herr_t H5SM__cache_table_get_initial_load_size(void *udata, size_t *image_len);
static htri_t H5SM__cache_table_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
static void *H5SM__cache_table_deserialize(const void *image, size_t len,
    void *udata, hbool_t *dirty); 
static herr_t H5SM__cache_table_image_len(const void *thing, size_t *image_len);
static herr_t H5SM__cache_table_serialize(const H5F_t *f, void *image,
    size_t len, void *thing); 
static herr_t H5SM__cache_table_free_icr(void *thing);

static herr_t H5SM__cache_list_get_initial_load_size(void *udata, size_t *image_len);
static htri_t H5SM__cache_list_verify_chksum(const void *image_ptr, size_t len, void *udata_ptr);
static void *H5SM__cache_list_deserialize(const void *image, size_t len,
    void *udata, hbool_t *dirty); 
static herr_t H5SM__cache_list_image_len(const void *thing, size_t *image_len);
static herr_t H5SM__cache_list_serialize(const H5F_t *f, void *image,
    size_t len, void *thing);
static herr_t H5SM__cache_list_free_icr(void *thing);


/*********************/
/* Package Variables */
/*********************/

/* H5SM inherits cache-like properties from H5AC */
const H5AC_class_t H5AC_SOHM_TABLE[1] = {{
    H5AC_SOHM_TABLE_ID,                 /* Metadata client ID */
    "shared message table",             /* Metadata client name (for debugging) */
    H5FD_MEM_SOHM_TABLE,                /* File space memory type for client */
    H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
    H5SM__cache_table_get_initial_load_size,    /* 'get_initial_load_size' callback */
    NULL,				/* 'get_final_load_size' callback */
    H5SM__cache_table_verify_chksum,	/* 'verify_chksum' callback */
    H5SM__cache_table_deserialize,      /* 'deserialize' callback */
    H5SM__cache_table_image_len,        /* 'image_len' callback */
    NULL,                               /* 'pre_serialize' callback */
    H5SM__cache_table_serialize,        /* 'serialize' callback */
    NULL,                               /* 'notify' callback */
    H5SM__cache_table_free_icr,         /* 'free_icr' callback */
    NULL,                               /* 'fsf_size' callback */
}};

const H5AC_class_t H5AC_SOHM_LIST[1] = {{
    H5AC_SOHM_LIST_ID,                  /* Metadata client ID */
    "shared message list",              /* Metadata client name (for debugging) */
    H5FD_MEM_SOHM_TABLE,                /* File space memory type for client */
    H5AC__CLASS_NO_FLAGS_SET,           /* Client class behavior flags */
    H5SM__cache_list_get_initial_load_size,     /* 'get_initial_load_size' callback */
    NULL,				/* 'get_final_load_size' callback */
    H5SM__cache_list_verify_chksum,	/* 'verify_chksum' callback */
    H5SM__cache_list_deserialize,       /* 'deserialize' callback */
    H5SM__cache_list_image_len,         /* 'image_len' callback */
    NULL,                               /* 'pre_serialize' callback */
    H5SM__cache_list_serialize,         /* 'serialize' callback */
    NULL,                               /* 'notify' callback */
    H5SM__cache_list_free_icr,          /* 'free_icr' callback */
    NULL,                               /* 'fsf_size' callback */
}};


/*****************************/
/* Library Private Variables */
/*****************************/


/*******************/
/* Local Variables */
/*******************/



/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_get_initial_load_size()
 *
 * Purpose:	Return the size of the master table of Shared Object Header 
 *		Message indexes on disk.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_table_get_initial_load_size(void *_udata, size_t *image_len)
{
    const H5SM_table_cache_ud_t *udata = (const H5SM_table_cache_ud_t *)_udata;  /* User data for callback */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(udata);
    HDassert(udata->f);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = H5SM_TABLE_SIZE(udata->f);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM__cache_table_get_initial_load_size() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_verify_chksum
 *
 * Purpose:     Verify the computed checksum of the data structure is the
 *              same as the stored chksum.
 *
 * Return:      Success:        TRUE/FALSE
 *              Failure:        Negative
 *
 * Programmer: 	Vailin Choi; Aug 2015
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5SM__cache_table_verify_chksum(const void *_image, size_t len, void H5_ATTR_UNUSED *_udata)
{
    const uint8_t *image = (const uint8_t *)_image;       /* Pointer into raw data buffer */
    uint32_t stored_chksum;     /* Stored metadata checksum value */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    htri_t ret_value = TRUE;	/* Return value */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(image);

    /* Get stored and computed checksums */
    H5F_get_checksums(image, len, &stored_chksum, &computed_chksum);

    if(stored_chksum != computed_chksum)
        ret_value = FALSE;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM__cache_table_verify_chksum() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_deserialize
 *
 * Purpose:	Given a buffer containing the on disk representation of the 
 *		master table of Shared Object Header Message indexes, deserialize
 *		the table, copy the contents into a newly allocated instance of 
 *		H5SM_master_table_t, and return a pointer to the new instance.
 *
 * Return:      Success:        Pointer to in core representation
 *              Failure:        NULL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static void *
H5SM__cache_table_deserialize(const void *_image, size_t len, void *_udata,
    hbool_t H5_ATTR_UNUSED *dirty)
{
    H5F_t		   *f;                  /* File pointer -- from user data */
    H5SM_master_table_t    *table = NULL;       /* Shared message table that we deserializing */
    H5SM_table_cache_ud_t  *udata = (H5SM_table_cache_ud_t *)_udata;    /* Pointer to user data */
    const uint8_t          *image = (const uint8_t *)_image;    /* Pointer into input buffer */
    uint32_t                stored_chksum;      /* Stored metadata checksum value */
    size_t                  u;                  /* Counter variable for index headers */
    void                   *ret_value = NULL;   /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(image);
    HDassert(len > 0);
    HDassert(udata);
    HDassert(udata->f);
    f = udata->f;
    HDassert(dirty);

    /* Verify that we're reading version 0 of the table; this is the only
     * version defined so far.
     */
    HDassert(H5F_SOHM_VERS(f) == HDF5_SHAREDHEADER_VERSION);

    /* Allocate space for the master table in memory */
    if(NULL == (table = H5FL_CALLOC(H5SM_master_table_t)))
        HGOTO_ERROR(H5E_SOHM, H5E_NOSPACE, NULL, "memory allocation failed")

    /* Read number of indexes and version from file superblock */
    table->num_indexes = H5F_SOHM_NINDEXES(f);
    HDassert(table->num_indexes > 0);

    /* Compute the size of the SOHM table header on disk.  This is the "table"
     * itself plus each index within the table
     */
    table->table_size = H5SM_TABLE_SIZE(f);
    HDassert(table->table_size == len);

    /* Check magic number */
    if(HDmemcmp(image, H5SM_TABLE_MAGIC, (size_t)H5_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "bad SOHM table signature")
    image += H5_SIZEOF_MAGIC;

    /* Allocate space for the index headers in memory*/
    if(NULL == (table->indexes = (H5SM_index_header_t *)H5FL_ARR_MALLOC(H5SM_index_header_t, (size_t)table->num_indexes)))
        HGOTO_ERROR(H5E_SOHM, H5E_NOSPACE, NULL, "memory allocation failed for SOHM indexes")

    /* Read in the index headers */
    for(u = 0; u < table->num_indexes; ++u) {
        /* Verify correct version of index list */
        if(H5SM_LIST_VERSION != *image++) 
            HGOTO_ERROR(H5E_SOHM, H5E_VERSION, NULL, "bad shared message list version number")

        /* Type of the index (list or B-tree) */
        table->indexes[u].index_type= (H5SM_index_type_t)*image++;

        /* Type of messages in the index */
        UINT16DECODE(image, table->indexes[u].mesg_types);

        /* Minimum size of message to share */
        UINT32DECODE(image, table->indexes[u].min_mesg_size);

        /* List cutoff; fewer than this number and index becomes a list */
        UINT16DECODE(image, table->indexes[u].list_max);

        /* B-tree cutoff; more than this number and index becomes a B-tree */
        UINT16DECODE(image, table->indexes[u].btree_min);

        /* Number of messages shared */
        UINT16DECODE(image, table->indexes[u].num_messages);

        /* Address of the actual index */
        H5F_addr_decode(f, &image, &(table->indexes[u].index_addr));

        /* Address of the index's heap */
        H5F_addr_decode(f, &image, &(table->indexes[u].heap_addr));

	/* Compute the size of a list index for this SOHM index */
        table->indexes[u].list_size = H5SM_LIST_SIZE(f, table->indexes[u].list_max);
    } /* end for */

    /* checksum verification already done in verify_chksum cb */

    /* Read in checksum */
    UINT32DECODE(image, stored_chksum);

    /* Sanity check */
    HDassert((size_t)(image - (const uint8_t *)_image) == table->table_size);

    /* Set return value */
    ret_value = table;

done:
    if(!ret_value && table)
        if(H5SM_table_free(table) < 0)
            HDONE_ERROR(H5E_SOHM, H5E_CANTFREE, NULL, "unable to destroy sohm table")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM__cache_table_deserialize() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_image_len
 *
 * Purpose:	Compute the size in bytes of the specified instance of
 *		H5SM_master_table_t on disk, and return it in *image_len.
 *		On failure, the value of *image_len is undefined.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_table_image_len(const void *_thing, size_t *image_len)
{
    const H5SM_master_table_t *table = (const H5SM_master_table_t *)_thing;     /* Shared message table to query */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(table);
    HDassert(table->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(table->cache_info.type == H5AC_SOHM_TABLE);
    HDassert(image_len);

    *image_len = table->table_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM__cache_table_image_len() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_serialize
 *
 * Purpose:	Serialize the contents of the supplied shared message table, and
 *		load this data into the supplied buffer.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_table_serialize(const H5F_t *f, void *_image, size_t len,
    void *_thing)
{
    H5SM_master_table_t *table = (H5SM_master_table_t *)_thing; /* Shared message table to encode */
    uint8_t            *image = (uint8_t *)_image;      /* Pointer into raw data buffer */
    uint32_t            computed_chksum;    /* Computed metadata checksum value */
    size_t              u;                  /* Counter variable */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(f);
    HDassert(image);
    HDassert(table);
    HDassert(table->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(table->cache_info.type == H5AC_SOHM_TABLE);
    HDassert(table->table_size == len);

    /* Verify that we're writing version 0 of the table; this is the only
     * version defined so far.
     */
    HDassert(H5F_SOHM_VERS(f) == HDF5_SHAREDHEADER_VERSION);

    /* Encode magic number */
    H5MM_memcpy(image, H5SM_TABLE_MAGIC, (size_t)H5_SIZEOF_MAGIC);
    image += H5_SIZEOF_MAGIC;

    /* Encode each index header */
   for(u = 0; u < table->num_indexes; ++u) {
        /* Version for this list */
        *image++ = H5SM_LIST_VERSION;

        /* Is message index a list or a B-tree? */
        *image++ = table->indexes[u].index_type;

        /* Type of messages in the index */
        UINT16ENCODE(image, table->indexes[u].mesg_types);

        /* Minimum size of message to share */
        UINT32ENCODE(image, table->indexes[u].min_mesg_size);

        /* List cutoff; fewer than this number and index becomes a list */
        UINT16ENCODE(image, table->indexes[u].list_max);

        /* B-tree cutoff; more than this number and index becomes a B-tree */
        UINT16ENCODE(image, table->indexes[u].btree_min);

        /* Number of messages shared */
        UINT16ENCODE(image, table->indexes[u].num_messages);

        /* Address of the actual index */
        H5F_addr_encode(f, &image, table->indexes[u].index_addr);

        /* Address of the index's heap */
        H5F_addr_encode(f, &image, table->indexes[u].heap_addr);
    } /* end for */

    /* Compute checksum on buffer */
    computed_chksum = H5_checksum_metadata(_image, (table->table_size - H5SM_SIZEOF_CHECKSUM), 0);
    UINT32ENCODE(image, computed_chksum);

    /* sanity check */
    HDassert((size_t)(image - ((uint8_t *)_image)) == table->table_size);

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM__cache_table_serialize() */

/*****************************************/
/* no H5SM_cache_table_notify() function */
/*****************************************/


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_table_free_icr
 *
 * Purpose:	Free memory used by the SOHM table.
 *
 * Note:	The metadata cache sets the object's cache_info.magic to
 *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
 *		callback (checked in assert).
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_table_free_icr(void *_thing)
{
    H5SM_master_table_t *table = (H5SM_master_table_t *)_thing;  /* Shared message table to release */
    herr_t               ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(table);
    HDassert(table->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
    HDassert(table->cache_info.type == H5AC_SOHM_TABLE);

    /* Destroy Shared Object Header Message table */
    if(H5SM_table_free(table) < 0)
        HGOTO_ERROR(H5E_SOHM, H5E_CANTRELEASE, FAIL, "unable to free shared message table")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM_cache_table_free_icr() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_list_get_initial_load_size()
 *
 * Purpose:	Return the on disk size of list of SOHM messages.  In this case,
 *		we simply look up the size in the user data, and return that value
 *		in *image_len.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_list_get_initial_load_size(void *_udata, size_t *image_len)
{
    const H5SM_list_cache_ud_t *udata = (const H5SM_list_cache_ud_t *)_udata;  	/* User data for callback */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(udata);
    HDassert(udata->header);
    HDassert(udata->header->list_size > 0);
    HDassert(image_len);

    /* Set the image length size */
    *image_len = udata->header->list_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM__cache_list_get_initial_load_size() */


/*-------------------------------------------------------------------------
 * Function:	H5SM__cache_list_verify_chksum
 *
 * Purpose:     Verify the computed checksum of the data structure is the
 *              same as the stored chksum.
 *
 * Return:      Success:        TRUE/FALSE
 *              Failure:        Negative
 *
 * Programmer:	Vailin Choi; Aug 2015
 *
 *-------------------------------------------------------------------------
 */
htri_t
H5SM__cache_list_verify_chksum(const void *_image, size_t H5_ATTR_UNUSED len, void *_udata)
{
    const uint8_t *image = (const uint8_t *)_image;       		/* Pointer into raw data buffer */
    H5SM_list_cache_ud_t *udata = (H5SM_list_cache_ud_t *)_udata;    	/* User data for callback */
    size_t chk_size;       	/* Exact size of the node with checksum at the end */
    uint32_t stored_chksum;     /* Stored metadata checksum value */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    htri_t ret_value = TRUE;	/* Return value */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(image);
    HDassert(udata);

    /* Exact size with checksum at the end */
    chk_size = H5SM_LIST_SIZE(udata->f, udata->header->num_messages);

    /* Get stored and computed checksums */
    H5F_get_checksums(image, chk_size, &stored_chksum, &computed_chksum);

    if(stored_chksum != computed_chksum)
	ret_value = FALSE;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM__cache_list_verify_chksum() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_list_deserialize
 *
 * Purpose:	Given a buffer containing the on disk image of a list of 
 *		SOHM message, deserialize the list, load it into a newly allocated 
 *		instance of H5SM_list_t, and return a pointer to same.
 *
 * Return:      Success:        Pointer to in core representation
 *              Failure:        NULL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static void *
H5SM__cache_list_deserialize(const void *_image, size_t len, void *_udata,
    hbool_t H5_ATTR_UNUSED *dirty)
{
    H5SM_list_t          *list = NULL;     /* The SOHM list being read in */
    H5SM_list_cache_ud_t *udata = (H5SM_list_cache_ud_t *)_udata;    /* User data for callback */
    H5SM_bt2_ctx_t        ctx;             /* Message encoding context */
    const uint8_t        *image = (const uint8_t *)_image;      /* Pointer into input buffer */
    uint32_t              stored_chksum;   /* Stored metadata checksum value */
    size_t                u;               /* Counter variable for messages in list */
    void                 *ret_value = NULL;   /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(image);
    HDassert(len > 0);
    HDassert(udata);
    HDassert(udata->header);
    HDassert(udata->header->list_size == len);
    HDassert(dirty);

    /* Allocate space for the SOHM list data structure */
    if(NULL == (list = H5FL_MALLOC(H5SM_list_t)))
        HGOTO_ERROR(H5E_SOHM, H5E_NOSPACE, NULL, "memory allocation failed")
    HDmemset(&list->cache_info, 0, sizeof(H5AC_info_t));

    /* Allocate list in memory as an array*/
    if(NULL == (list->messages = (H5SM_sohm_t *)H5FL_ARR_MALLOC(H5SM_sohm_t, udata->header->list_max)))
        HGOTO_ERROR(H5E_SOHM, H5E_NOSPACE, NULL, "file allocation failed for SOHM list")
    list->header = udata->header;

    /* Check magic number */
    if(HDmemcmp(image, H5SM_LIST_MAGIC, (size_t)H5_SIZEOF_MAGIC))
        HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "bad SOHM list signature")
    image += H5_SIZEOF_MAGIC;

    /* Read messages into the list array */
    ctx.sizeof_addr = H5F_SIZEOF_ADDR(udata->f);
    for(u = 0; u < udata->header->num_messages; u++) {
        if(H5SM__message_decode(image, &(list->messages[u]), &ctx) < 0)
            HGOTO_ERROR(H5E_SOHM, H5E_CANTLOAD, NULL, "can't decode shared message")

        image += H5SM_SOHM_ENTRY_SIZE(udata->f);
    } /* end for */

    /* checksum verification already done in verify_chksum cb */

    /* Read in checksum */
    UINT32DECODE(image, stored_chksum);

    /* Sanity check */
    HDassert((size_t)(image - (const uint8_t *)_image) <= udata->header->list_size);

    /* Initialize the rest of the array */
    for(u = udata->header->num_messages; u < udata->header->list_max; u++)
        list->messages[u].location = H5SM_NO_LOC;

    /* Set return value */
    ret_value = list;

done:
    if(!ret_value && list) {
        if(list->messages)
            list->messages = H5FL_ARR_FREE(H5SM_sohm_t, list->messages);
        list = H5FL_FREE(H5SM_list_t, list);
    } /* end if */

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM__cache_list_deserialize() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_list_image_len
 *
 * Purpose:	Get the size of the shared message list on disk.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_list_image_len(const void *_thing, size_t *image_len)
{
    const H5SM_list_t *list = (const H5SM_list_t *)_thing;      /* Shared message list to query */

    FUNC_ENTER_STATIC_NOERR

    /* Check arguments */
    HDassert(list);
    HDassert(list->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(list->cache_info.type == H5AC_SOHM_LIST);
    HDassert(list->header);
    HDassert(image_len);

    *image_len = list->header->list_size;

    FUNC_LEAVE_NOAPI(SUCCEED)
} /* end H5SM__cache_list_image_len() */


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_list_serialize
 *
 * Purpose:	Serialize the contents of the supplied shared message list, and
 *		load this data into the supplied buffer.
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_list_serialize(const H5F_t *f, void *_image, size_t len,
    void *_thing)
{
    H5SM_list_t *list = (H5SM_list_t *)_thing ;   /* Instance being serialized */
    H5SM_bt2_ctx_t ctx;         /* Message encoding context */
    uint8_t *image = (uint8_t *)_image;         /* Pointer into raw data buffer */
    uint32_t computed_chksum;   /* Computed metadata checksum value */
    size_t mesgs_serialized;    /* Number of messages serialized */
    size_t u;                   /* Local index variable */
    herr_t       ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(f);
    HDassert(image);
    HDassert(list);
    HDassert(list->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_MAGIC);
    HDassert(list->cache_info.type == H5AC_SOHM_LIST);
    HDassert(list->header);
    HDassert(list->header->list_size == len);

    /* Encode magic number */
    H5MM_memcpy(image, H5SM_LIST_MAGIC, (size_t)H5_SIZEOF_MAGIC);
    image += H5_SIZEOF_MAGIC;

    /* serialize messages from the messages array */
    mesgs_serialized = 0;
    ctx.sizeof_addr = H5F_SIZEOF_ADDR(f);
    for(u = 0; ((u < list->header->list_max) && (mesgs_serialized < list->header->num_messages)); u++) {
        if(list->messages[u].location != H5SM_NO_LOC) {
            if(H5SM__message_encode(image, &(list->messages[u]), &ctx) < 0)
                HGOTO_ERROR(H5E_SOHM, H5E_CANTFLUSH, FAIL, "unable to serialize shared message")

            image += H5SM_SOHM_ENTRY_SIZE(f);
            ++mesgs_serialized;
        } /* end if */
    } /* end for */

    HDassert(mesgs_serialized == list->header->num_messages);

    /* Compute checksum on buffer */
    computed_chksum = H5_checksum_metadata(_image, (size_t)(image - (uint8_t *)_image), 0);
    UINT32ENCODE(image, computed_chksum);

    /* sanity check */
    HDassert((size_t)(image - (uint8_t *)_image) <= list->header->list_size);

    /* Clear memory */
    HDmemset(image, 0, (list->header->list_size - (size_t)(image - (uint8_t *)_image)));

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5SM__cache_list_serialize() */

/****************************************/
/* no H5SM_cache_list_notify() function */
/****************************************/


/*-------------------------------------------------------------------------
 * Function:    H5SM__cache_list_free_icr
 *
 * Purpose:	Free all memory used by the list.
 *
 * Note:	The metadata cache sets the object's cache_info.magic to
 *		H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC before calling a free_icr
 *		callback (checked in assert).
 *
 * Return:      Success:        SUCCEED
 *              Failure:        FAIL
 *
 * Programmer:  John Mainzer
 *              7/28/14
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5SM__cache_list_free_icr(void *_thing)
{
    H5SM_list_t *list = (H5SM_list_t *)_thing;  /* Shared message list to release */
    herr_t       ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_STATIC

    /* Check arguments */
    HDassert(list);
    HDassert(list->cache_info.magic == H5C__H5C_CACHE_ENTRY_T_BAD_MAGIC);
    HDassert(list->cache_info.type == H5AC_SOHM_LIST);

    /* Destroy Shared Object Header Message list */
    if(H5SM_list_free(list) < 0)
        HGOTO_ERROR(H5E_SOHM, H5E_CANTRELEASE, FAIL, "unable to free shared message list")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5O_cache_list_free_icr() */