usr/src/lib/libshare/common/libsharecore.c
changeset 3957 86c9dda5df37
parent 3910 00586900e34c
child 4180 30b15f0bd3c9
equal deleted inserted replaced
3956:ea75466401e7 3957:86c9dda5df37
    50 #include <limits.h>
    50 #include <limits.h>
    51 #include <sys/param.h>
    51 #include <sys/param.h>
    52 #include <signal.h>
    52 #include <signal.h>
    53 #include <libintl.h>
    53 #include <libintl.h>
    54 
    54 
       
    55 #include <sharefs/share.h>
    55 #include "sharetab.h"
    56 #include "sharetab.h"
    56 
    57 
    57 #define	DFSTAB_NOTICE_LINES	5
    58 #define	DFSTAB_NOTICE_LINES	5
    58 static char *notice[DFSTAB_NOTICE_LINES] =	{
    59 static char *notice[DFSTAB_NOTICE_LINES] =	{
    59 	"# Do not modify this file directly.\n",
    60 	"# Do not modify this file directly.\n",
  1185 	headp = NULL;
  1186 	headp = NULL;
  1186 	tailp = NULL;
  1187 	tailp = NULL;
  1187 
  1188 
  1188 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
  1189 	if ((fp = fopen(SHARETAB, "r")) != NULL) {
  1189 		struct share	*sharetab_entry;
  1190 		struct share	*sharetab_entry;
  1190 		(void) lockf(fileno(fp), F_LOCK, 0);
       
  1191 
  1191 
  1192 		while (getshare(fp, &sharetab_entry) > 0) {
  1192 		while (getshare(fp, &sharetab_entry) > 0) {
  1193 		    newp = alloc_sharelist();
  1193 		    newp = alloc_sharelist();
  1194 		    if (newp == NULL) {
  1194 		    if (newp == NULL) {
  1195 			goto err;
  1195 			goto err;
  1826 	    free(sh->sh_descr);
  1826 	    free(sh->sh_descr);
  1827 	sh->sh_descr = NULL;
  1827 	sh->sh_descr = NULL;
  1828 }
  1828 }
  1829 
  1829 
  1830 /*
  1830 /*
  1831  * checkshare(struct share *)
       
  1832  *
       
  1833  * If the share to write to sharetab is not present, need to add.  If
       
  1834  * the share is present, replace if options are different else we want
       
  1835  * to keep it.
       
  1836  * Return values:
       
  1837  *	1 - keep
       
  1838  *	2 - replace
       
  1839  * The CHK_NEW value isn't currently returned.
       
  1840  */
       
  1841 #define	CHK_NEW		0
       
  1842 #define	CHK_KEEP	1
       
  1843 #define	CHK_REPLACE	2
       
  1844 static int
       
  1845 checkshare(struct share *sh)
       
  1846 {
       
  1847 	xfs_sharelist_t *list, *head;
       
  1848 	int err;
       
  1849 	int ret = CHK_NEW;
       
  1850 
       
  1851 	head = list = get_share_list(&err);
       
  1852 	while (list != NULL && ret == CHK_NEW) {
       
  1853 	    if (strcmp(sh->sh_path, list->path) == 0) {
       
  1854 		/* Have the same path so check if replace or keep */
       
  1855 		if (strcmp(sh->sh_opts, list->options) == 0)
       
  1856 		    ret = CHK_KEEP;
       
  1857 		else
       
  1858 		    ret = CHK_REPLACE;
       
  1859 	    }
       
  1860 	    list = list->next;
       
  1861 	}
       
  1862 	if (head != NULL) {
       
  1863 	    dfs_free_list(head);
       
  1864 	}
       
  1865 	/*
       
  1866 	 * Just in case it was added by another process after our
       
  1867 	 * scan, we always replace even if we think it is new.
       
  1868 	 */
       
  1869 	if (ret == CHK_NEW)
       
  1870 	    ret = CHK_REPLACE;
       
  1871 	return (ret);
       
  1872 }
       
  1873 
       
  1874 /*
       
  1875  * sa_update_sharetab(share, proto)
  1831  * sa_update_sharetab(share, proto)
  1876  *
  1832  *
  1877  * Update the sharetab file with info from the specified share.
  1833  * Update the sharetab file with info from the specified share.
  1878  * This could be an update or add.
  1834  * This could be an update or add.
  1879  */
  1835  */
  1880 
  1836 
  1881 int
  1837 int
  1882 sa_update_sharetab(sa_share_t share, char *proto)
  1838 sa_update_sharetab(sa_share_t share, char *proto)
  1883 {
  1839 {
  1884 	int ret = SA_OK;
  1840 	int	ret = SA_OK;
  1885 	struct share shtab;
  1841 	share_t	sh;
  1886 	char *path;
  1842 	char	*path;
  1887 	int logging = 0;
       
  1888 	FILE *sharetab;
       
  1889 	sigset_t old;
       
  1890 	int action;
       
  1891 
  1843 
  1892 	path = sa_get_share_attr(share, "path");
  1844 	path = sa_get_share_attr(share, "path");
  1893 	if (path != NULL) {
  1845 	if (path != NULL) {
  1894 	    (void) memset(&shtab, '\0', sizeof (shtab));
  1846 		(void) memset(&sh, '\0', sizeof (sh));
  1895 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
  1847 
  1896 	    if (sharetab == NULL) {
       
  1897 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
       
  1898 	    }
       
  1899 	    if (sharetab != NULL) {
       
  1900 		(void) setvbuf(sharetab, NULL, _IOLBF, BUFSIZ * 8);
       
  1901 		sablocksigs(&old);
       
  1902 		/*
  1848 		/*
  1903 		 * Fill in share structure and write it out if the
  1849 		 * Fill in share structure and send it to the kernel.
  1904 		 * share isn't already shared with the same options.
       
  1905 		 */
  1850 		 */
  1906 		(void) fillshare(share, proto, &shtab);
  1851 		(void) fillshare(share, proto, &sh);
  1907 		/*
  1852 		(void) sharefs(SHAREFS_ADD, &sh);
  1908 		 * If share is new or changed, remove the old,
  1853 		emptyshare(&sh);
  1909 		 * otherwise keep it in place since it hasn't changed.
  1854 		sa_free_attr_string(path);
  1910 		 */
  1855 	}
  1911 		action = checkshare(&shtab);
  1856 
  1912 		(void) lockf(fileno(sharetab), F_LOCK, 0);
       
  1913 		switch (action) {
       
  1914 		case CHK_REPLACE:
       
  1915 		    (void) remshare(sharetab, path, &logging);
       
  1916 		    (void) putshare(sharetab, &shtab);
       
  1917 		    break;
       
  1918 		case CHK_KEEP:
       
  1919 		    /* Don't do anything */
       
  1920 		    break;
       
  1921 		}
       
  1922 		emptyshare(&shtab);
       
  1923 		(void) fflush(sharetab);
       
  1924 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
       
  1925 		(void) fsync(fileno(sharetab));
       
  1926 		saunblocksigs(&old);
       
  1927 		(void) fclose(sharetab);
       
  1928 	    } else {
       
  1929 		if (errno == EACCES || errno == EPERM) {
       
  1930 		    ret = SA_NO_PERMISSION;
       
  1931 		} else {
       
  1932 		    ret = SA_CONFIG_ERR;
       
  1933 		}
       
  1934 	    }
       
  1935 	    sa_free_attr_string(path);
       
  1936 	}
       
  1937 	return (ret);
  1857 	return (ret);
  1938 }
  1858 }
  1939 
  1859 
  1940 /*
  1860 /*
  1941  * sa_delete_sharetab(path, proto)
  1861  * sa_delete_sharetab(path, proto)
  1944  */
  1864  */
  1945 
  1865 
  1946 int
  1866 int
  1947 sa_delete_sharetab(char *path, char *proto)
  1867 sa_delete_sharetab(char *path, char *proto)
  1948 {
  1868 {
  1949 	int ret = SA_OK;
  1869 	int	ret = SA_OK;
  1950 	int logging = 0;
  1870 
  1951 	FILE *sharetab;
  1871 	share_t	sh;
  1952 	sigset_t old;
  1872 
  1953 #ifdef lint
  1873 	/*
  1954 	proto = proto;
  1874 	 * Both the path and the proto are
  1955 #endif
  1875 	 * keys into the sharetab.
  1956 
  1876 	 */
  1957 	if (path != NULL) {
  1877 	if (path != NULL && proto != NULL) {
  1958 	    sharetab = fopen(SA_LEGACY_SHARETAB, "r+");
  1878 		(void) memset(&sh, '\0', sizeof (sh));
  1959 	    if (sharetab == NULL) {
  1879 		sh.sh_path = path;
  1960 		sharetab = fopen(SA_LEGACY_SHARETAB, "w+");
  1880 		sh.sh_fstype = proto;
  1961 	    }
  1881 
  1962 	    if (sharetab != NULL) {
  1882 		ret = sharefs(SHAREFS_REMOVE, &sh);
  1963 		/* should block keyboard level signals around the lock */
  1883 	}
  1964 		sablocksigs(&old);
  1884 
  1965 		(void) lockf(fileno(sharetab), F_LOCK, 0);
       
  1966 		ret = remshare(sharetab, path, &logging);
       
  1967 		(void) fflush(sharetab);
       
  1968 		(void) lockf(fileno(sharetab), F_ULOCK, 0);
       
  1969 		(void) fsync(fileno(sharetab));
       
  1970 		saunblocksigs(&old);
       
  1971 		(void) fclose(sharetab);
       
  1972 	    } else {
       
  1973 		if (errno == EACCES || errno == EPERM) {
       
  1974 		    ret = SA_NO_PERMISSION;
       
  1975 		} else {
       
  1976 		    ret = SA_CONFIG_ERR;
       
  1977 		}
       
  1978 	    }
       
  1979 	}
       
  1980 	return (ret);
  1885 	return (ret);
  1981 }
  1886 }