xref: /dflybsd-src/lib/libc/sys/sys_checkpoint.2 (revision 9caa806a14eb1ab2e76e711c7ce4a81d63a532c7)
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*9caa806aSMatthew Dillon.\" $DragonFly: src/lib/libc/sys/sys_checkpoint.2,v 1.6 2007/06/30 01:59:41 dillon 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
40c1de475dSHiten 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
49c1de475dSHiten PandyaThe
50c1de475dSHiten Pandya.Fn sys_checkpoint
51c1de475dSHiten Pandyasystem call executes a checkpoint function as specified by
528877df94SMatthew Dillon.Fa type .
538877df94SMatthew DillonSupported types are as follows:
548877df94SMatthew Dillon.Pp
55c1de475dSHiten 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.
87c1de475dSHiten PandyaThe
88c1de475dSHiten Pandya.Fa pid
898877df94SMatthew Dillonmust be specified as -1, and
908877df94SMatthew Dillon.Fa fd
918877df94SMatthew Dillonrepresents the checkpoint file.
92c1de475dSHiten PandyaThe
93c1de475dSHiten Pandya.Fa retval
94c1de475dSHiten Pandyaspecifies the value returned to the resumed program if
958877df94SMatthew Dillon.Fn sys_checkpoint
96c1de475dSHiten 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
104c1de475dSHiten Pandyais returned and the global variable
105c1de475dSHiten Pandya.Li errno
106c1de475dSHiten Pandyais set to indicate the error.
1078877df94SMatthew Dillon.Sh EXAMPLE
108c1de475dSHiten Pandya.Bd -literal -offset indent -compact
1098877df94SMatthew Dillon
1108877df94SMatthew Dillon/*
111c1de475dSHiten Pandya * Demonstrate checkpointing.  Use control-E to checkpoint
112c1de475dSHiten Pandya * the program and 'checkpt -r x.ckpt' to resume it.
1138877df94SMatthew Dillon */
1148877df94SMatthew Dillon#include <sys/types.h>
1158877df94SMatthew Dillon#include <sys/signal.h>
1168877df94SMatthew Dillon#include <sys/checkpoint.h>
1178877df94SMatthew Dillon#include <stdio.h>
1188877df94SMatthew Dillon#include <unistd.h>
1198877df94SMatthew Dillon#include <fcntl.h>
1208877df94SMatthew Dillon#include <errno.h>
1218877df94SMatthew Dillon
1228877df94SMatthew Dillonvoid docheckpoint(void);
1238877df94SMatthew Dillon
1248877df94SMatthew Dillonint wantckpt;
1258877df94SMatthew Dillon
1268877df94SMatthew Dillonvoid
1278877df94SMatthew Dillondockpt(int sig)
1288877df94SMatthew Dillon{
1298877df94SMatthew Dillon    wantckpt = 1;
1308877df94SMatthew Dillon}
1318877df94SMatthew Dillon
1328877df94SMatthew Dillonint
1338877df94SMatthew Dillonmain(int argc, char** argv)
1348877df94SMatthew Dillon{
1358877df94SMatthew Dillon     int i = 0;
1368877df94SMatthew Dillon
1378877df94SMatthew Dillon     signal(SIGCKPT, dockpt);
1388877df94SMatthew Dillon
1398877df94SMatthew Dillon     for (;;) {
1408877df94SMatthew Dillon	printf("iteration: %d\en", i);
1418877df94SMatthew Dillon	++i;
1428877df94SMatthew Dillon	sleep(1);
1438877df94SMatthew Dillon	if (wantckpt) {
1448877df94SMatthew Dillon		wantckpt = 0;
1458877df94SMatthew Dillon		printf("Checkpoint requested\en");
1468877df94SMatthew Dillon		docheckpoint();
1478877df94SMatthew Dillon	}
1488877df94SMatthew Dillon    }
1498877df94SMatthew Dillon    return(0);
1508877df94SMatthew Dillon}
1518877df94SMatthew Dillon
1528877df94SMatthew Dillonvoid
1538877df94SMatthew Dillondocheckpoint(void)
1548877df94SMatthew Dillon{
1558877df94SMatthew Dillon    int ret;
1568877df94SMatthew Dillon    int fd;
1578877df94SMatthew Dillon
1588877df94SMatthew Dillon    fd = open("x.ckpt", O_RDWR|O_CREAT|O_TRUNC, 0666);
1598877df94SMatthew Dillon    if (fd < 0) {
160c1de475dSHiten Pandya	printf("unable to create checkpoint file: %s\en",
161c1de475dSHiten Pandya		strerror(errno));
1628877df94SMatthew Dillon	return;
1638877df94SMatthew Dillon    }
1648877df94SMatthew Dillon
1658877df94SMatthew Dillon    ret = sys_checkpoint(CKPT_FREEZE, fd, -1, -1);
1668877df94SMatthew Dillon    if (ret < 0) {
167c1de475dSHiten Pandya	printf("unable to checkpoint: %s\en",
168c1de475dSHiten Pandya		strerror(errno));
1698877df94SMatthew Dillon    } else if (ret == 0) {
1708877df94SMatthew Dillon	printf("checkpoint successful, continuing\en");
1718877df94SMatthew Dillon    } else if (ret == 1) {
1728877df94SMatthew Dillon	printf("resuming from checkpoint.\en");
1738877df94SMatthew Dillon    } else {
1748877df94SMatthew Dillon	printf("unknown return value %d from sys_checkpoint\en", ret);
1758877df94SMatthew Dillon	exit(1);
1768877df94SMatthew Dillon    }
1778877df94SMatthew Dillon    /* note that the file descriptor is still valid on a resume */
1788877df94SMatthew Dillon    close(fd);
1798877df94SMatthew Dillon}
1808877df94SMatthew Dillon.Ed
1810b84df5cSSascha Wildner.Sh ERRORS
1820b84df5cSSascha Wildner.Bl -tag -width Er
1830b84df5cSSascha Wildner.It Bq Er EBADF
1840b84df5cSSascha WildnerThe given
1850b84df5cSSascha Wildner.Fa fd
1860b84df5cSSascha Wildneris not a valid regular file, socket descriptor, or pipe.  Note that not
1870b84df5cSSascha Wildnerall systems necessarily support checkpointing to sockets and pipes.
1880b84df5cSSascha Wildner.It Bq Er EPERM
1890b84df5cSSascha WildnerThe caller does not have permission to issue the checkpoint command.
1900b84df5cSSascha WildnerCheckpointing may be restricted or disabled using sysctls.
1910b84df5cSSascha Wildner.It Bq Er EIO
1920b84df5cSSascha WildnerAn I/O error occurred while reading from the file system.
1930b84df5cSSascha Wildner.It Bq Er EINVAL
1940b84df5cSSascha WildnerAn invalid parameter was specified.
1950b84df5cSSascha Wildner.El
1960b84df5cSSascha Wildner.Sh CHECKPOINT FEATURES
1970b84df5cSSascha WildnerThe system checkpointing code will save the process register state (including
1980b84df5cSSascha Wildnerfloating point registers), signal state, file descriptors representing
1990b84df5cSSascha Wildnerregular files or directories (anything that can be converted into a file
2000b84df5cSSascha Wildnerhandle for storage), and both shared and private memory mappings.
2010b84df5cSSascha WildnerPrivate, writable mappings are copied to the checkpoint file while shared
2020b84df5cSSascha Wildnermappings and stored by referencing the file handle and offset.
2030b84df5cSSascha WildnerNote that the system checkpointing code does not retain references to
2040b84df5cSSascha Wildnerdeleted files, so mappings and open descriptors of deleted files
2050b84df5cSSascha Wildnercannot be restored.
2060b84df5cSSascha WildnerUnpredictable operation will occur if a checkpoint-unaware program
2070b84df5cSSascha Wildneris restored and some of the underlying files mapped by the program
2080b84df5cSSascha Wildnerhave changed.
2090b84df5cSSascha Wildner.Pp
2100b84df5cSSascha WildnerThe system checkpointing code is not able to retain the process pid, process
2110b84df5cSSascha Wildnergroup, user/group creds, or descriptors 0, 1, and 2.  These will be inherited
2120b84df5cSSascha Wildnerfrom whomever restores the checkpoint.
2130b84df5cSSascha Wildner.Pp
2140b84df5cSSascha WildnerWhen a checkpointed program is restored modified private mappings will
2150b84df5cSSascha Wildnerbe mapped from the checkpoint file itself, but major portions of the
2160b84df5cSSascha Wildneroriginal program binary will be mapped from the original program binary.
2170b84df5cSSascha WildnerIf the resumed program is checkpointed again the system will automatically
2180b84df5cSSascha Wildnercopy any mappings from the original checkpoint file to the new one, since
2190b84df5cSSascha Wildnerthe original is likely being replaced.
2200b84df5cSSascha WildnerThe caller must not truncate the existing checkpoint file when creating
2210b84df5cSSascha Wildnera new one or specify the existing file's file descriptor as the new
2220b84df5cSSascha Wildnerone as this will destroy the data that the checkpoint operation needs
223*9caa806aSMatthew Dillonto copy to the new file.  It is best to checkpoint to a new file and then
224*9caa806aSMatthew Dillonrename-over the old, or to remove() the old file before creating the new
225*9caa806aSMatthew Dillonone so it remains valid as long as the program continues to run.
226*9caa806aSMatthew Dillon.Pp
227*9caa806aSMatthew DillonThreaded programs cannot currently be checkpointed.  The program must be
228*9caa806aSMatthew Dillonreduced to a single thread before it can be safely checkpointed.
229*9caa806aSMatthew Dillon.Pp
230*9caa806aSMatthew DillonMAP_VPAGETABLE mappings cannot currently be checkpointed.  A program must
231*9caa806aSMatthew Dillonrestore such mappings manually on resumption.  Only regular file and
232*9caa806aSMatthew Dillonanonymous memory mappings are checkpointed and restored.  Device and other
233*9caa806aSMatthew Dillonspecial mappings are not.  Only regular file descriptors are checkpointed
234*9caa806aSMatthew Dillonand restored.  Devices, pipes, sockets, and other special descriptors are
235*9caa806aSMatthew Dillonnot.  Memory wiring states are not checkpointed or restored.  madvise()
236*9caa806aSMatthew Dillonstates are not checkpointed or restored.  Basic mapping permissions are
237*9caa806aSMatthew Dilloncheckpointed and restored.
238*9caa806aSMatthew Dillon.Pp
2390b84df5cSSascha Wildner.Sh SECURITY
2400b84df5cSSascha WildnerThe sysctl
2410b84df5cSSascha Wildner.Em kern.ckptgroup
2420b84df5cSSascha Wildnercontrols which group can use system checkpointing.
2430b84df5cSSascha WildnerBy default, only users in the
2440b84df5cSSascha Wildner.Ql wheel
2450b84df5cSSascha Wildnergroup are allowed to checkpoint and restore processes.
2460b84df5cSSascha WildnerTo allow users in any group to have this capability (risky), set sysctl
2470b84df5cSSascha Wildner.Em kern.ckptgroup
2480b84df5cSSascha Wildnerto -1.
2490b84df5cSSascha Wildner.Sh SIGNALS
2500b84df5cSSascha WildnerTwo signals are associated with checkpointing.
2510b84df5cSSascha Wildner.Dv SIGCKPT
2520b84df5cSSascha Wildneris delivered via the tty ckpt character, usually control-E.  Its default
2530b84df5cSSascha Wildneraction is to checkpoint a program and continue running it.  The
2540b84df5cSSascha Wildner.Dv SIGCKPTEXIT
2550b84df5cSSascha Wildnersignal can only be delivered by
2560b84df5cSSascha Wildner.Xr kill 2 .
2570b84df5cSSascha WildnerIts default action is to checkpoint a program and then exit.
2580b84df5cSSascha Wildner.Dv SIGCKPTEXIT
2590b84df5cSSascha Wildnermight not be implemented by the system.  Both signals are defined to
2600b84df5cSSascha Wildnerbe greater or equal to signal 32 and cannot be manipulated using legacy
2610b84df5cSSascha Wildnermasking functions.
2620b84df5cSSascha Wildner.Pp
2630b84df5cSSascha WildnerIf a program overrides the default action for a checkpoint signal the
2640b84df5cSSascha Wildnersystem will not undertake any action of its own.  The program may issue
2650b84df5cSSascha Wildnerthe checkpoint command from the signal handler itself or simply set a
2660b84df5cSSascha Wildnerreminder for later action.  It is usually safest to set a reminder and
2670b84df5cSSascha Wildnerdo the actual checkpointing from your main loop.
2688877df94SMatthew Dillon.Sh SEE ALSO
269c1de475dSHiten Pandya.Xr checkpt 1 ,
270c1de475dSHiten Pandya.Xr signal 3
2718877df94SMatthew Dillon.Sh HISTORY
2728877df94SMatthew DillonThe
2738877df94SMatthew Dillon.Fn sys_checkpoint
2748877df94SMatthew Dillonfunction call
2758877df94SMatthew Dillonappeared in
276a3220ac5SSascha Wildner.Dx 1.1 .
277