1swapctl(2)                       System Calls                       swapctl(2)
2
3
4

NAME

6       swapctl - manage swap space
7

SYNOPSIS

9       #include <sys/stat.h>
10       #include <sys/swap.h>
11
12       int swapctl(int cmd, void *arg);
13
14

DESCRIPTION

16       The  swapctl()  function  adds,   deletes, or returns information about
17       swap resources. cmd specifies one of the following options contained in
18       <sys/swap.h>:
19
20         SC_ADD        /* add a resource for swapping */
21         SC_LIST       /* list the resources for swapping */
22         SC_REMOVE     /* remove a resource for swapping */
23         SC_GETNSWP    /* return number of swap resources */
24
25
26
27       When  SC_ADD  or  SC_REMOVE is specified, arg is a pointer to a swapres
28       structure containing the following members:
29
30         char    *sr_name;    /* pathname of resource */
31         off_t   sr_start;    /* offset to start of swap area */
32         off_t   sr_length;   /* length of swap area */
33
34
35
36       The sr_start and sr_length members are specified in 512-byte blocks.  A
37       swap resource can only be removed by specifying the same values for the
38       sr_start and sr_length members as were specified  when  it  was  added.
39       Swap  resources  need  not  be  removed in the order in which they were
40       added.
41
42
43       When SC_LIST is specified, arg is a pointer to  a  swaptable  structure
44       containing the following members:
45
46         int             swt_n;       /* number of swapents following */
47         struct swapent  swt_ent[];   /* array of swt_n swapents */
48
49
50
51       A swapent structure contains the following members:
52
53         char   *ste_path;    /* name of the swap file */
54         off_t  ste_start;    /* starting block for swapping */
55         off_t  ste_length;   /* length of swap area */
56         long   ste_pages;    /* number of pages for swapping */
57         long   ste_free;     /* number of ste_pages free */
58         long   ste_flags;    /* ST_INDEL bit set if swap file */
59                              /* is now being deleted */
60
61
62
63       The  SC_LIST function causes swapctl() to return at most swt_n entries.
64       The return value of swapctl() is  the  number  actually  returned.  The
65       ST_INDEL  bit  is  turned  on  in  ste_flags if the swap file is in the
66       process of being deleted.
67
68
69       When SC_GETNSWP is specified, swapctl() returns as its value the number
70       of swap resources in use. arg is ignored for this operation.
71
72
73       The  SC_ADD  and  SC_REMOVE functions will fail if calling process does
74       not have appropriate privileges.
75

RETURN VALUES

77       Upon successful completion, the function swapctl() returns a value of 0
78       for  SC_ADD or SC_REMOVE, the number of struct swapent entries actually
79       returned  for SC_LIST, or the number  of  swap  resources  in  use  for
80       SC_GETNSWP. Upon failure, the function swapctl() returns a value  of −1
81       and sets errno to indicate an error.
82

ERRORS

84       Under the following conditions, the function swapctl() fails  and  sets
85       errno to:
86
87       EEXIST          Part  of  the range specified by sr_start and sr_length
88                       is already being used for  swapping  on  the  specified
89                       resource (SC_ADD).
90
91
92       EFAULT          Either  arg,  sr_name, or ste_path points to an illegal
93                       address.
94
95
96       EINVAL          The specified function value is  not  valid,  the  path
97                       specified  is  not a swap resource (SC_REMOVE), part of
98                       the range specified by sr_start and sr_length lies out‐
99                       side  the resource specified (SC_ADD), or the specified
100                       swap area is less than one page (SC_ADD).
101
102
103       EISDIR          The path specified for SC_ADD is a directory.
104
105
106       ELOOP           Too many symbolic links were encountered in translating
107                       the pathname provided to SC_ADD or SC_REMOVE.
108
109
110       ENAMETOOLONG    The  length  of  a  component of the path specified for
111                       SC_ADD or SC_REMOVE exceeds NAME_MAX characters or  the
112                       length  of  the  path  exceeds  PATH_MAX characters and
113                       _POSIX_NO_TRUNC is in effect.
114
115
116       ENOENT          The pathname specified for SC_ADD or SC_REMOVE does not
117                       exist.
118
119
120       ENOMEM          An  insufficient  number  of  struct swapent structures
121                       were provided to SC_LIST, or  there  were  insufficient
122                       system  storage resources available during an SC_ADD or
123                       SC_REMOVE, or the system would  not  have  enough  swap
124                       space after an SC_REMOVE.
125
126
127       ENOSYS          The pathname specified for SC_ADD or SC_REMOVE is not a
128                       file or block special device.
129
130
131       ENOTDIR         Pathname provided to SC_ADD or  SC_REMOVE  contained  a
132                       component in the path prefix that was not a directory.
133
134
135       EPERM           The  {PRIV_SYS_MOUNT} was not asserted in the effective
136                       set of the calling process.
137
138
139       EROFS           The pathname specified for SC_ADD is a  read-only  file
140                       system.
141
142
143
144       Additionally,  the  swapctl()  function will fail for 32-bit interfaces
145       if:
146
147       EOVERFLOW    The amount of swap space configured on the machine is  too
148                    large to be represented by a 32-bit quantity.
149
150

EXAMPLES

152       Example 1 The usage of the SC_GETNSWP and SC_LIST commands.
153
154
155       The  following  example  demonstrates  the  usage of the SC_GETNSWP and
156       SC_LIST commands.
157
158
159         #include <sys/stat.h>
160         #include <sys/swap.h>
161         #include <stdio.h>
162
163         #define MAXSTRSIZE 80
164
165         main(argc, argv)
166             int            argc;
167             char           *argv[];
168         {
169             swaptbl_t      *s;
170             int            i, n, num;
171             char           *strtab;    /* string table for path names */
172
173         again:
174             if ((num = swapctl(SC_GETNSWP, 0)) == -1) {
175                 perror("swapctl: GETNSWP");
176                 exit(1);
177             }
178             if (num == 0) {
179                 fprintf(stderr, "No Swap Devices Configured\n");
180                 exit(2);
181             }
182             /* allocate swaptable for num+1 entries */
183             if ((s = (swaptbl_t *)
184                 malloc(num * sizeof(swapent_t) +
185                     sizeof(struct swaptable))) ==
186                 (void *) 0) {
187                 fprintf(stderr, "Malloc Failed\n");
188                 exit(3);
189             }
190             /* allocate num+1 string holders */
191             if ((strtab = (char *)
192                 malloc((num + 1) * MAXSTRSIZE)) == (void *) 0) {
193                 fprintf(stderr, "Malloc Failed\n");
194                 exit(3);
195             }
196             /* initialize string pointers */
197             for (i = 0; i < (num + 1); i++) {
198                 s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
199             }
200
201             s->swt_n = num + 1;
202             if ((n = swapctl(SC_LIST, s)) < 0) {
203                 perror("swapctl");
204                 exit(1);
205             }
206             if (n > num) {        /* more were added */
207                 free(s);
208                 free(strtab);
209                 goto again;
210             }
211             for (i = 0; i < n; i++)
212                 printf("%s %ld\n",
213                     s->swt_ent[i].ste_path, s->swt_ent[i].ste_pages);
214         }
215
216

SEE ALSO

218       privileges(5)
219
220
221
222SunOS 5.11                        25 Sep 1997                       swapctl(2)
Impressum