18877df94SMatthew Dillon.\" Copyright (c) 2003,2004 The DragonFly Project. All rights reserved. 28877df94SMatthew Dillon.\" 38877df94SMatthew Dillon.\" This code is derived from software contributed to The DragonFly Project 48877df94SMatthew Dillon.\" by Matthew Dillon <dillon@backplane.com> 58877df94SMatthew Dillon.\" 68877df94SMatthew Dillon.\" Redistribution and use in source and binary forms, with or without 78877df94SMatthew Dillon.\" modification, are permitted provided that the following conditions 88877df94SMatthew Dillon.\" are met: 98877df94SMatthew Dillon.\" 108877df94SMatthew Dillon.\" 1. Redistributions of source code must retain the above copyright 118877df94SMatthew Dillon.\" notice, this list of conditions and the following disclaimer. 128877df94SMatthew Dillon.\" 2. Redistributions in binary form must reproduce the above copyright 138877df94SMatthew Dillon.\" notice, this list of conditions and the following disclaimer in 148877df94SMatthew Dillon.\" the documentation and/or other materials provided with the 158877df94SMatthew Dillon.\" distribution. 168877df94SMatthew Dillon.\" 3. Neither the name of The DragonFly Project nor the names of its 178877df94SMatthew Dillon.\" contributors may be used to endorse or promote products derived 188877df94SMatthew Dillon.\" from this software without specific, prior written permission. 198877df94SMatthew Dillon.\" 208877df94SMatthew Dillon.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 218877df94SMatthew Dillon.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 228877df94SMatthew Dillon.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 238877df94SMatthew Dillon.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 248877df94SMatthew Dillon.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 258877df94SMatthew Dillon.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 268877df94SMatthew Dillon.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 278877df94SMatthew Dillon.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 288877df94SMatthew Dillon.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 298877df94SMatthew Dillon.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 308877df94SMatthew Dillon.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 318877df94SMatthew Dillon.\" SUCH DAMAGE. 328877df94SMatthew Dillon.\" 33*c1de475dSHiten Pandya.\" $DragonFly: src/lib/libc/sys/sys_checkpoint.2,v 1.2 2004/11/24 02:56:43 hmp Exp $ 348877df94SMatthew Dillon.\" 358877df94SMatthew Dillon.Dd November 22, 2004 368877df94SMatthew Dillon.Dt SYS_CHECKPOINT 2 378877df94SMatthew Dillon.Os 388877df94SMatthew Dillon.Sh NAME 398877df94SMatthew Dillon.Nm sys_checkpoint 40*c1de475dSHiten Pandya.Nd checkpoint or restore a process 418877df94SMatthew Dillon.Sh LIBRARY 428877df94SMatthew Dillon.Lb libc 438877df94SMatthew Dillon.Sh SYNOPSIS 448877df94SMatthew Dillon.In sys/types.h 458877df94SMatthew Dillon.In sys/checkpoint.h 468877df94SMatthew Dillon.Ft int 478877df94SMatthew Dillon.Fn sys_checkpoint "int type" "int fd" "pid_t pid" "int retval" 488877df94SMatthew Dillon.Sh DESCRIPTION 49*c1de475dSHiten PandyaThe 50*c1de475dSHiten Pandya.Fn sys_checkpoint 51*c1de475dSHiten Pandyasystem call executes a checkpoint function as specified by 528877df94SMatthew Dillon.Fa type . 538877df94SMatthew DillonSupported types are as follows: 548877df94SMatthew Dillon.Pp 55*c1de475dSHiten Pandya.Bl -tag -width CKPT_FREEZE -offset indent 568877df94SMatthew Dillon.It Dv CKPT_FREEZE 578877df94SMatthew DillonGenerate a checkpoint file. 588877df94SMatthew DillonCurrently 598877df94SMatthew Dillon.Fa pid 608877df94SMatthew Dillonmust be -1 or the pid of the current process. 618877df94SMatthew DillonThe checkpoint file will be written out to 628877df94SMatthew Dillon.Fa fd , 638877df94SMatthew Dillonand 648877df94SMatthew Dillon.Fa retval 658877df94SMatthew Dillonis unused but must be specified as -1. 668877df94SMatthew DillonAs a special case, if 678877df94SMatthew Dillon.Fa pid 688877df94SMatthew Dillonand 698877df94SMatthew Dillon.Fa fd 708877df94SMatthew Dillonare both specified as -1, the system will generate a checkpoint file 718877df94SMatthew Dillonusing the system checkpoint template. 728877df94SMatthew Dillon.Pp 738877df94SMatthew DillonThis function returns 0 on success, -1 on error, and typically 1 748877df94SMatthew Dillonon resume. The value returned on resume is controlled by the 758877df94SMatthew Dillon.Fa retval 768877df94SMatthew Dillonargument passed to 778877df94SMatthew Dillon.Fn sys_checkpoint 788877df94SMatthew Dillonwhen resuming a checkpoint file. A user program which installs its 798877df94SMatthew Dillonown 808877df94SMatthew Dillon.Dv SIGCKPT 818877df94SMatthew Dillonsignal handler and calls 828877df94SMatthew Dillon.Fn sys_checkpoint 838877df94SMatthew Dillonmanually thus has control over both termination/continuance and 848877df94SMatthew Dillonresumption. 858877df94SMatthew Dillon.It Dv CKPT_THAW 868877df94SMatthew DillonRestore a checkpointed program. 87*c1de475dSHiten PandyaThe 88*c1de475dSHiten Pandya.Fa pid 898877df94SMatthew Dillonmust be specified as -1, and 908877df94SMatthew Dillon.Fa fd 918877df94SMatthew Dillonrepresents the checkpoint file. 92*c1de475dSHiten PandyaThe 93*c1de475dSHiten Pandya.Fa retval 94*c1de475dSHiten Pandyaspecifies the value returned to the resumed program if 958877df94SMatthew Dillon.Fn sys_checkpoint 96*c1de475dSHiten Pandyawas called directly. 978877df94SMatthew Dillon.Pp 988877df94SMatthew DillonThe checkpointed program will replace the current program, similar to 998877df94SMatthew Dillonan exec call. 1008877df94SMatthew Dillon.El 1018877df94SMatthew Dillon.Sh RETURN VALUES 1028877df94SMatthew DillonUpon successful completion, the value 0 is typically returned. A checkpoint 1038877df94SMatthew Dillonbeing resumed typically returns a positive value; otherwise the value -1 104*c1de475dSHiten Pandyais returned and the global variable 105*c1de475dSHiten Pandya.Li errno 106*c1de475dSHiten Pandyais set to indicate the error. 1078877df94SMatthew Dillon.Sh ERRORS 1088877df94SMatthew Dillon.Bl -tag -width Er 1098877df94SMatthew Dillon.It Bq Er EBADF 110*c1de475dSHiten PandyaThe given 111*c1de475dSHiten Pandya.Fa fd 1128877df94SMatthew Dillonis not a valid regular file, socket descriptor, or pipe. Note that not 1138877df94SMatthew Dillonall systems necessarily support checkpointing to sockets and pipes. 1148877df94SMatthew Dillon.It Bq Er EPERM 1158877df94SMatthew DillonThe caller does not have permission to issue the checkpoint command. 116*c1de475dSHiten PandyaCheckpointing may be restricted or disabled using sysctls. 1178877df94SMatthew Dillon.It Bq Er EIO 1188877df94SMatthew DillonAn I/O error occurred while reading from the file system. 1198877df94SMatthew Dillon.It Bq Er EINVAL 1208877df94SMatthew DillonAn invalid parameter was specified. 1218877df94SMatthew Dillon.El 1228877df94SMatthew Dillon.Sh CHECKPOINT FEATURES 1238877df94SMatthew DillonThe system checkpointing code will save the process register state (including 1248877df94SMatthew Dillonfloating point registers), signal state, file descriptors representing 1258877df94SMatthew Dillonregular files or directories (anything that can be converted into a file 1268877df94SMatthew Dillonhandle for storage), and both shared and private memory mappings. 1278877df94SMatthew DillonPrivate, writable mappings are copied to the checkpoint file while shared 1288877df94SMatthew Dillonmappings and stored by referencing the file handle and offset. 1298877df94SMatthew DillonNote that the system checkpointing code does not retain references to 1308877df94SMatthew Dillondeleted files, so mappings and open descriptors of deleted files 1318877df94SMatthew Dilloncannot be restored. 1328877df94SMatthew DillonUnpredictable operation will occur if a checkpoint-unaware program 1338877df94SMatthew Dillonis restored and some of the underlying files mapped by the program 1348877df94SMatthew Dillonhave changed. 1358877df94SMatthew Dillon.Pp 1368877df94SMatthew DillonThe system checkpointing code is not able to retain the process pid, process 1378877df94SMatthew Dillongroup, user/group creds, or descriptors 0, 1, and 2. These will be inherited 1388877df94SMatthew Dillonfrom whomever restores the checkpoint. 1398877df94SMatthew Dillon.Pp 1408877df94SMatthew DillonWhen a checkpointed program is restored modified private mappings will 1418877df94SMatthew Dillonbe mapped from the checkpoint file itself, but major portions of the 1428877df94SMatthew Dillonoriginal program binary will be mapped from the original program binary. 1438877df94SMatthew DillonIf the resumed program is checkpointed again the system will automatically 1448877df94SMatthew Dilloncopy any mappings from the original checkpoint file to the new one, since 1458877df94SMatthew Dillonthe original is likely being replaced. 1468877df94SMatthew DillonThe caller must not truncate the existing checkpoint file when creating 1478877df94SMatthew Dillona new one or specify the existing file's file descriptor as the new 1488877df94SMatthew Dillonone as this will destroy the data that the checkpoint operation needs 1498877df94SMatthew Dillonto copy to the new file. 150*c1de475dSHiten Pandya.Sh SECURITY 151*c1de475dSHiten PandyaThe sysctl 152*c1de475dSHiten Pandya.Em kern.ckptgroup 153*c1de475dSHiten Pandyacontrols which group can use system checkpointing. 154*c1de475dSHiten PandyaBy default, only users in the 155*c1de475dSHiten Pandya.Ql wheel 156*c1de475dSHiten Pandyagroup are allowed to checkpoint and restore processes. 157*c1de475dSHiten PandyaTo allow users in any group to have this capability (risky), set sysctl 158*c1de475dSHiten Pandya.Em kern.ckptgroup 159*c1de475dSHiten Pandyato -1. 1608877df94SMatthew Dillon.Sh SIGNALS 1618877df94SMatthew DillonTwo signals are associated with checkpointing. 1628877df94SMatthew Dillon.Dv SIGCKPT 1638877df94SMatthew Dillonis delivered via the tty ckpt character, usually control-E. Its default 1648877df94SMatthew Dillonaction is to checkpoint a program and continue running it. The 1658877df94SMatthew Dillon.Dv SIGCKPTEXIT 1668877df94SMatthew Dillonsignal can only be delivered by 1678877df94SMatthew Dillon.Xr kill 2 . 1688877df94SMatthew DillonIts default action is to checkpoint a program and then exit. 1698877df94SMatthew Dillon.Dv SIGCKPTEXIT 1708877df94SMatthew Dillonmight not be implemented by the system. Both signals are defined to 1718877df94SMatthew Dillonbe greater or equal to signal 32 and cannot be manipulated using legacy 1728877df94SMatthew Dillonmasking functions. 1738877df94SMatthew Dillon.Pp 1748877df94SMatthew DillonIf a program overrides the default action for a checkpoint signal the 1758877df94SMatthew Dillonsystem will not undertake any action of its own. The program may issue 1768877df94SMatthew Dillonthe checkpoint command from the signal handler itself or simply set a 1778877df94SMatthew Dillonreminder for later action. It is usually safest to set a reminder and 1788877df94SMatthew Dillondo the actual checkpointing from your main loop. 1798877df94SMatthew Dillon.Sh EXAMPLE 180*c1de475dSHiten Pandya.Bd -literal -offset indent -compact 1818877df94SMatthew Dillon 1828877df94SMatthew Dillon/* 183*c1de475dSHiten Pandya * Demonstrate checkpointing. Use control-E to checkpoint 184*c1de475dSHiten Pandya * the program and 'checkpt -r x.ckpt' to resume it. 1858877df94SMatthew Dillon */ 1868877df94SMatthew Dillon#include <sys/types.h> 1878877df94SMatthew Dillon#include <sys/signal.h> 1888877df94SMatthew Dillon#include <sys/checkpoint.h> 1898877df94SMatthew Dillon#include <stdio.h> 1908877df94SMatthew Dillon#include <unistd.h> 1918877df94SMatthew Dillon#include <fcntl.h> 1928877df94SMatthew Dillon#include <errno.h> 1938877df94SMatthew Dillon 1948877df94SMatthew Dillonvoid docheckpoint(void); 1958877df94SMatthew Dillon 1968877df94SMatthew Dillonint wantckpt; 1978877df94SMatthew Dillon 1988877df94SMatthew Dillonvoid 1998877df94SMatthew Dillondockpt(int sig) 2008877df94SMatthew Dillon{ 2018877df94SMatthew Dillon wantckpt = 1; 2028877df94SMatthew Dillon} 2038877df94SMatthew Dillon 2048877df94SMatthew Dillonint 2058877df94SMatthew Dillonmain(int argc, char** argv) 2068877df94SMatthew Dillon{ 2078877df94SMatthew Dillon int i = 0; 2088877df94SMatthew Dillon 2098877df94SMatthew Dillon signal(SIGCKPT, dockpt); 2108877df94SMatthew Dillon 2118877df94SMatthew Dillon for (;;) { 2128877df94SMatthew Dillon printf("iteration: %d\en", i); 2138877df94SMatthew Dillon ++i; 2148877df94SMatthew Dillon sleep(1); 2158877df94SMatthew Dillon if (wantckpt) { 2168877df94SMatthew Dillon wantckpt = 0; 2178877df94SMatthew Dillon printf("Checkpoint requested\en"); 2188877df94SMatthew Dillon docheckpoint(); 2198877df94SMatthew Dillon } 2208877df94SMatthew Dillon } 2218877df94SMatthew Dillon return(0); 2228877df94SMatthew Dillon} 2238877df94SMatthew Dillon 2248877df94SMatthew Dillonvoid 2258877df94SMatthew Dillondocheckpoint(void) 2268877df94SMatthew Dillon{ 2278877df94SMatthew Dillon int ret; 2288877df94SMatthew Dillon int fd; 2298877df94SMatthew Dillon 2308877df94SMatthew Dillon fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666); 2318877df94SMatthew Dillon if (fd < 0) { 232*c1de475dSHiten Pandya printf("unable to create checkpoint file: %s\en", 233*c1de475dSHiten Pandya strerror(errno)); 2348877df94SMatthew Dillon return; 2358877df94SMatthew Dillon } 2368877df94SMatthew Dillon 2378877df94SMatthew Dillon ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1); 2388877df94SMatthew Dillon if (ret < 0) { 239*c1de475dSHiten Pandya printf("unable to checkpoint: %s\en", 240*c1de475dSHiten Pandya strerror(errno)); 2418877df94SMatthew Dillon } else if (ret == 0) { 2428877df94SMatthew Dillon printf("checkpoint successful, continuing\en"); 2438877df94SMatthew Dillon } else if (ret == 1) { 2448877df94SMatthew Dillon printf("resuming from checkpoint.\en"); 2458877df94SMatthew Dillon } else { 2468877df94SMatthew Dillon printf("unknown return value %d from sys_checkpoint\en", ret); 2478877df94SMatthew Dillon exit(1); 2488877df94SMatthew Dillon } 2498877df94SMatthew Dillon /* note that the file descriptor is still valid on a resume */ 2508877df94SMatthew Dillon close(fd); 2518877df94SMatthew Dillon} 2528877df94SMatthew Dillon.Ed 2538877df94SMatthew Dillon.Sh SEE ALSO 254*c1de475dSHiten Pandya.Xr checkpt 1 , 255*c1de475dSHiten Pandya.Xr signal 3 2568877df94SMatthew Dillon.Sh HISTORY 2578877df94SMatthew DillonThe 2588877df94SMatthew Dillon.Fn sys_checkpoint 2598877df94SMatthew Dillonfunction call 2608877df94SMatthew Dillonappeared in 2618877df94SMatthew DillonDragonFly 1.1 . 262