putdgrp.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
6330N/A * The contents of this file are subject to the terms of the 6301N/A * Common Development and Distribution License, Version 1.0 only 6301N/A * (the "License"). You may not use this file except in compliance 6330N/A * See the License for the specific language governing permissions 6330N/A * and limitations under the License. 6330N/A * When distributing Covered Code, include this CDDL HEADER in each 6330N/A * If applicable, add the following below this CDDL HEADER, with the 6330N/A * fields enclosed by brackets "[]" replaced with your own identifying 6330N/A * information: Portions Copyright [yyyy] [name of copyright owner] 6330N/A * Copyright (c) 1996-1997, by Sun Microsystems, Inc. 6330N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ /* All Rights Reserved */ #
pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2 */ * _putdgrptabrec() Write a device-group record to a stream * _rmdgrptabrec() Remove a device-group table record * _rmdgrpmems() Remove specific members from a device group * _adddgrptabrec() Add a device-group record to the table * G L O B A L R E F E R E N C E S * <stdio.h> Standard I/O definitions * <fcntl.h> Definitions for file control * <errno.h> Error handling definitions * <string.h> String Handling Definitions * <unistd.h> Standard UNIX(r) Definitions * "devtab.h" Local Device Management Definitions * L O C A L D E F I N I T I O N S * TDGTABNM Name of the temporary device-group table (in the * directory of the existing table) * TDGTABNMLN Number of characters added to the directory * name -- the length of the device-group table temp name * lkdgrptab Locks the device-group table * unlkdgrptab Unlocks the device-group table * mkdgrptabent Builds a device-group table entry from the alias and the * list of attr=val pairs given * opennewdgrptab Opens a new device-group table (as a temp file) * mknewdgrptab Makes the temp device-group table the new dgrptab * rmnewdgrptab Remove the temporary device-group table and free space * allocated to the filename of that file. * FILE *opennewdgrptab(pname) * Generates a temporary device-group table name from the existing * device-group table name (in the same directory) and opens that * file for writing. It puts a pointer to the malloc()ed space * containing the temp device-group table's name at the place * pname Pointer to the char * to contain the address of the name * A pointer to the opened stream or (FILE *) NULL if an error occurred. * If an error occurred, "errno" will be set to reflect the problem. char *
oldname;
/* Ptr to the dgrptab name */ char *
buf;
/* Ptr to the temp file's name */ char *
dirname;
/* Directory containing dgrptab */ char *p;
/* Ptr to last '/' in dgrptab name */ int fd;
/* Opened file descriptor */ FILE *
fp;
/* Opened file pointer */ struct stat64 sbuf;
/* stat buf for old dgrptab file */ /* Get the name of the device-group table */ * It is possible for us to have sufficient permissions to create * the new file without having sufficient permissions to write the * original dgrptab file. For consistency with the operations which * modify the original file by writing it directly we require write * permissions for the original file in order to make a new one. /* Get the directory that the device-group table lives in */ /* Get space for the temp dgrptab pathname */ * Build the name of the temp dgrptab and open the * file. We must reset the owner, group and perms to those * of the original dgrptab file. /* Free the space containing the dgrptab's name */ /* Finished. Return what we've got */ * int rmnewdgrptab(tempname) * Unlink the temp dgrptab and free the memory allocated to * contain the name of that file * tempname Name of the temporary file * TRUE if successful, FALSE otherwise /* Unlink the temporary file */ * int mknewdgrptab(tempname) * Make the temporary device-group table the new system * tempname Name of the temporary file * TRUE if successful, FALSE otherwise * - Need to use rename() someday instead of link()/unlink() * - This code is somewhat ineffecient in that asks for the name * of the device-group table more than once. Done so that we don't * have to manage that space, but this may be somewhat lazy. char *
dgrpname;
/* Ptr to the dgrptab's name */ int noerr;
/* FLAG, TRUE if all's well */ /* Get the dgrptab's pathname */ /* Unlink the existing file */ /* Make the temp file the real device-group table */ /* Remove the temp file */ /* Free the dgrptab's name */ /* Finished. Return success indicator */ * int lkdgrptab(o_mode, lktype) * Lock the device-group table for writing. If it isn't available, it * o_mode The open() mode to use when opening the device-group table * lktype The type of lock to apply * TRUE if successful, FALSE with errno set otherwise int noerr;
/* FLAG, TRUE if no error */ int olderrno;
/* Former value of errno */ /* Close the device-group table (if it's open) */ * Lock the device-group table (for writing). If it's not * available, wait until it is, then close and open the * table (modify and delete change the table!) and try /* Build the locking structure */ /* Keep on going until we lock the file or an error happens */ * If errno=EACCES, it's because the file's locked by someone * else. Wait for the file to be unlocked, then close and * reopen the file and try the lock again. }
/* End while (fcntl() && !noerr) */ /* Don't keep file open if an error happened */ * Unlock the locked device-group table. * Whatever fcntl() returns... int noerr;
/* FLAG, TRUE if all's well */ /* Build the locking structure */ * struct dgrptabent *mkdgrptabent(dgroup, members) * This function builds a struct dgrptabent structure describing the * device-group <dgroup> so that it contains the members in the * membership list <members>. * dgroup The device-group being added to the device-group table * members The members of the device-group * Returns: struct dgrptabent * * A completed struct dgrptabent structure containing the description * of the device group. The structure, and all of the data in the * structure are each in space allocated using the malloc() function * and should be freed using the free() function (or the _freedgrptabent() char *
dgroup,
/* Device-group being created (or modified) */ char **
members)
/* Members to add to that entry */ char **
pp;
/* Ptr into list of ptrs */ int noerr;
/* TRUE if all's well */ /* Get space for the structure */ /* Fill in default values */ /* Fill in the device-group name */ /* Add membership to the structure */ }
/* End membership processing loop */ * If there was a problem, clean up the mess we've made }
else noerr =
FALSE;
/* if (malloc(dgrptabent space)) */ * int _putdgrptabrec(stream, rec) * Write a device-group table record containing the information in the * struct dgrptab structure <rec> to the current position of the * standard I/O stream <stream>. * stream The stream to write to * rec The structure containing the information to write * The number of characters written or EOF if there was some error. char *
buf;
/* Allocated buffer */ char *p;
/* Temp char pointer */ char *q;
/* Temp char pointer */ int count;
/* Number of chars written */ int size;
/* Size of needed buffer */ /* Comment or data record? */ * Record is a data record /* Figure out the amount of space the record needs */ /* "membername " or "membername\n" */ size++;
/* Count trailing '\n' if empty grp */ /* Alloc space for the record */ /* Write the device-group name */ /* Write the membership list */ /* Terminate the record */ * int _adddgrptabrec(dgrp, members) * If <dgrp> doesn't exist, this function adds a record to the * device-group table for that device-group. That record will * have the name <dgrp> and will have a membership described in * the list referenced by <members>. The record is added to the * If <dgrp> already exists in the table, the function adds the * members in the <members> list to the group's membership. * dgrp The name of the device-group being added to the * members A pointer to the first item of the list of members * in the device-group being added to the table. * (This value may be (char **) NULL). * TRUE if successful, FALSE with "errno" set otherwise. char *
dgrp,
/* Devgrp to add to the table */ char **
members)
/* Members for that devgrp */ struct dgrptabent *p;
/* Temp ptr to dev tab info */ struct member *
pm, *
qm, *
rm;
/* Tmp ptrs to struct member */ FILE *
fd;
/* File descr, temp file */ char *
path;
/* Ptr to new devtab name */ int noerr;
/* FLAG, TRUE if all's well */ /* Make a structure describing the new information */ * Lock the device-group table. This only returns if the * table is locked or some error occurred. It waits until the * If the device-group is already in the table, add /* Any members to add? If not, do nothing. */ /* Any existing members? */ /* Find the end of the existing membership list */ /* Append the new members to the membership list */ /* Remove any duplicates */ /* No members in the new list any more */ * Make a new device-group table, replacing the * record for the specified device-group /* While there's more records and no error ... */ * If this isn't the record we're replacing, * write it to the temporary file. Otherwise, * write the updated record }
/* if (opennewdgrptab()) */ }
/* If there's members to add */ /* Free the memory associated with the updated entry */ * Otherwise, add the device-group to the end of the table return (
noerr);
/* Return with success indicator */ * int _rmdgrptabrec(dgrp) * This function removes the record in the device-group table * for the specified device-group. * dgrp The device-group to be removed * Success indicator: TRUE if successful, FALSE with errno set otherwise. FILE *
fd;
/* Temp file's file descriptor */ char *
path;
/* Pathname of temp file */ int noerr;
/* FLAG, TRUE if all's well */ * int _rmdgrpmems(dgrp, mems, notfounds) * Remove the specified members from the membership of the specified * device-group. Any members not found in that device-group are * returned in the list referenced by <notfounds>. * dgrp The device-group from which members are to be removed * mems The address of the first element in the list of * members to remove. This list is terminated by * notfounds The place to put the address of the list of addresses * referencing the requested members that were not * members of the specified device-group * TRUE if successful, FALSE with errno set otherwise. char *
dgrp,
/* Device-group to modify */ char **
mems,
/* Members to remove */ char ***
notfounds)
/* Members req'd but not found */ struct member *
pm;
/* Ptr to member being examined */ char **
nflst;
/* Ptr to not-found list */ char **
pnf;
/* Ptr into not-found list */ char **
pp;
/* Ptr into members-to-rm list */ FILE *
fd;
/* Temp file's file descriptor */ char *
path;
/* Pathname of temp file */ int noerr;
/* TRUE if all's well */ int found;
/* TRUE if member is in membership */ int i;
/* Temp counter */ /* Lock the device-group table */ /* Nothing is "not found" yet */ /* Get the entry we're to modify */ /* Allocate space for the not-found list */ /* For each member to remove ... (if any) */ /* Compare against each member in the membership list */ /* Found. Remove from linked list */ /* Bump to the next member */ }
/* For each member in the group */ * If the requested member-to-remove wasn't found, * add it to the list of not-found members }
/* for (each requested member to remove */ * If there was no error but there was requested members * that weren't found, set the not-found list and the error * information. Otherwise, free the not-found list /* Free the description of the modified device group */ /* Unlock the original device-group table */